using PlcCom;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing.Design;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using PlcUiControl;
using PlcUiForm;
using Sunny.UI;
namespace PlcComponent
{
    public partial class PlcOptionSelection : Component, ISingleOutputComponent
    {
        public PlcOptionSelection()
        {
            InitializeComponent();
        }
        public PlcOptionSelection(IContainer container)
        {
            container.Add(this);

            InitializeComponent();
        }
        [Category("PLC")]
        [Description("PLC读地址，必须为Bool类型")]
        [Editor(typeof(NodeTypeEditor), typeof(UITypeEditor))]
        public Node ReadNode { get; set; }

        [Category("PLC")]
        [Description("确认地址1，必须为Bool类型")]
        [Editor(typeof(NodeTypeEditor), typeof(UITypeEditor))]
        public Node EnsureNode1 { get; set; }

        [Category("PLC")]
        [Description("确认地址2，必须为Bool类型")]
        [Editor(typeof(NodeTypeEditor), typeof(UITypeEditor))]
        public Node EnsureNode2 { get; set; }

        [Category("PLC")]
        [Description("窗体名称")]
        [DefaultValue("请选择")]
        public string FormCaption { get; set; }

        [Category("PLC")]
        [Description("提示信息")]
        [DefaultValue("提示")]
        public string Message { get; set; }

        [Category("PLC")]
        [Description("选项1的名称")]
        public string Text1 { get; set; }
        [Category("PLC")]
        [Description("选项2的名称")]
        public string Text2 { get; set; }


        [Category("PLC")]
        [Description("第一个按钮按下")]
        public event EventHandler Button1MouseDown;
        [Category("PLC")]
        [Description("第二个按钮按下")]
        public event EventHandler Button2MouseDown;
        [Category("PLC")]
        [Description("第一个按钮抬起")]
        public event EventHandler Button1MouseUp;
        [Category("PLC")]
        [Description("第二个按钮抬起")]
        public event EventHandler Button2MouseUp;

        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public IPlcComProtocol Protocol { get; set; }

        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public object State
        {
            get => state;
            set
            {
                if (!object.Equals(state, value))
                {
                    state = value;
                    OnPLCRegisterValueChanged?.Invoke(this, ReadNode, state);
                }
                if (value is bool b)
                {
                    if (b)
                    {
                        if (optionSelectionForm == null || optionSelectionForm.IsDisposed)
                            ShowOptionSelectionForm();
                    }
                    else
                    {
                        if (optionSelectionForm != null && !optionSelectionForm.IsDisposed)
                        {
                            if(plcUiButton1.Read().Equals(true))
                                plcUiButton1.WriteValue(false);

                            if (plcUiButton2.Read().Equals(true))
                                plcUiButton2.WriteValue(false);

                            optionSelectionForm.Close();
                        }
                    }
                }
            }
        }
        private object state;
        PlcUiButton plcUiButton1, plcUiButton2;
        PlcBaseForm optionSelectionForm;
        void ShowOptionSelectionForm()
        {
            optionSelectionForm = new PlcBaseForm();
            optionSelectionForm.Name = FormCaption;

            UILabel uILabel = new UILabel();
            uILabel.AutoSize = true;
            uILabel.Text = Message;
            uILabel.Left = 40;
            uILabel.Top = 60;
            optionSelectionForm.Controls.Add(uILabel);

            plcUiButton1 = new PlcUiButton();
            plcUiButton1.ButtonType = ButtonType.保持型;
            plcUiButton1.WriteNode = EnsureNode1;
            plcUiButton1.Name = Text1;
            plcUiButton1.Text = Text1;
            plcUiButton1.Size = new System.Drawing.Size(80, 40);
            plcUiButton1.Left = 80;
            plcUiButton1.Top = 120;
            plcUiButton1.Height = 40;
            plcUiButton1.OnSymbol = 0;
            plcUiButton1.OffSymbol = 0;
            plcUiButton1.Symbol = 0;
            plcUiButton1.Radius = 20;
            plcUiButton1.AfterMouseDown += PlcUiButton1_AfterMouseDown;
            plcUiButton1.AfterMouseUp += PlcUiButton1_AfterMouseUp;
            optionSelectionForm.Controls.Add(plcUiButton1);

            plcUiButton2 = new PlcUiButton();
            plcUiButton2.ButtonType = ButtonType.保持型;
            plcUiButton2.WriteNode = EnsureNode2;
            plcUiButton2.Name = Text2;
            plcUiButton2.Text = Text2;
            plcUiButton2.Size = new System.Drawing.Size(80, 40);
            plcUiButton2.Left = 220;
            plcUiButton2.Top = 120;
            plcUiButton2.Height = 40;
            plcUiButton2.OnSymbol = 0;
            plcUiButton2.OffSymbol = 0;
            plcUiButton2.Symbol = 0;
            plcUiButton2.Radius = 20;
            plcUiButton2.AfterMouseDown += PlcUiButton2_AfterMouseDown;
            plcUiButton2.AfterMouseUp += PlcUiButton2_AfterMouseUp;
            optionSelectionForm.Controls.Add(plcUiButton2);

            optionSelectionForm.TopMost = true;
            optionSelectionForm.Text = FormCaption;
            optionSelectionForm.Width = 400;
            optionSelectionForm.Height = 200;
            optionSelectionForm.MaximizeBox = false;
            optionSelectionForm.MinimizeBox = false;
            optionSelectionForm.Show();
        }
        private void PlcUiButton1_AfterMouseDown(object obj)
        {
            Button1MouseDown?.Invoke(this, EventArgs.Empty);
        }
        private void PlcUiButton2_AfterMouseDown(object obj)
        {
            Button2MouseDown?.Invoke(this, EventArgs.Empty);
        }
        private void PlcUiButton1_AfterMouseUp(object obj)
        {
            Button1MouseUp?.Invoke(this, EventArgs.Empty);
        }
        private void PlcUiButton2_AfterMouseUp(object obj)
        {
            Button2MouseUp?.Invoke(this, EventArgs.Empty);
        }
        public event RegisterValueChangedHandler OnPLCRegisterValueChanged;
        public event ReadRegisterFailedHandler OnReadRegisterFailed;
        public event UpdateComponentStateFailedHandler OnUpdateComponentStateFailed;


        public object Read()
        {
            if (Protocol != null && ReadNode != null)
            {
                object result;
                var rt = Protocol.ReadValue(ReadNode.Value, out result);
                if (rt == 0)
                {
                    UpdateValue(result);
                    return result;
                }
                else
                {
                    OnReadRegisterFailed?.Invoke(this, ReadNode, rt);
                }
            }
            return null;
        }
        public void UpdateValue(object value)
        {
            if (value is bool)
                State = value;
            else
                OnUpdateComponentStateFailed?.Invoke(this, ReadNode, value, "读取到的值不是Bool类型");
        }

    }
}
