using Model;
using PlcUiControl;
using PlcUiForm;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using YangjieTester.Properties;

namespace YangjieTester.主界面
{
    [FormDescriptionAttribute("跑马灯")]
    public partial class FlashLightForm : PlcBaseForm
    {
        PlcUiLight[] MainLights = new PlcUiLight[36];

        PlcUiLight[] TestLoadLights = new PlcUiLight[3];
        PlcUiLight[] TestUnloadLights = new PlcUiLight[3];
        PlcUiLight[] SubLights = new PlcUiLight[100];
        PlcUiLight[] LaserLights = new PlcUiLight[4];
        List<PlcUiLight> AllLights= new List<PlcUiLight>(); 

        List<PlcUiStateDisplay> AllStateDisplays = new List<PlcUiStateDisplay>();

        Dictionary<PlcUiLight,PlcUiStateDisplay> Lights_StateDisplays= new Dictionary<PlcUiLight, PlcUiStateDisplay>();
        public FlashLightForm()
        {
            for (int i = 0; i < MainLights.Length; i++)
            {
                MainLights[i] = new PlcUiLight();
                MainLights[i].Location = new Point(100, 100);
                MainLights[i].Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
                MainLights[i].MinimumSize = new System.Drawing.Size(1, 1);
                MainLights[i].MyTextAlign = System.Drawing.ContentAlignment.MiddleCenter;
                MainLights[i].Name = $"plcUiLight{i+1:D2}";
                MainLights[i].NodeName = $"{i + 1:D2}";
                MainLights[i].Off_Color = System.Drawing.Color.FromArgb(((int)(((byte)(140)))), ((int)(((byte)(140)))), ((int)(((byte)(140)))));
                MainLights[i].OffText = null;
                MainLights[i].On_Color = Color.Green;
                MainLights[i].OnText = null;
                MainLights[i].Radius = 35;
                MainLights[i].ReadNode = new Node($"Application.Var_state.RotorPieceInf[{i+1}].b_Exist", NodeType.UINT, "");
                MainLights[i].RequiredPermissionLevel = PermissionLevel.操作工;
                MainLights[i].ShowText = true;
                MainLights[i].Size = new System.Drawing.Size(30, 30);
                MainLights[i].State = Sunny.UI.UILightState.Off;
                MainLights[i].TabIndex = i+1;
                MainLights[i].Text = $"{i + 1:D2}";
                MainLights[i].TextCoverSymbol = true;
                MainLights[i].WriteNode = null;
            }
            this.Controls.AddRange(MainLights);

            for (int i = 0; i < TestLoadLights.Length; i++)
            {
                TestLoadLights[i] = new PlcUiLight();
                TestLoadLights[i].Location = new Point(100, 100);
                TestLoadLights[i].Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
                TestLoadLights[i].MinimumSize = new System.Drawing.Size(1, 1);
                TestLoadLights[i].MyTextAlign = System.Drawing.ContentAlignment.MiddleCenter;
                TestLoadLights[i].Name = $"plcUiLight{i + 1:D2}";
                TestLoadLights[i].NodeName = $"{i + 1:D2}";
                TestLoadLights[i].Off_Color = System.Drawing.Color.FromArgb(((int)(((byte)(140)))), ((int)(((byte)(140)))), ((int)(((byte)(140)))));
                TestLoadLights[i].OffText = null;
                TestLoadLights[i].On_Color = Color.Green;
                TestLoadLights[i].OnText = null;
                TestLoadLights[i].Radius = 35;

                TestLoadLights[i].ReadNode = new Node($"Application.A14_ThermalTest_Auto.dutOutPut入料盘材料信息[{i + 1}].b_Exist", NodeType.UINT, "");
                TestLoadLights[i].RequiredPermissionLevel = PermissionLevel.操作工;
                TestLoadLights[i].ShowText = true;
                TestLoadLights[i].Size = new System.Drawing.Size(20, 20);
                TestLoadLights[i].State = Sunny.UI.UILightState.Off;
                TestLoadLights[i].TabIndex = i + 1;
                TestLoadLights[i].Text = $"{i + 1:D2}";
                TestLoadLights[i].TextCoverSymbol = true;
                TestLoadLights[i].WriteNode = null;
            }
            this.Controls.AddRange(TestLoadLights);

            for (int i = 0; i < TestUnloadLights.Length; i++)
            {
                TestUnloadLights[i] = new PlcUiLight();
                TestUnloadLights[i].Location = new Point(100, 100);
                TestUnloadLights[i].Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
                TestUnloadLights[i].MinimumSize = new System.Drawing.Size(1, 1);
                TestUnloadLights[i].MyTextAlign = System.Drawing.ContentAlignment.MiddleCenter;
                TestUnloadLights[i].Name = $"plcUiLight{i + 1:D2}";
                TestUnloadLights[i].NodeName = $"{i + 1:D2}";
                TestUnloadLights[i].Off_Color = System.Drawing.Color.FromArgb(((int)(((byte)(140)))), ((int)(((byte)(140)))), ((int)(((byte)(140)))));
                TestUnloadLights[i].OffText = null;
                TestUnloadLights[i].On_Color = Color.Green;
                TestUnloadLights[i].OnText = null;
                TestUnloadLights[i].Radius = 35;
                TestUnloadLights[i].ReadNode = new Node($"Application.A14_ThermalTest_Auto.dutOutPut出料盘材料信息[{i + 1}].b_Exist", NodeType.UINT, "");
                TestUnloadLights[i].RequiredPermissionLevel = PermissionLevel.操作工;
                TestUnloadLights[i].ShowText = true;
                TestUnloadLights[i].Size = new System.Drawing.Size(20, 20);
                TestUnloadLights[i].State = Sunny.UI.UILightState.Off;
                TestUnloadLights[i].TabIndex = i + 1;
                TestUnloadLights[i].Text = $"{i + 1:D2}";
                TestUnloadLights[i].TextCoverSymbol = true;
                TestUnloadLights[i].WriteNode = null;
            }
            this.Controls.AddRange(TestUnloadLights);

            for (int i = 0; i < SubLights.Length; i++)
            {
                SubLights[i] = new PlcUiLight();
                SubLights[i].Location = new Point(100, 100);
                SubLights[i].Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
                SubLights[i].MinimumSize = new System.Drawing.Size(1, 1);
                SubLights[i].MyTextAlign = System.Drawing.ContentAlignment.MiddleCenter;
                SubLights[i].Name = $"plcUiLight{i + 1:D2}";
                SubLights[i].NodeName = $"{i + 1:D2}";
                SubLights[i].Off_Color = System.Drawing.Color.FromArgb(((int)(((byte)(140)))), ((int)(((byte)(140)))), ((int)(((byte)(140)))));
                SubLights[i].OffText = null;
                SubLights[i].On_Color = Color.Green;
                SubLights[i].OnText = null;
                SubLights[i].Radius = 35;
                SubLights[i].ReadNode = new Node($"Application.A14_ThermalTest_Auto.dutOutPut热测盘材料信息[{i + 1}].b_Exist", NodeType.UINT, "");

                SubLights[i].RequiredPermissionLevel = PermissionLevel.操作工;
                SubLights[i].ShowText = true;
                SubLights[i].Size = new System.Drawing.Size(18, 18);
                SubLights[i].State = Sunny.UI.UILightState.Off;
                SubLights[i].TabIndex = i + 1;
                SubLights[i].Text = $"{i + 1:D2}";
                SubLights[i].TextCoverSymbol = true;
                SubLights[i].WriteNode = null;
            }
            this.Controls.AddRange(SubLights);

            for (int i = 0; i < LaserLights.Length; i++)
            {
                LaserLights[i] = new PlcUiLight();
                LaserLights[i].Location = new Point(100, 100);
                LaserLights[i].Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
                LaserLights[i].MinimumSize = new System.Drawing.Size(1, 1);
                LaserLights[i].MyTextAlign = System.Drawing.ContentAlignment.MiddleCenter;
                LaserLights[i].Name = $"plcUiLight{i + 1:D2}";
                LaserLights[i].NodeName = $"{i + 1:D2}";
                LaserLights[i].Off_Color = System.Drawing.Color.FromArgb(((int)(((byte)(140)))), ((int)(((byte)(140)))), ((int)(((byte)(140)))));
                LaserLights[i].OffText = null;
                LaserLights[i].On_Color = Color.Green;
                LaserLights[i].OnText = null;
                LaserLights[i].Radius = 35;
                LaserLights[i].ReadNode = new Node($"Application.A23_LaserMarking_Auto.dutOutPut镭射盘材料信息[{i + 1}].b_Exist", NodeType.UINT, "");
                LaserLights[i].RequiredPermissionLevel = PermissionLevel.操作工;
                LaserLights[i].ShowText = true;
                LaserLights[i].Size = new System.Drawing.Size(20, 20);
                LaserLights[i].State = Sunny.UI.UILightState.Off;
                LaserLights[i].TabIndex = i + 1;
                LaserLights[i].Text = $"{i + 1:D2}";
                LaserLights[i].TextCoverSymbol = true;
                LaserLights[i].WriteNode = null;
            }
            this.Controls.AddRange(LaserLights);

            AllLights.AddRange(this.Controls.Cast<Control>().Select(c=>(PlcUiLight)c).ToList());


            var stateDisplayItems = new List<Item>(Destionations.Select(d => new Item() { State = d.destination.ToString(), Message = d.destination.ToString(), BackColor = Color.White, ForeColor = Color.Black, Blink = false }).ToList());

            AllStateDisplays.AddRange(AllLights.Select(light=> {
                var s = new PlcUiStateDisplay()
                {
                    BorderStyle = BorderStyle.FixedSingle,
                    TextAlign = ContentAlignment.MiddleCenter,
                    Location = new Point(light.Right + 5, light.Top),
                    Font = light.Font,
                    MinimumSize = new System.Drawing.Size(1, 1),
                    Name = $"plcUiStateDisplay_{light.Name}",
                    //ReadNode = new Node(ReplaceAfterLastDot(light.ReadNode.Value, "Goal"), NodeType.UINT, ""),
                    ReadNode = new Node(light.ReadNode.Value.Replace("b_Exist","Goal"), NodeType.UINT, ""),
                    RequiredPermissionLevel = 0,
                    Size = light.Size,
                    TabIndex = light.TabIndex + 1000,
                    Items = stateDisplayItems
                };
                Lights_StateDisplays.Add(light, s);
                return s;
            }).ToList());

            this.Controls.AddRange(AllStateDisplays.ToArray());

            AllStateDisplays.ForEach(s => s.Visible=false);

            InitializeComponent();
            CircleMain.PaintCompleted += CircleMain_PaintCompleted;
            CircleLoad.PaintCompleted += CircleLoad_PaintCompleted;
            CircleUnload.PaintCompleted += CircleUnload_PaintCompleted;
            CircleSub.PaintCompleted += CircleSub_PaintCompleted;
            CircleLaser.PaintCompleted += CircleLaser_PaintCompleted;
        }
        public static string ReplaceAfterLastDot(string input, string newSuffix)
        {
            if (string.IsNullOrEmpty(input))
                return input;

            int lastDotIndex = input.LastIndexOf('.');
            if (lastDotIndex == -1)
                return newSuffix; // 没有 '.'，整个字符串视为后缀

            return input.Substring(0, lastDotIndex + 1) + newSuffix;
        }
        List<(int destination, string alias)> Destionations = new List<(int, string)>()
        {
            (0,""),
            (1,"编带1"),
            (2,"编带2"),
            (11,"料盒1-1"),
            (12,"料盒1-21"),
            (13,"料盒1-3"),
            (14,"料盒1-4"),
            (21,"料盒2-1"),
            (22,"料盒2-2"),
            (23,"料盒2-3"),
            (24,"料盒2-4"),
            (31,"料盒3-1"),
            (32,"料盒3-2"),
            (33,"料盒3-3"),
            (34,"料盒3-4"),
            (41,"料盒4-1"),
            (42,"料盒4-2"),
            (43,"料盒4-3"),
            (44,"料盒4-4"),
            (51,"料盒5-1"),
            (52,"料盒5-2"),
            (53,"料盒5-3"),
            (54,"料盒5-4"),
        };
        

        private void CircleMain_PaintCompleted(object sender, EventArgs e)
        {
            for (int i = 0; i < CircleMain.Points.Count; i++)
            {
                MainLights[i].Left = CircleMain.Left + CircleMain.Points[i].X - MainLights[i].Width / 2;
                MainLights[i].Top = CircleMain.Top + CircleMain.Points[i].Y - MainLights[i].Height / 2;
                Lights_StateDisplays[MainLights[i]].Location = MainLights[i].Location;
            }
        }
        private void CircleLoad_PaintCompleted(object sender, EventArgs e)
        {
            for (int i = 0; i < CircleLoad.Points.Count; i++)
            {
                TestLoadLights[i].Left = CircleLoad.Left + CircleLoad.Points[i].X - TestLoadLights[i].Width / 2;
                TestLoadLights[i].Top = CircleLoad.Top + CircleLoad.Points[i].Y - TestLoadLights[i].Height / 2;
                Lights_StateDisplays[TestLoadLights[i]].Location = TestLoadLights[i].Location;
            }
        }
        private void CircleUnload_PaintCompleted(object sender, EventArgs e)
        {
            for (int i = 0; i < CircleUnload.Points.Count; i++)
            {
                TestUnloadLights[i].Left = CircleUnload.Left + CircleUnload.Points[i].X - TestUnloadLights[i].Width / 2;
                TestUnloadLights[i].Top = CircleUnload.Top + CircleUnload.Points[i].Y - TestUnloadLights[i].Height / 2;
                Lights_StateDisplays[TestUnloadLights[i]].Location = TestUnloadLights[i].Location;
            }
        }
        private void CircleSub_PaintCompleted(object sender, EventArgs e)
        {
            for (int i = 0; i < CircleSub.Points.Count; i++)
            {
                SubLights[i].Left = CircleSub.Left + CircleSub.Points[i].X - SubLights[i].Width / 2;
                SubLights[i].Top = CircleSub.Top + CircleSub.Points[i].Y - SubLights[i].Height / 2;
                Lights_StateDisplays[SubLights[i]].Location = SubLights[i].Location;
            }
        }
        private void CircleLaser_PaintCompleted(object sender, EventArgs e)
        {
            for (int i = 0; i < CircleLaser.Points.Count; i++)
            {
                LaserLights[i].Left = CircleLaser.Left + CircleLaser.Points[i].X - LaserLights[i].Width / 2;
                LaserLights[i].Top = CircleLaser.Top + CircleLaser.Points[i].Y - LaserLights[i].Height / 2;
                Lights_StateDisplays[LaserLights[i]].Location = LaserLights[i].Location;
            }
        }

        private void uiSwitch1_ValueChanged(object sender, bool value)
        {
            AllStateDisplays.ForEach(s => s.Visible = uiSwitch1.Active);
            AllLights.ForEach(l => l.Visible = !uiSwitch1.Active);
            lblDestinationDefine.Visible = uiSwitch1.Active;
        }
    }
}
