using DbCommon.BusinessCore.BaseCore;
using ProjectManagementSystem.Common.Core;
using ProjectManagementSystem.Common.Logger;
using ProjectManagementSystem.DispatchPack;
using ProjectManagementSystem.DispatchPack.Entity;
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;

namespace ProjectManagementSystem.UI
{
    public partial class DispatchPackUI : UserControl, IDisplayUI
    {
        private StationInfoManager stationInfoManager = new StationInfoManager();

        public DispatchPackUI()
        {
            InitializeComponent();
        }

        public bool Init()
        {
            try
            {
                PackEngine.Instance.OnDisplayInfo += Instance_OnDisplayInfo;
                PackEngine.Instance.OnDisplayEntityInfo += Instance_OnDisplayEntityInfo;
                PackEngine.Instance.OnStart += Instance_OnStart;

                return true;

            }
            catch (Exception ex)
            {
                CLog.Instance.TaskLog.WriteException("UI", ex);
            }

            return false;
        }

        private void Instance_OnStart()
        {
            this.Invoke(new MethodInvoker(delegate
            {
                InitStationUI();
            }));
        }

        private void InitStationUI()
        {
            this.tabControl1.Controls.Clear();

            var entityList = PackEngine.Instance.EntityBaseList;
            for (int i = 0; i < entityList.Count; i++)
            {
                EntityBase entity = entityList[i];

                var tabPage = new System.Windows.Forms.TabPage();
                tabPage.Location = new System.Drawing.Point(8, 39);
                tabPage.Name = string.Format("tabPage{0}", i + 1);
                tabPage.Padding = new System.Windows.Forms.Padding(3);
                tabPage.Size = new System.Drawing.Size(1396, 474);
                tabPage.TabIndex = i;
                tabPage.Text = entity.LineInfo.Name;
                tabPage.UseVisualStyleBackColor = true;


                var flowLayoutPanel = new System.Windows.Forms.FlowLayoutPanel();
                flowLayoutPanel.Name = string.Format("flowLayoutPanel{0}", i + 1);
                flowLayoutPanel.Dock = DockStyle.Fill;
                flowLayoutPanel.AutoScroll = true;

                foreach (var item in entity.StationInfoList)
                {
                    StationUI stationUI = new StationUI();
                    stationUI.Init(item);
                    stationUI.Display();
                    flowLayoutPanel.Controls.Add(stationUI);

                    PackEngine.Instance.StationUIList.Add(stationUI);
                }

                tabPage.Controls.Add(flowLayoutPanel);
                this.tabControl1.Controls.Add(tabPage);
            }
        }

        public void UpdateDisplay()
        {
            //if (!Visible) return;

            //var stationUIList = CoreEngine.Instance.StationUIList;
            //for (int i = 0; i < stationUIList.Count; i++)
            //{
            //    var btn = stationUIList[i];
            //    btn.BeginInvoke(new MethodInvoker(delegate
            //    {
            //        btn.Display();
            //    }));
            //}
        }

        private void Instance_OnDisplayEntityInfo(EntityBase arg1, string arg2)
        {
            foreach (Control item in this.tabControl1.Controls)
            {
                if (item.TabIndex == arg1.Index)
                {
                    this.BeginInvoke((Action)(() =>
                    {
                        item.Text = string.Format("{0}({1})", arg1.LineInfo.Name, arg2);
                    }));
                    break;
                }
            }
        }

        private void Instance_OnDisplayInfo(string obj)
        {
            this.BeginInvoke((Action)(() =>
            {
                label1.Text = obj;
            }));
        }

        private async Task RailAgvOperate(object sender, Action<int> action)
        {
            (sender as Control).Enabled = false;

            int agvMaxNum = PackEngine.Instance.AgvMaxNum;
            await Task.Factory.StartNew(() =>
            {
                for (int index = 0; index < 3; index++)
                {
                    for (int i = 1; i <= agvMaxNum; i++)
                    {
                        action.Invoke(i);
                        System.Threading.Thread.Sleep(20);
                    }

                    System.Threading.Thread.Sleep(100);
                }
            });

            (sender as Control).Enabled = true;
            (sender as Control).Focus();
        }

        private async void button1_Click(object sender, EventArgs e)
        {
            var speed = numericUpDown1.Value;
            await RailAgvOperate(sender, id => DeviceControl.Instance.RailAgv_SpeedChange(id, speed));
        }

        private async void button2_Click(object sender, EventArgs e)
        {
            await RailAgvOperate(sender, id => DeviceControl.Instance.RailAgv_StartButton(id));
        }

        private async void button3_Click(object sender, EventArgs e)
        {
            await RailAgvOperate(sender, id => DeviceControl.Instance.RailAgv_StopButton(id));
        }

        private void button4_Click(object sender, EventArgs e)
        {
            int interval = (int)numericUpDown2.Value;
            int alarm = (int)numericUpDown3.Value;

            CountDown(interval, alarm);
        }

        private void button5_Click(object sender, EventArgs e)
        {
            int interval = 0;
            int alarm = 0;

            CountDown(interval, alarm);
        }

        private void CountDown(int interval, int alarm)
        {
            var dataList = stationInfoManager.GetList();
            for (int i = 0; i < dataList.Count; i++)
            {
                dataList[i].CountInterval = interval;
                dataList[i].CountAlarm = alarm;
            }

            bool result = stationInfoManager.UpdateCountdown(dataList);
            if (result)
            {
                MessageBox.Show("操作成功");
            }
            else
            {
                MessageBox.Show("操作失败");
            }
        }
    }
}
