using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static AGVEmulator.DevAGV;


namespace AGVEmulator
{
    public partial class fMain
    {
        private void Agv_Command(object sender, commandargs e)
        {
            switch (e.Command.ToLower())
            {
                case "stopmark":
                    agvViewer1.StopbyMark = true;
                    break;
            }
        }
        private void Agv_StsValueChanged(object sender, DevAGV.StsValueChangedArgs e)
        {
            Console.WriteLine($"STS [{e.vtype}] VAL={e.Value}");
            if (this.InvokeRequired)
            {
                this.BeginInvoke(new EventHandler<StsValueChangedArgs>(Agv_StsValueChanged), sender, e);
                return;
            }
            switch (e.vtype)
            {
                case DevAGV.estsvaluetype.direction:
                    foreach (RadioButton c in this.groupBox5.Controls)
                    {
                        if (c.Text[0].Equals(e.Value))
                        {
                            c.Checked = true;

                        }
                        else
                        {
                            c.Checked = false;
                        }
                        c.Refresh();
                    }
                    groupBox5.Refresh();
                    break;
                case DevAGV.estsvaluetype.bunki:
                    foreach (RadioButton c in this.groupBox2.Controls)
                    {
                        if (c.Text[0].Equals(e.Value))
                        {
                            c.Checked = true;
                            c.Refresh();
                            break;
                        }
                    }
                    break;
                case DevAGV.estsvaluetype.speed:
                    foreach (RadioButton c in this.groupBox4.Controls)
                    {
                        if (c.Text[0].Equals(e.Value))
                        {
                            c.Checked = true;
                            c.Refresh();
                            break;
                        }
                    }
                    break;
                case DevAGV.estsvaluetype.sensor:
                    foreach (RadioButton c in this.groupBox8.Controls)
                    {
                        if (c.Text.Equals(e.Value))
                        {
                            c.Checked = true;
                            c.Refresh();
                            break;
                        }
                    }
                    break;
            }
            UpdateUIStatus();
        }

        private void Agv_ValueChanged(object sender, DevAGV.ValueChangedArgs e)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new EventHandler<ValueChangedArgs>(Agv_ValueChanged), sender, e);
                return;
            }

            //내부값이 바뀌었다면 컨트롤을 변경해준다.
            switch (e.vtype)
            {

                case DevAGV.evaluetype.system0:
                    foreach (CheckBox c in panel6.Controls)
                    {
                        var idx = int.Parse(c.Tag.ToString());
                        if (idx == e.Idx)
                        {
                            c.Checked = e.Value;
                            break;
                        }
                    }
                    break;
                case DevAGV.evaluetype.system1:

                    var v = (DevAGV.esystemflag1)e.Idx;
                    if (e.Value)
                    {
                        if (v == esystemflag1.agv_run)
                        {
                            agvViewer1.wat.Restart();
                        }
                        if (v == esystemflag1.agv_stop)
                        {
                            agvViewer1.wat.Stop();
                        }
                    }


                    foreach (CheckBox c in panel7.Controls)
                    {
                        var idx = int.Parse(c.Tag.ToString());
                        if (idx == e.Idx)
                        {
                            c.Checked = e.Value;
                            break;
                        }
                    }
                    break;
                case DevAGV.evaluetype.error:
                    foreach (CheckBox c in panel9.Controls)
                    {
                        var idx = int.Parse(c.Tag.ToString());
                        if (idx == e.Idx)
                        {
                            c.Checked = e.Value;
                            break;
                        }
                    }
                    break;
                case DevAGV.evaluetype.signal1:
                    foreach (CheckBox c in panel8.Controls)
                    {
                        var idx = int.Parse(c.Tag.ToString());
                        if (idx == e.Idx)
                        {
                            c.Checked = e.Value;
                            break;
                        }
                    }
                    break;
                case DevAGV.evaluetype.signal2:
                    foreach (CheckBox c in panel2.Controls)
                    {
                        var idx = int.Parse(c.Tag.ToString());
                        if (idx == e.Idx)
                        {
                            c.Checked = e.Value;
                            break;
                        }
                    }
                    break;

            }
        }

        private void Agv_RequestStatusData(object sender, DevAGV.RequestStatusDataArgs e)
        {
            //UInt16 system0 = 0xFFFF;
            //UInt16 system1 = 0xFFFF;
            //UInt16 error = 0xFFFF;
            //byte signal = 0xFF;

            aaplycheckboxbit(ref AGV.system0, panel6);
            aaplycheckboxbit(ref AGV.system1, panel7);
            aaplycheckboxbit(ref AGV.error, panel9);
            aaplycheckboxbit(ref AGV.signal1, panel8);
            aaplycheckboxbit(ref AGV.signal2, panel2);

            if (this.agvViewer1.StopbyMark) AGV.sts_speed = 'S';
            else AGV.sts_speed = GetGroupItemCheckbox(groupBox4);
            AGV.sts_bunki = GetGroupItemCheckbox(groupBox2);
            AGV.sts_dir = GetGroupItemCheckbox(groupBox5);
            AGV.sts_sensor = GetGroupItemCheckbox(groupBox8);

            //this.Invoke(new Action(() =>
            //{
            //    e.system0 = system0;
            //    e.system1 = system1;
            //    e.error = error;
            //    e.signal = signal;
            //    e.speed = GetGroupItemCheckbox(groupBox4);
            //    e.bunki = GetGroupItemCheckbox(groupBox2);
            //    e.direction = GetGroupItemCheckbox(groupBox5);
            //    e.sensor = GetGroupItemCheckbox(groupBox8);

            //    e.volt = 23.4f;
            //}));
        }
        private void AGV_Message(object sender, AR.Dev.RS232.MessageEventArgs e)
        {
            var dev = sender as AR.Dev.RS232;
            if (dev is DevBMS)
            {
                logBMS.Add(e.Message);
            }
            else if (dev is DevAGV)
            {
                logAGV.Add(e.Message);
            }
            else if (dev is DevXBE)
            {
                if (e.MsgType == AR.Dev.RS232.MessageType.Send)
                    logCAL.Add(">> " + e.Data.HexString());
                else if (e.MsgType == AR.Dev.RS232.MessageType.Recv)
                    logCAL.Add("<< " + e.Data.HexString());
                else logCAL.Add(e.Message);
            }
        }
    }
}
