using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Test_BMS
{
    public partial class Form1 : Form
    {
        arDev.Narumi dev;
        arUtil.Log log1 = new arUtil.Log();
        arUtil.Log log2 = new arUtil.Log();
        public Form1()
        {
            InitializeComponent();
            dev = new arDev.Narumi();
            dev.DataReceive += Dev_DataReceive;
            dev.Message += Dev_Message;
            log1.FileNameFormat = "{yyyyMMdd}_COM";
            log2.FileNameFormat = "{yyyyMMdd}_STS";
            this.FormClosed += Form1_FormClosed;
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            log1.Flush();
            log2.Flush();
        }

        private void Dev_Message(object sender, arDev.NarumiSerialComm.MessageEventArgs e)
        {
            addmsg(e.MsgType.ToString(), e.Message);// $"{e.MsgType}:{e.Message}");
        }

        private void Dev_DataReceive(object sender, arDev.Narumi.DataEventArgs e)
        {
            //throw new NotImplementedException();
            updatestatue();
        }

        void updatestatue()
        {
            if (this.rt0.InvokeRequired)
            {
                this.Invoke(new MethodInvoker(updatestatue));
                return;
            }

            this.rt0.Text = $"system0-{dev.system0.Value:X2}\n" + dev.system0.ToString();
            this.rt1.Text = $"system1-{dev.system1.Value:X2}\n" + dev.system1.ToString();
            this.rt2.Text = $"error-{dev.error.Value:X2}\n" + dev.error.ToString();
            this.rt3.Text = $"iosignal-{dev.signal1.Value:X2}\n" + dev.signal1.ToString() + "data\n" + dev.data.ToString();
           //this.rt4.Text = "data\n" + dev.data.ToString();


        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.comboBox1.Items.Clear();
            foreach (var port in System.IO.Ports.SerialPort.GetPortNames())
                this.comboBox1.Items.Add(port);
        }

        void addmsg(string grp,string m)
        {
            if(m.Contains("STS"))
            {
                log2.Add(grp, m);
                if (logTextBox2.InvokeRequired)
                {
                    logTextBox2.BeginInvoke(new Action(() =>
                    {
                        logTextBox2.AddMsg(m);
                    }));
                }
                else
                {
                    logTextBox2.AddMsg(m);
                }
            }
            else
            {
                log1.Add(grp, m);
                if (logTextBox1.InvokeRequired)
                {
                    logTextBox1.BeginInvoke(new Action(() =>
                    {
                        logTextBox1.AddMsg(m);
                    }));
                }
                else
                {
                    logTextBox1.AddMsg(m);
                }
            }
            
        }




        private void button1_Click(object sender, EventArgs e)
        {
            dev.BaudRate = int.Parse(this.comboBox2.Text);
            dev.PortName = comboBox1.Text;
            dev.Open();
            addmsg("conn",$"open:{dev.IsOpen}");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            dev.Close();
        }



        private void button6_Click(object sender, EventArgs e)
        {
            dev.AGVMoveSet(new arDev.Narumi.BunkiData
            {
                Bunki = arDev.Narumi.eBunki.Strate,
                Direction = arDev.Narumi.eMoveDir.Forward,
                PBSSensor = 1,
                Speed = arDev.Narumi.eMoveSpd.Low,
            });
        }

        private void button5_Click(object sender, EventArgs e)
        {
            dev.AGVMoveStop("form1-button5", arDev.Narumi.eStopOpt.Stop);
        }

        private void button7_Click(object sender, EventArgs e)
        {
            dev.AGVMoveStop("form1-button7",arDev.Narumi.eStopOpt.MarkStop);
        }

        private void button4_Click(object sender, EventArgs e)
        {
            dev.AGVMoveRun(arDev.Narumi.eRunOpt.Backward);
        }
        private void button3_Click(object sender, EventArgs e)
        {
            dev.AGVMoveRun(arDev.Narumi.eRunOpt.Forward);
        }

        private void button8_Click(object sender, EventArgs e)
        {
            dev.AGVMoveRun();
        }

        private void button9_Click(object sender, EventArgs e)
        {
            dev.AGVCharge(30, true);
        }

        private void button10_Click(object sender, EventArgs e)
        {
            dev.AGVCharge(30, false);
        }

        private void button11_Click(object sender, EventArgs e)
        {
            dev.AGVTowerLamp(true);
        }

        private void button12_Click(object sender, EventArgs e)
        {
            dev.AGVTowerLamp(false);
        }

        private void button13_Click(object sender, EventArgs e)
        {
            dev.AGVErrorReset();
        }

        private void button14_Click(object sender, EventArgs e)
        {
            dev.AGVMoveSet(new arDev.Narumi.BunkiData
            {
                Bunki = arDev.Narumi.eBunki.Strate,
                Direction = arDev.Narumi.eMoveDir.Backward,
                PBSSensor = 1,
                Speed = arDev.Narumi.eMoveSpd.Low,
            });
        }

        private void button15_Click(object sender, EventArgs e)
        {
            dev.AGVMoveManual(arDev.Narumi.ManulOpt.FS, arDev.Narumi.Speed.Low, arDev.Narumi.Sensor.AllOn);
        }

        private void button16_Click(object sender, EventArgs e)
        {
            dev.AGVMoveManual(arDev.Narumi.ManulOpt.BS, arDev.Narumi.Speed.Low, arDev.Narumi.Sensor.AllOn);
        }

        private void button17_Click(object sender, EventArgs e)
        {
            dev.AGVMoveSet(new arDev.Narumi.BunkiData(dev.data.Direction, dev.data.Sts, 'L'));
        }

        private void button18_Click(object sender, EventArgs e)
        {
            dev.AGVMoveSet(new arDev.Narumi.BunkiData(dev.data.Direction, dev.data.Sts, 'M'));
        }

        private void button19_Click(object sender, EventArgs e)
        {
            dev.AGVMoveSet(new arDev.Narumi.BunkiData(dev.data.Direction, dev.data.Sts, 'H'));
        }
    }
}
