using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;

namespace InfraredManager
{
    public class InfraredCameraHelper
    {
        #region 单例
        private static InfraredCameraHelper _Instance;
        public static InfraredCameraHelper GetInstance()
        {
            if (_Instance == null) _Instance = new();
            return _Instance;
        }
        #endregion

        #region 创建销毁
        private GameObject _managerObject;
        private ScreenLocate _screenLocate;
        public void Create()
        {
            if (_managerObject) return;
            string prefabName = "WebCameraView";
            var o = UnityEngine.Object.Instantiate(Resources.Load<GameObject>(prefabName));
            UnityEngine.Object.DontDestroyOnLoad(o);
            o.name = prefabName;
            _screenLocate = o.GetComponent<ScreenLocate>();
            _screenLocate.InfraredCameraHelper = this;
            _managerObject = o;
            if (Application.platform == RuntimePlatform.Android) Application.targetFrameRate = 60;
        }
        public void Dispose()
        {
            if (_managerObject) UnityEngine.Object.DestroyImmediate(_managerObject);
            if (_Instance == this) _Instance = null;
        }
        #endregion

        #region 画面接口
        /// <summary>
        /// 获取相机画面贴图
        /// </summary>
        public Texture GetCameraTexture()
        {
            return _screenLocate.rawImage.texture;
        }
        /// <summary>
        /// 获取相机画面材质
        /// </summary>
        public Material GetCameraMaterial()
        {
            return _screenLocate.rawImage.material;
        }
        /// <summary>
        /// 设置亮度（0~10）
        /// </summary>
        public void SetBrightness(float value)
        {
            _screenLocate.rawImage.material.SetFloat("_Brightness", value);
        }
        /// <summary>
        /// 获取亮度（0~10）
        /// </summary>
        public float GetBrightness()
        {
            return _screenLocate.rawImage.material.GetFloat("_Brightness");
        }
        /// <summary>
        /// 设置饱和度（0~10）
        /// </summary>
        public void SetSaturation(float value)
        {
            _screenLocate.rawImage.material.SetFloat("_Saturation", value);
        }
        /// <summary>
        /// 获取饱和度（0~10）
        /// </summary>
        public float GetSaturation()
        {
            return _screenLocate.rawImage.material.GetFloat("_Saturation");
        }
        /// <summary>
        /// 设置对比度（0~10）
        /// </summary>
        public void SetContrast(float value)
        {
            _screenLocate.rawImage.material.SetFloat("_Contrast", value);
        }
        /// <summary>
        /// 获取对比度（0~10）
        /// </summary>
        public float GetContrast()
        {
            return _screenLocate.rawImage.material.GetFloat("_Contrast");
        }
        /// <summary>
        /// 设置相机画面分辨率
        /// </summary>
        public void SetCameraResolution(int width, int height)
        {
            _screenLocate.mUVCManager.DefaultWidth = width;
            _screenLocate.mUVCManager.DefaultHeight = height;
            _screenLocate.mUVCDrawer.DefaultWidth = width;
            _screenLocate.mUVCDrawer.DefaultHeight = height;
        }
        /// <summary>
        /// 获取相机画面分辨率
        /// </summary>
        public Vector2 GetCameraResolution()
        {
            return new Vector2(
                _screenLocate.mUVCDrawer.DefaultWidth,
                _screenLocate.mUVCDrawer.DefaultHeight
            );
        }
        #endregion

        #region 操作接口
        /// <summary>
        /// 单点模式开关
        /// </summary>
        public void SetSinglePoint(bool value)
        {
            _screenLocate.bSinglePoint = value;
        }
        /// <summary>
        /// 是否为单点模式
        /// </summary>
        public bool IsSinglePoint()
        {
            return _screenLocate.bSinglePoint;
        }
        /// <summary>
        /// 设置抖动过滤值
        /// </summary>
        public void SetShakeFilterValue(float value)
        {
            _screenLocate.filterDis = value;
        }
        /// <summary>
        /// 获取抖动过滤值
        /// </summary>
        public float GetShakeFilterValue()
        {
            return _screenLocate.filterDis;
        }
        /// <summary>
        /// 是否已经定位
        /// </summary>
        public bool IsScreenLoateOK()
        {
            return _screenLocate.IsScreenLoateOK();
        }
        /// <summary>
        /// 是否正在手动定位
        /// </summary>
        public bool IsScreenLocateManualDoing()
        {
            return _screenLocate.IsScreenLocateManualDoing();
        }
        /// <summary>
        /// 进入手动定位
        /// </summary>
        /// <returns>相机画面截图</returns>
        public Texture2D EnterScreenLocateManual()
        {
            return _screenLocate.EnterScreenLocateManual();
        }
        /// <summary>
        /// 退出/完成手动定位
        /// </summary>
        /// <param name="points">定位点（左下、右下、右上、左上）</param>
        public void QuitScreenLocateManual(List<Vector2> points)
        {
            _screenLocate.QuitScreenLocateManual(points);
        }
        #endregion

        #region 监听事件
        /// <summary>
        /// 单点更新事件
        /// </summary>
        public OnPositionUpdateEvent OnPositionUpdate;
        public delegate void OnPositionUpdateEvent(Vector2 position);
        public void InvokeOnPositionUpdate(Vector2 position)
        {
            try
            {
                OnPositionUpdate?.Invoke(position);
            }
            catch (Exception e)
            {
                Debug.LogError(e);
            }
        }
        /// <summary>
        /// 双点更新事件
        /// </summary>
        public OnPositionUpdate2Event OnPositionUpdate2;
        public delegate void OnPositionUpdate2Event(Vector2 position, int index);
        public void InvokeOnPositionUpdate2(Vector2 position, int index)
        {
            try
            {
                OnPositionUpdate2?.Invoke(position, index);
            }
            catch (Exception e)
            {
                Debug.LogError(e);
            }
        }
        #endregion
    }
}
