using OnlineStore.Common;
using OnlineStore.DeviceLibrary;
using OnlineStore.LoadCSVLibrary;
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using UserFromControl;

namespace OnlineStore.XLRStore.useControl
{
    public partial class CT : UserControl
    {
        private InputEquip inputEquip;
        private BoxEquip boxBean;
        private BatchMoveBean moveBean;
        public CT()
        {
            InitializeComponent();
            StoreManager.Loadfinishevent += StoreManager_Loadfinishevent;
            this.Enabled = false;
        }

        private void StoreManager_Loadfinishevent(object sender, bool e)
        {
            inputEquip = StoreManager.XLRStore.inputEquip;
            boxBean = StoreManager.XLRStore.boxEquip;
            timer1.Enabled = true;
            this.Enabled = true;
            axisMoveControl1.LoadData(StoreManager.XLRStore.inputEquip, new AxisBean[] { inputEquip.BatchMove_A.BatchAxis, inputEquip.BatchMove_B.BatchAxis });
            boxBean.cttime += BoxBean_cttime;
            inputEquip.cttime += InputEquip_cttime;
            LoadIOList();
        }

        private void InputEquip_cttime(object sender, double e)
        {
            var cc = DateTime.Now - cttime;
            lbl_t1.Text = $"CT:{cc.TotalSeconds:F2}";
        }

        private void BoxBean_cttime(object sender, double e)
        {
            lbl_t2.Text = $"CT:{e:F2}";
        }
        DateTime cttime;
        private void btn_ct_Click(object sender, EventArgs e)
        {
            if (StoreManager.XLRStore.runStatus != RunStatus.Runing)
            {
                MessageBox.Show("请先启动设备");
                return;
            }
            if (StoreManager.XLRStore.boxEquip.IOValue(IO_Type.UpperArea_Check_A).Equals(IO_VALUE.LOW)
                 || StoreManager.XLRStore.boxEquip.IOValue(IO_Type.UpperArea_Check_B).Equals(IO_VALUE.LOW))
            {
                MessageBox.Show("请在出库缓存位上放上料盘");
                return;
            }
            if (StoreManager.XLRStore.boxEquip.IOValue(IO_Type.UnderArea_Check_A).Equals(IO_VALUE.HIGH)
                 || StoreManager.XLRStore.boxEquip.IOValue(IO_Type.UnderArea_Check_B).Equals(IO_VALUE.HIGH))
            {
                MessageBox.Show("请清空入库缓存上的料盘");
                return;
            }
            BoxPosition posiiton = CSVPositionReader<BoxPosition>.GetPositon(PosIDManger.APosList.DRAWER[1][0].PosID);
            InOutParam param = new InOutParam(new InOutPosInfo("InstoreSTEST", posiiton.PositionNum, posiiton.BagHigh, posiiton.BagWidth));
            param.ShelfType = 1;
            LogUtil.info("点击 CT测试");
            cttime = DateTime.Now;
            inputEquip.StartInstore(param);
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            lblMoveInfo.Text = inputEquip.GetMoveStr();
            lblMoveInfo2.Text = boxBean.GetMoveStr();
            ReadIOList();
        }

        private void CT_Load(object sender, EventArgs e)
        {
            for (int i = 1; i <= 13; i++)
            {
                // 创建按钮
                Button button = new Button();
                button.Text = "位置 " + i; // 设置按钮文本
                button.Tag = i; // 设置按钮的Tag属性
                button.Click += new EventHandler(Button_Click); // 为按钮添加点击事件
                button.Width = 100;
                button.Height = 40;

                // 将按钮添加到FlowLayoutPanel
                flowLayoutPanel1.Controls.Add(button);
            }

        }
        private void Button_Click(object sender, EventArgs e)
        {
            // 获取触发事件的按钮
            Button clickedButton = sender as Button;

            // 检查Tag属性并显示提示
            if (clickedButton != null && clickedButton.Tag is int tag)
            {
                btn_auto_Click(tag);
            }
        }

        private void btn_auto_Click(int drawerindex)
        {
            if (StoreManager.XLRStore.runStatus != RunStatus.Runing)
            {
                MessageBox.Show("请先启动设备");
                return;
            }
            if (StoreManager.XLRStore.boxEquip.IOValue(IO_Type.UpperArea_Check_A).Equals(IO_VALUE.HIGH)
                || StoreManager.XLRStore.boxEquip.IOValue(IO_Type.UpperArea_Check_B).Equals(IO_VALUE.HIGH))
            {
                MessageBox.Show("请清空出库缓存位上的料盘");
                return;
            }
            if (StoreManager.XLRStore.boxEquip.IOValue(IO_Type.UnderArea_Check_A).Equals(IO_VALUE.LOW)
                 || StoreManager.XLRStore.boxEquip.IOValue(IO_Type.UnderArea_Check_B).Equals(IO_VALUE.LOW))
            {
                MessageBox.Show("请在入库缓存位上放上料盘");
                return;
            }
            //int drawerindex = int.Parse(cb_poslist.SelectedItem.ToString());

            string apos = "", bpos = "", aout = "", bout = "";

            //if (drawerindex == 20)
            //{
            //    apos = "01AA15060420";
            //    bpos = "01BB14050401";
            //}
            //else
            PosIDManger.GetDarwerPoslist(drawerindex, out apos, out bpos, out aout, out bout);
            if (string.IsNullOrEmpty(apos))
            {
                MessageBox.Show($"位置{drawerindex} A侧没有可用库位");
                return;
            }
            if (string.IsNullOrEmpty(bpos))
            {
                MessageBox.Show($"位置{drawerindex} B侧没有可用库位");
                return;
            }
            if (PosIDManger.GetPosHasReel(apos))
            {
                MessageBox.Show($"库位{apos} 已有物料,不能入库");
                return;
            }
            if (PosIDManger.GetPosHasReel(bpos))
            {
                MessageBox.Show($"库位{bpos} 已有物料,不能入库");
                return;
            }
            LogUtil.info($"工作库位: apos:{apos}, bpos:{bpos}, aout:{aout}, bout:{bout}");
            InOutParam inp;
            if (apos.IndexOf("AA") > 0)
            {
                BufferDataManager.AInStoreInfo = new InOutPosInfo("STEST", apos, 7, 8);
                BufferDataManager.BInStoreInfo = new InOutPosInfo("STEST", bpos, 7, 8);
                inp = new InOutParam() { PosInfo = BufferDataManager.AInStoreInfo, PosInfoBack = BufferDataManager.BInStoreInfo };
                if (!string.IsNullOrEmpty(aout))
                    inp.AOutPosInfo = new InOutPosInfo("STEST", aout, 7, 8);
                if (!string.IsNullOrEmpty(bout))
                    inp.BOutPosInfo = new InOutPosInfo("STEST", bout, 7, 8);
            }
            else
            {
                BufferDataManager.AInStoreInfo = new InOutPosInfo("STEST", bpos, 7, 8);
                BufferDataManager.BInStoreInfo = new InOutPosInfo("STEST", apos, 7, 8);
                inp = new InOutParam() { PosInfo = BufferDataManager.BInStoreInfo, PosInfoBack = BufferDataManager.AInStoreInfo };
                if (!string.IsNullOrEmpty(bout))
                    inp.AOutPosInfo = new InOutPosInfo("STEST", bout, 7, 8);
                if (!string.IsNullOrEmpty(aout))
                    inp.BOutPosInfo = new InOutPosInfo("STEST", aout, 7, 8);
            }

            if (!StoreManager.XLRStore.boxEquip.StartInstore(inp))
            {
                MessageBox.Show("启动失败");
                return;
            }

            //LogUtil.info("手动入库测试...");
            //string apos = PosIDManger.APosList.DRAWER[selindex][0].PosID;
            //string aout = PosIDManger.APosList.DRAWER[selindex][1].PosID;
            //string bpos = PosIDManger.BPosList.DRAWER[selindex][0].PosID;
            //string bout = PosIDManger.BPosList.DRAWER[selindex][1].PosID;

            //LogUtil.info($"工作库位: apos:{apos}, bpos:{bpos}, aout:{aout}, bout:{bout}");

            //BufferDataManager.AInStoreInfo = new InOutPosInfo("STEST", apos, 7, 8);
            //BufferDataManager.BInStoreInfo = new InOutPosInfo("STEST", bpos, 7, 8);
            //var inp = new InOutParam() { PosInfo = BufferDataManager.AInStoreInfo, PosInfoBack = BufferDataManager.BInStoreInfo };

            //if (!string.IsNullOrEmpty(aout))
            //    inp.AOutPosInfo = new InOutPosInfo("STEST", aout, 7, 8);
            //if (!string.IsNullOrEmpty(bout))
            //    inp.BOutPosInfo = new InOutPosInfo("STEST", bout, 7, 8);

            //if (!StoreManager.XLRStore.boxEquip.StartInstore(inp))
            //{
            //    MessageBox.Show("启动失败");
            //    return;
            //}
            //else
            //{
            //    StoreManager.XLRStore.boxEquip.MoveInfo.NextMoveStep(StepEnum.SO_15_ToBufferArea);
            //    LogUtil.info($"直接前往缓存放料");
            //}
        }

        private void chbMoveStop_CheckedChanged(object sender, EventArgs e)
        {
            boxBean.MoveStop = chbMoveStop.Checked;
            inputEquip.MoveStop = chbMoveStop.Checked;
            LogUtil.info(inputEquip.Name + "用户切换是否暂停: " + inputEquip.MoveStop);
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            StoreManager.XLRStore.StopRun();
            LogUtil.info("手动停止");
        }

        private void lbl_t1_Click(object sender, EventArgs e)
        {

        }

        private void btn_ttin_Click(object sender, EventArgs e)
        {
            if (StoreManager.XLRStore.runStatus != RunStatus.Runing)
            {
                MessageBox.Show("请先启动设备");
                return;
            }
            if (StoreManager.XLRStore.boxEquip.IOValue(IO_Type.UnderArea_Check_A).Equals(IO_VALUE.LOW)
                 || StoreManager.XLRStore.boxEquip.IOValue(IO_Type.UnderArea_Check_B).Equals(IO_VALUE.LOW))
            {
                MessageBox.Show("请在入库缓存位上放上料盘");
                return;
            }
            //int drawerindex = int.Parse(cb_poslist.SelectedItem.ToString());

            string apos = "01AA15060420", bpos = "01BB14050401", aout = "", bout = "";

            if (PosIDManger.GetPosHasReel(apos))
            {
                MessageBox.Show($"库位{apos} 已有物料,不能入库");
                apos = "";
            }
            if (PosIDManger.GetPosHasReel(bpos))
            {
                MessageBox.Show($"库位{bpos} 已有物料,不能入库");
                bpos = "";
            }
            if (apos == "" && bpos == "")
                return;
            LogUtil.info($"工作库位: apos:{apos}, bpos:{bpos}, aout:{aout}, bout:{bout}");
            InOutParam inp;

            BufferDataManager.AInStoreInfo = apos == "" ? null : new InOutPosInfo("STEST", apos, 7, 8);
            BufferDataManager.BInStoreInfo = bpos == "" ? null : new InOutPosInfo("STEST", bpos, 7, 8);
            if (BufferDataManager.AInStoreInfo != null)
                inp = new InOutParam() { PosInfo = BufferDataManager.AInStoreInfo, PosInfoBack = BufferDataManager.BInStoreInfo };
            else
                inp = new InOutParam() { PosInfo = BufferDataManager.BInStoreInfo };


            if (!StoreManager.XLRStore.boxEquip.StartInstore(inp))
            {
                MessageBox.Show("启动失败");
                return;
            }
        }

        private void btn_ttout_Click(object sender, EventArgs e)
        {
            if (StoreManager.XLRStore.runStatus != RunStatus.Runing)
            {
                MessageBox.Show("请先启动设备");
                return;
            }
            if (StoreManager.XLRStore.boxEquip.IOValue(IO_Type.UpperArea_Check_A).Equals(IO_VALUE.HIGH)
                || StoreManager.XLRStore.boxEquip.IOValue(IO_Type.UpperArea_Check_B).Equals(IO_VALUE.HIGH))
            {
                MessageBox.Show("请清空出库缓存位上的料盘");
                return;
            }
            //int drawerindex = int.Parse(cb_poslist.SelectedItem.ToString());

            string apos = "", bpos = "", aout = "01AA15060420", bout = "01BB14050401";

            if (!PosIDManger.GetPosHasReel(aout))
            {
                MessageBox.Show($"库位{aout} 没有物料,不能出库");
                aout = "";
                //return;
            }
            if (!PosIDManger.GetPosHasReel(bout))
            {
                MessageBox.Show($"库位{bout} 没有物料,不能出库");
                bout = "";
                //return;
            }
            if (aout == "" && bout == "")
                return;

            LogUtil.info($"工作库位: apos:{apos}, bpos:{bpos}, aout:{aout}, bout:{bout}");
            InOutParam inp;

            BufferDataManager.AInStoreInfo = new InOutPosInfo("OUTSTEST", aout, 7, 8);
            BufferDataManager.BInStoreInfo = new InOutPosInfo("OUTSTEST", bout, 7, 8);
            inp = new InOutParam();
            if (!string.IsNullOrEmpty(aout))
            {
                inp.AOutPosInfo = new InOutPosInfo("STEST", aout, 7, 8);
                inp.PosInfo = BufferDataManager.AInStoreInfo;
            }            
            if (!string.IsNullOrEmpty(bout))
            {
                inp.BOutPosInfo = new InOutPosInfo("STEST", bout, 7, 8);
                inp.PosInfo = BufferDataManager.BInStoreInfo;
            }
            if (!string.IsNullOrEmpty(aout) && !string.IsNullOrEmpty(bout))
                inp = new InOutParam() { PosInfo = BufferDataManager.AInStoreInfo, PosInfoBack = BufferDataManager.BInStoreInfo };
            
            if (!StoreManager.XLRStore.boxEquip.StartInstore(inp))
            {
                MessageBox.Show("启动失败");
                return;
            }
        }
        protected Dictionary<string, IOTextControl> DIControlList = new Dictionary<string, IOTextControl>();
        protected void ReadIOList()
        {
            foreach (string key in DIControlList.Keys)
            {
                IOTextControl control = DIControlList[key];
                int iov = (int)IOManager.IOValue(key, inputEquip.DeviceID);
                if (iov != control.IOValue)
                {
                    control.IOValue = iov;
                    control.ShowData();
                }
            }
        }
        private void LoadIOList()
        {
            int maxCount = 20;
            int roleindex = 0;
            this.tableLayoutPanel1.RowStyles.Clear();
            this.tableLayoutPanel1.RowCount = maxCount;
            //this.tableLayoutPanel3.RowStyles.Clear();
            //this.tableLayoutPanel3.RowCount = maxCount;
            int i = 0;
            foreach (ConfigIO ioValue in inputEquip.Config.DIList.Values)
            {
                IOTextControl control = new IOTextControl(ioValue.ElectricalDefinition + "_" + ioValue.Explain, ioValue.ProName);
                {
                    // 为每个控件添加一个新的行样式
                    this.tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.Absolute, 28));

                    // 计算列索引,每两个控件换一行
                    int columnIndex = i % 2;

                    // 将控件添加到正确的列和行
                    this.tableLayoutPanel1.Controls.Add(control, columnIndex, i / 2);
                }
                roleindex++;
                i++;
                DIControlList.Add(ioValue.ProName, control);

            }

            this.SuspendLayout();    //此处为不闪屏,一定要有的! 

        }
    }
}