using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Color = UnityEngine.Color;

public class o0WebCamera : MonoBehaviour {
    public int width = 1280;
    public int height = 720;
    public int fps = 60;
    public RawImage rawImage;
    public RawImage rawImage2;
    public RawImage rawImage3;
    public RawImage rawImage4;
    public RawImage rawImage5;
    public RawImage rawImage6;
    private WebCamTexture _webCamTexture;
    public WebCamTexture webCamTexture { get => _webCamTexture; }

    //o0.Project.WebCam o0WebCam = null;
    o0.Project.InfraredIdentification o0InfraredIdentification;
    static public List<RawImage> DebugImage = new List<RawImage>();
    static public void DebugTexture(int index, Texture texture)
    {
        if (index >= DebugImage.Count)
            return;
        Destroy(DebugImage[index].texture);
        DebugImage[index].texture = texture;
    }
    static public void SetScreen(UnityEngine.Color? color = null)
    {
        var canvas = GameObject.Find("WebCameraView").GetComponent<RectTransform>();
        var background = canvas.Find("Background").GetComponent<RectTransform>();
        background.gameObject.SetActive(color != null);
        background.GetComponent<RawImage>().color = color ?? Color.black;
    }

    void Start()
    {
        DebugImage.Add(rawImage);
        DebugImage.Add(rawImage2);
        DebugImage.Add(rawImage3);
        DebugImage.Add(rawImage4);
        DebugImage.Add(rawImage5);
        DebugImage.Add(rawImage6);

        o0InfraredIdentification = new o0.Project.InfraredIdentification(new o0.Geometry2D.Vector<int>(width, height));
        OnClick_Open();
    }

    void Update()
    {
        if (_webCamTexture) {
            width = _webCamTexture.width;
            height = _webCamTexture.height;
            fps = (int)_webCamTexture.requestedFPS;

            if (_webCamTexture.didUpdateThisFrame)
            {

                //Debug.Log(o0InfraredIdentification);
                o0InfraredIdentification.Update(_webCamTexture);
            }
        }
        if (Input.GetKeyDown(KeyCode.Alpha1))
            o0InfraredIdentification.LocateScreen();
        if (Input.GetKeyDown(KeyCode.Alpha2))
            SetScreen(Color.white);
        if (Input.GetKeyDown(KeyCode.Alpha3))
            SetScreen(Color.black);
        if (Input.GetKeyDown(KeyCode.Alpha4))
            SetScreen(null);
    }

    public void OnClick_Open()
    {
        if (_webCamTexture != null)
        {
            Debug.LogError("开启失败，请先关闭正在使用的摄像头！");
            return;
        }
        if (Application.HasUserAuthorization(UserAuthorization.WebCam))
        {
            WebCamDevice[] devices = WebCamTexture.devices;
            if (devices.Length == 0)
            {
                Debug.LogError("开启失败，没找到可用的摄像头！");
                return;
            }
            string deviceName = devices[0].name;
            _webCamTexture = new WebCamTexture(deviceName, width, height, fps);
            _webCamTexture.Play();
            if (rawImage) rawImage.texture = _webCamTexture;
            Debug.Log("成功开启摄像头 " + deviceName);
            //o0WebCam = new o0.Project.WebCam(_webCamTexture);
            /*
            Task.Run(() => {
                if(_webCamTexture.didUpdateThisFrame)
            });/**/
        }
        else
        {
            Debug.LogError("开启失败，用户未授予摄像头权限！");
        }
    }

    public void OnClick_Close()
    {
        if (_webCamTexture != null)
        {
            _webCamTexture.Stop();
            _webCamTexture = null;
            if (rawImage) rawImage.texture = null;
            Debug.Log("成功关闭摄像头");
            //o0WebCam.Dispose();
            //o0WebCam = null;
        }
    }
}
