using AGVNavigationCore.Models;
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 static arDev.Narumi;

namespace Project.ViewForm
{
    public partial class fIO : Form
    {
        public fIO()
        {
            InitializeComponent();


            //이름설정 INput #1
            List<string> namearray = new List<string>(32);
            List<string> tagarray = new List<string>(32);

            namearray.Clear();
            tagarray.Clear();
            //for (byte i = 0; i < 16; i++)
            //{
            //    var flagName = ((arDev.FakePLC.DIName)i).ToString();
            //    var enumValue = Enum.Parse(typeof(arDev.FakePLC.DIPin), flagName);
            //    if (flagName == i.ToString()) tagarray.Add(string.Empty);
            //    else
            //    {
            //        //동일한 이름의 pin 번호르 찾아서 할당해준다.
            //        var eV = (arDev.FakePLC.DIPin)enumValue;
            //        tagarray.Add(((byte)eV).ToString());
            //    }
            //    namearray.Add(flagName.Replace("_", "\n"));
            //}
            //for (byte i = 0; i < 8; i++)
            //{
            //    var flagName = ((arDev.FakePLC.DOName)i).ToString();
            //    var enumValue = Enum.Parse(typeof(arDev.FakePLC.DOPin), flagName);
            //    if (flagName == i.ToString()) tagarray.Add(string.Empty);
            //    else
            //    {
            //        //동일한 이름의 pin 번호르 찾아서 할당해준다.
            //        var eV = (arDev.FakePLC.DOPin)enumValue;
            //        tagarray.Add(((byte)eV).ToString());
            //    }
            //    namearray.Add(flagName);
            //}
        

            this.FormClosed += FIO_FormClosed;
            UpdateControl();
        }
        void Update_HWStatus()
        {
            var rownum = 1;
            var colIdx = 0;


            if (PUB.AGV.IsOpen == false) BlinkGridViewItem(HWState, rownum, colIdx++, 3, 4);
            else HWState.setValue(rownum, colIdx++, 2); // 

            if (PUB.XBE.IsOpen == false) BlinkGridViewItem(HWState, rownum, colIdx++, 3, 4);
            else HWState.setValue(rownum, colIdx++, 2); // 

            //if (PUB.PLC.IsOpen == false) BlinkGridViewItem(HWState, rownum, colIdx++, 3, 4);
            //else HWState.setValue(rownum, colIdx++, 2); // 

            if (PUB.BMS.IsOpen == false) BlinkGridViewItem(HWState, rownum, colIdx++, 3, 4);
            else HWState.setValue(rownum, colIdx++, 2); // 
        }
        void BlinkGridViewItem(arFrame.Control.GridView ctl, int row, int col, int value1, int value2)
        {
            var curValue = ctl.getValue(row, col);
            ctl.setValue(row, col, (byte)(curValue == value1 ? value2 : value1)); // 
        }

        void UpdateControl()
        {
            var rowNum = 1;
            var colIdx = 0;
            HWState.setTitle(0, colIdx++, "AGV");
            HWState.setTitle(0, colIdx++, "XBE");
            //HWState.setTitle(0, colIdx++, "PLC");
            HWState.setTitle(0, colIdx++, "BAT");

            colIdx = 0;
            HWState.setTitle(rowNum, colIdx++, PUB.setting.Port_AGV);
            HWState.setTitle(rowNum, colIdx++, PUB.setting.Port_XBE);
            //HWState.setTitle(rowNum, colIdx++, PUB.setting.Port_PLC);
            HWState.setTitle(rowNum, colIdx++, PUB.setting.Port_BAT);

            HWState.Invalidate();

        }
        private void fIO_Load(object sender, EventArgs e)
        {
            this.timer1.Start();
        }
        private void FIO_FormClosed(object sender, FormClosedEventArgs e)
        {
            //Pub.plcM.ValueChanged -= Plc1_ValueChanged;
            //Pub.plcS.ValueChanged -= Plc2_ValueChanged;
        }


     


        bool tmrun = false;
        private void timer1_Tick(object sender, EventArgs e)
        {
            if (this.Visible == false) return;
            if (tmrun == true) return;
            tmrun = true;

            Update_HWStatus();

            List<Boolean> fgValueM = new List<bool>();
            List<Boolean> fgValueS = new List<bool>();


            tmrun = false;
        }

        private void fIO_VisibleChanged(object sender, EventArgs e)
        {
            this.timer1.Enabled = this.Visible;
            if (timer1.Enabled) timer1.Start();
            else timer1.Stop();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            PUB.AGV.LiftControl(LiftCommand.UP);
        }

     
        private void button2_Click(object sender, EventArgs e)
        {
            PUB.AGV.LiftControl(LiftCommand.STP);
        }

        private void button3_Click_1(object sender, EventArgs e)
        {
            PUB.AGV.LiftControl(LiftCommand.DN);
        }

        private void button4_Click(object sender, EventArgs e)
        {
            PUB.AGV.LiftControl(LiftCommand.ON);
        }

        private void button5_Click(object sender, EventArgs e)
        {
            PUB.AGV.LiftControl(LiftCommand.OFF);
        }
    }
}
