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 COMM;

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


            this.FormClosed += FIO_FormClosed;
        }

        private void fFlag_Load(object sender, EventArgs e)
        {
            this.timer1.Start();
        }

        private void FIO_FormClosed(object sender, FormClosedEventArgs e)
        {
            this.timer1.Stop();
        }


        private void timer1_Tick(object sender, EventArgs e)
        {
            timer1.Stop();
            lbIP.Text = PUB.IP;
            label1.Text = PUB.AGV.LastSTS;
            richTextBox1.Text = PUB.AGV.system0.ToString();
            richTextBox2.Text = PUB.AGV.system1.ToString();
            richTextBox3.Text = PUB.AGV.signal.ToString() + "\n" + PUB.AGV.data.ToString();
            richTextBox4.Text = PUB.AGV.error.ToString();
            timer1.Start();
        }

        private void fAgv_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.AGVErrorReset();
            if(PUB.sm.Step == StateMachine.eSMStep.RUN)
            PUB.sm.SetNewStep(StateMachine.eSMStep.IDLE);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            PUB.log.Add($"사용자 충전 명령 전송 : {PUB.setting.ChargerID}");
            PUB.AGV.AGVCharge(PUB.setting.ChargerID, true);
            if (PUB.sm.Step == StateMachine.eSMStep.RUN)
                PUB.sm.SetNewStep(StateMachine.eSMStep.IDLE);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            PUB.AGV.AGVCharge(PUB.setting.ChargerID, false);
            if (PUB.sm.Step == StateMachine.eSMStep.RUN)
                PUB.sm.SetNewStep(StateMachine.eSMStep.IDLE);
        }

        private void button4_Click(object sender, EventArgs e)
        {
            PUB.AGV.AGVMoveStop("userbutton page:fagv button:button4");
            if (PUB.sm.Step == StateMachine.eSMStep.RUN)
                PUB.sm.SetNewStep(StateMachine.eSMStep.IDLE);
        }

        private void button5_Click(object sender, EventArgs e)
        {
            PUB.AGV.AGVMoveStop("user button clic fagv",arDev.Narumi.eStopOpt.MarkStop);
            if (PUB.sm.Step == StateMachine.eSMStep.RUN)
                PUB.sm.SetNewStep(StateMachine.eSMStep.IDLE);
        }

        private void button7_Click(object sender, EventArgs e)
        {
            PUB.AGV.AGVMoveManual(arDev.Narumi.ManulOpt.FS, arDev.Narumi.Speed.Low, arDev.Narumi.Sensor.AllOn);
            if (PUB.sm.Step == StateMachine.eSMStep.RUN)
                PUB.sm.SetNewStep(StateMachine.eSMStep.IDLE);
        }

        private void button6_Click(object sender, EventArgs e)
        {
            PUB.AGV.AGVMoveManual(arDev.Narumi.ManulOpt.BS, arDev.Narumi.Speed.Low, arDev.Narumi.Sensor.AllOn);
            if (PUB.sm.Step == StateMachine.eSMStep.RUN)
                PUB.sm.SetNewStep(StateMachine.eSMStep.IDLE);
        }

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