using System.ComponentModel;
using System.Reflection;
using AntdUI;
using DH.Commons.Base;
using DH.Devices.Vision;

namespace DHSoftware.Views
{
    public partial class CorrelatedCameraEdit : UserControl
    {
        List<KeyValuePair<string, int>> resultStates = GetFilteredEnumDescriptionsAndValuesres<ResultState>();

        // 获取枚举的描述和对应的值,只筛选出 OK 和 NG
        public static List<KeyValuePair<string, int>> GetFilteredEnumDescriptionsAndValuesres<T>() where T : Enum
        {
            return Enum.GetValues(typeof(T))
                .Cast<T>()
                /*   .Where(e => e.Equals(ResultState.OK) || e.Equals(ResultState.DetectNG))*/ // 只保留 OK 和 NG
                .Select(e =>
                {
                    // 通过反射获取 DescriptionAttribute 描述,如果没有描述,则使用枚举项名称
                    var description = e.GetType()
                        .GetField(e.ToString())
                        ?.GetCustomAttribute<DescriptionAttribute>()
                        ?.Description ?? e.ToString(); // 如果没有 DescriptionAttribute,则使用枚举名称

                    // 返回描述和值的键值对
                    return new KeyValuePair<string, int>(description, Convert.ToInt32(e));
                })
                .ToList();
        }
        private AntdUI.Window window;
        private List<RelatedCamera> Cameras;
        public bool submit;
        public CorrelatedCameraEdit(AntdUI.Window _window, List<RelatedCamera> cameras)
        {
            this.window = _window;
            Cameras = cameras;
            InitializeComponent();
            //设置默认值
            InitData();
            // 绑定事件
            BindEventHandler();
        }

        private void BindEventHandler()
        {
            button_ok.Click += Button_ok_Click;
            button_cancel.Click += Button_cancel_Click;
        }

        private void Button_cancel_Click(object sender, EventArgs e)
        {
            submit = false;
            this.Dispose();
        }

        private void Button_ok_Click(object sender, EventArgs e)
        {
            if (flowPanel1.Controls.Count > 0) 
            {
                Cameras.Clear();
                foreach (Control control in flowPanel1.Controls)
                {
                    if (control is AntdUI.Checkbox checkbox)
                    {
                        // 操作 CheckBox
                        bool isChecked = checkbox.Checked;
                        if (isChecked)
                        {
                            string name = checkbox.Text;
                            RelatedCamera relatedCamera = new RelatedCamera(name);
                            Cameras.Add(relatedCamera);
                        }
                       
                    }
                }

            }
            submit = true;
            this.Dispose();
        }

        private void InitData()
        {



            for (int i = 1; i < 15; i++)
            {
                string name = $"Cam{i}";
                var control = new AntdUI.Checkbox()
                {
                    AutoCheck = true,
                    Font = new System.Drawing.Font("Microsoft YaHei UI", 9F),
                    Size = new Size(90, 42),
                    Text = name,
                };
                foreach (var item in Cameras)
                {
                    if (item.CameraSourceId.Equals(name))
                    {
                        control.Checked = true;
                        break;
                    }
                }
                // 通过主窗口设置DPI控制添加控件保持缩放比例
                window.AutoDpi(control);
                flowPanel1.Controls.Add(control);
                control.BringToFront();
            }

        }

        private void CorrelatedCameraEdit_Load(object sender, EventArgs e)
        {
      
        }
    }
}