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

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;

            rtSystem0.Rtf = PUB.AGV.system0.ToRtfString();
            rtSystem1.Rtf = PUB.AGV.system1.ToRtfString();
            rtSignal1.Rtf = PUB.AGV.signal1.ToRtfString();
            rtSignal2.Rtf = PUB.AGV.signal2.ToRtfString();
            rtData.Rtf = PUB.AGV.data.ToRtfString();
            rtError.Rtf = PUB.AGV.error.ToRtfString();
            lbPortName.Text = $"AGV:{PUB.setting.Port_AGV}\nBMS:{PUB.setting.Port_BAT}";
            timer1.Start();
        }

        private string CombineRtfStrings(string rtf1, string rtf2)
        {
            // RTF 문자열 합치기: 첫 번째 RTF의 닫는 } 제거하고 두 번째 RTF의 헤더 제거
            var rtf1Body = rtf1.Replace("}", "").Trim();
            var rtf2Lines = rtf2.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);

            var sb = new System.Text.StringBuilder();
            sb.Append(rtf1Body);

            // 두 번째 RTF의 헤더 부분 건너뛰기
            bool skipHeader = true;
            foreach (var line in rtf2Lines)
            {
                if (line.Contains(@"\line") || (!line.StartsWith(@"{\rtf") && !line.StartsWith(@"{\colortbl") && !line.Contains("}")))
                {
                    skipHeader = false;
                }

                if (!skipHeader && !line.Trim().Equals("}"))
                {
                    sb.AppendLine(line);
                }
            }

            sb.AppendLine("}");
            return sb.ToString();
        }

        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( arDev.Narumi.eRunOpt.Forward);
        }

        private void button9_Click(object sender, EventArgs e)
        {
            var inputbox = AR.UTIL.InputBox("input value", "1000");
            if (inputbox.Item1 == false) return;
            if (UInt16.TryParse(inputbox.Item2, out UInt16 value) == false) return;
            PUB.AGV.TurnGDSCenterScope(value); 
            if (PUB.sm.Step == StateMachine.eSMStep.RUN)
                PUB.sm.SetNewStep(StateMachine.eSMStep.IDLE);
        }

        private void button16_Click(object sender, EventArgs e)
        {
            var inputbox = AR.UTIL.InputBox("input value","100");
            if (inputbox.Item1 == false) return;
            if (int.TryParse(inputbox.Item2, out int value) == false) return;
            PUB.AGV.SetBackturnTime(value);
            if (PUB.sm.Step == StateMachine.eSMStep.RUN)
                PUB.sm.SetNewStep(StateMachine.eSMStep.IDLE);
        }

        private void button10_Click(object sender, EventArgs e)
        {
            var inputbox = AR.UTIL.InputBox("input value","50");
            if (inputbox.Item1 == false) return;
            if (int.TryParse(inputbox.Item2, out int value) == false) return;
            PUB.AGV.SetGateOutOffTime(value);
            if (PUB.sm.Step == StateMachine.eSMStep.RUN)
                PUB.sm.SetNewStep(StateMachine.eSMStep.IDLE);
        }

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

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

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

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

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

        private void button17_Click(object sender, EventArgs e)
        {
            PUB.AGV.AGVMoveRun(arDev.Narumi.eRunOpt.Backward);
        }

        private void button18_Click(object sender, EventArgs e)
        {
            //턴정보 hw sw 를 초기화한다.
            PUB.AGV.TurnInformation.State = arDev.eNarumiTurn.None;
            PUB._virtualAGV.Turn = AGVNavigationCore.Models.AGVTurn.None;
            PUB.log.AddAT($"[fAGV] 턴 정보를 초기화 합니다");
        }
    }
}
