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

using Rect = UnityEngine.Rect;
using UnityEngine.UI;

namespace SLAMUVC
{
    [RequireComponent(typeof(UVCInterface))]
    public class MyUVCManager : MonoBehaviour
    {
        //public UVCCameraPixelFormat PixelFormat = UVCCameraPixelFormat.PIXEL_FORMAT_RGBX;
        public int width = 1280;
        public int height = 720;
        public CameraFormat mCameraFormat = CameraFormat.MJPEG;
        public RawImage rawImage;

        protected UVCInterface _interface;
        //protected List<string> _devices = new List<string>();

        private string[] resolutions;

        private bool isGetResolutions = false;

        // 滑块的当前值
        public float sliderValue;
        // 用于选择当前显示的 UVCCtrl

        private int selectedIndex = 0; // 用于选择当前显示的 UVCCtrl
        private int sliderStartPos = 100; // 滑块起始 X 坐标
        private int sliderWidth = 200; // 滑块的宽度
        private int sliderHeight = 20; // 滑块的高度
        private int controlHeight = 30; // 控件的高度
        private int verticalSpacing = 10; // 控件垂直间距

        private int brightnessValue;
        private int contrastValue;
        private void Awake()
        {
            _interface = GetComponent<UVCInterface>();
            _interface.systemCameraPermissionHandle += () =>
            {
                //授权系统相机之后，初始化红外相机
                _interface.InitCamera(width, height);
            };
        }
        // Use this for initialization
        void Start()
        {
            //_interface.InitCamera(width, height);

            // 初始化滑块的值
            sliderValue = 5.0f;

        }

        void Update()
        {

            if (rawImage != null || rawImage.texture.GetNativeTexturePtr() != _interface.CameraTexture.GetNativeTexturePtr())
            {
                rawImage.texture = _interface.CameraTexture;
                // GameObject cube = GameObject.Find("Cube");
                // cube.GetComponent<Renderer>().material.mainTexture = _interface.CameraTexture;
            }
        }
        void OnGUI()
        {
            int buttonWidth = 200;
            int buttonHeight = 80;
            int spacing = 10; // 间距
            int startX = 10;  // 按钮的起始X坐标
            int startY = 10;  // 按钮的起始Y坐标

            if (GUI.Button(new Rect(startX, startY, buttonWidth, buttonHeight), "OpenCamera"))
            {
                _interface.OpenCamera();
            }

            if (GUI.Button(new Rect(startX + buttonWidth + spacing, startY, buttonWidth, buttonHeight), "CloseCamera"))
            {
                _interface.CloseCamera();
            }

            if (GUI.Button(new Rect(startX + (buttonWidth + spacing) * 2, startY, buttonWidth, buttonHeight), "GetUvcCtrlList"))
            {
                _interface.GetUvcCtrlList();
            }
            if (GUI.Button(new Rect(startX + (buttonWidth + spacing) * 2, startY + buttonHeight + spacing, buttonWidth, buttonHeight), "重置参数"))
            {
                _interface.resetControlParams();
            }


            if (GUI.Button(new Rect(startX + (buttonWidth + spacing) * 3, startY, buttonWidth, buttonHeight), "水平翻转(H)"))
            {
                _interface.FlipHorizontally();
            }

            if (GUI.Button(new Rect(startX + (buttonWidth + spacing) * 3, startY + buttonHeight + spacing, buttonWidth, buttonHeight), "垂直翻转(V)"))
            {
                _interface.FlipVertically();
            }


            bool isPreviewing = _interface.GetIsPreviewing();

            if (isPreviewing)
            {
                int offsetY = startY + buttonHeight + spacing;

                if (resolutions == null && !isGetResolutions)
                {
                    isGetResolutions = true;

                    resolutions = _interface.GetSupportedResolutions();
                }

                if (resolutions != null)
                {
                    for (int i = 0; i < resolutions.Length; i++)
                    {
                        string resolution = resolutions[i];
                        if (GUI.Button(new Rect(startX, offsetY, buttonWidth * 2 + spacing, buttonHeight), resolution))
                        {
                            string[] res = resolution.ToString().Split('x');
                            width = int.Parse(res[0]);
                            height = int.Parse(res[1]);
                            _interface.ChangeCameraInfo(width, height);

                        }
                        offsetY += buttonHeight + spacing;
                    }
                }





                int sliderStartPos = startX + (buttonWidth + spacing) * 4 + 50;
                var uvcCtrls = _interface.UVCCtrls;
                if (uvcCtrls == null)
                {
                    GUI.Label(new Rect(sliderStartPos, 10, 200, 30), "No data available.");
                    return;
                }

                int yPos = 50;

                foreach (UVCCtrl uvcCtrl in uvcCtrls)
                {
                    GUI.Label(new Rect(sliderStartPos, yPos, 200, 30), $"Name: {uvcCtrl.name}");
                    yPos += controlHeight + verticalSpacing;

                    // 检查并显示亮度滑块
                    if (uvcCtrl.name.Equals("PU_BRIGHTNESS", System.StringComparison.OrdinalIgnoreCase))
                    {
                        GUI.Label(new Rect(sliderStartPos, yPos, 200, 30), "Brightness");
                        yPos += controlHeight + verticalSpacing;

                        // 临时变量用于控制亮度滑块
                        uvcCtrl.value = (int)GUI.HorizontalSlider(new Rect(sliderStartPos, yPos, sliderWidth, sliderHeight), uvcCtrl.value, uvcCtrl.limit[0], uvcCtrl.limit[1]);
                        yPos += sliderHeight + verticalSpacing;
                        if (brightnessValue != uvcCtrl.value)
                        {
                            brightnessValue = uvcCtrl.value;
                            SetBrightness(uvcCtrl.value);
                        }
                        // 按钮设置亮度
                        //if (GUI.Button(new Rect(sliderStartPos, yPos, sliderWidth, controlHeight), "Set Brightness"))
                        //{
                        //   // uvcCtrl.value = Mathf.RoundToInt(brightnessValue);
                        //    SetBrightness(uvcCtrl.value);
                        //}
                        yPos += controlHeight + verticalSpacing;
                    }
                    else if (uvcCtrl.name.Equals("PU_CONTRAST", System.StringComparison.OrdinalIgnoreCase))
                    {
                        GUI.Label(new Rect(sliderStartPos, yPos, 200, 30), "Contrast");
                        yPos += controlHeight + verticalSpacing;

                        // 临时变量用于控制对比度滑块
                        uvcCtrl.value = (int)GUI.HorizontalSlider(new Rect(sliderStartPos, yPos, sliderWidth, sliderHeight), uvcCtrl.value, uvcCtrl.limit[0], uvcCtrl.limit[1]);
                        yPos += sliderHeight + verticalSpacing;
                        if (contrastValue != uvcCtrl.value)
                        {
                            contrastValue = uvcCtrl.value;
                            SetContrast(uvcCtrl.value);
                        }
                        // 按钮设置对比度
                        //if (GUI.Button(new Rect(sliderStartPos, yPos, sliderWidth, controlHeight), "Set Contrast"))
                        //{
                        //    //uvcCtrl.value = Mathf.RoundToInt(contrastValue);
                        //    SetContrast(uvcCtrl.value);
                        //}
                        yPos += controlHeight + verticalSpacing;
                    }
                }

            }
        }
        public void SetBrightness(int value)
        {
            _interface.SetBrightness(value);
        }

        public void SetContrast(int value)
        {
            _interface.SetContrast(value);
        }
        // Update is called once per frame
        //void OnGUI()
        //{
        //    int offset = 10;
        //    if (GUI.Button(new Rect(10, offset, 400, 100), "OpenCamera"))
        //    {
        //        _interface.OpenCamera();
        //    }
        //    if (GUI.Button(new Rect(420, offset, 400, 100), "CloseCamera"))
        //    {
        //        _interface.CloseCamera();
        //    }

        //    bool isPreviewing = _interface.GetIsPreviewing();

        //    if (isPreviewing)
        //    {
        //        int offset2 = 10;
        //        if (GUI.Button(new Rect(220, offset2, 400, 100), "MJPEG"))
        //        {
        //            mCameraFormat = CameraFormat.MJPEG;
        //        }

        //        offset2 += 110;
        //        if (GUI.Button(new Rect(220, offset2, 400, 100), "YUV"))
        //        {
        //            mCameraFormat = CameraFormat.YUV;
        //        }

        //        // resolutions  
        //        if(resolutions == null) resolutions = _interface.GetSupportedResolutions();

        //        int offset3 = 10;

        //        if (resolutions != null)
        //        {
        //            foreach (var resolution in resolutions)
        //            {
        //                if (GUI.Button(new Rect(430, offset3, 400, 100), resolution))
        //                {
        //                    string[] res = resolution.ToString().Split('x');

        //                    width = int.Parse(res[0]);
        //                    height = int.Parse(res[1]);

        //                    _interface.ChangeCameraInfo(width, height, mCameraFormat.ToString());
        //                }
        //                offset3 += 110;
        //            }
        //        }
        //    }
        //}
    }

}
