using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using AR;
using arDev;
using COMM;

namespace Project
{
    public partial class fMain
    {

        private void XBE_ProtocReceived(object sender, ENIG.EEProtocol.DataEventArgs e)
        {
            //TODO : 기능 처리필요 (XBee 메세지 데이터처리)
            //PUB.CheckManualChargeMode() : 수동충전확인
            //VAR.BOOL[eVarBool.FLAG_AUTORUN]  : 자동실행
            //PUB.Speak("현재 위치는 QA 입니다.") : 음성출력

            //ACS 수신 데이터 처리(타 장비는 확인하지 않는다)
            if (e.ReceivedPacket.ID == 0)
            {
                var data = e.ReceivedPacket.Data;
                var dataStr = System.Text.Encoding.Default.GetString(data);
                var cmd = (ENIGProtocol.AGVCommandHE)e.ReceivedPacket.Command;
                var TargetID = 0;
                if (dataStr.Length >= 2)
                {
                    //대상디바이스
                    TargetID = Convert.ToByte(dataStr.Substring(0, 2), 16);

                    //데이터영역을 다시 설정
                    if (dataStr.Length > 2) dataStr = dataStr.Substring(2);
                    else dataStr = string.Empty;
                }
                else
                {
                    PUB.log.Add($"ACS 데이터에서 TARGET ID가 없습니다(data : first byte)");
                }


                if (PUB.setting.XBE_ID != TargetID)
                    return;

                switch (cmd)
                {

                    case ENIGProtocol.AGVCommandHE.SetCurrent: //Set Current Position

                        if (dataStr.Length == 6)
                        {
                            var targID = dataStr.Substring(0, 2);
                            var targstr = dataStr.Substring(2);

                            if (byte.TryParse(targID, out byte tID))
                            {
                                if (PUB.setting.XBE_ID == tID)
                                {
                                    if (ushort.TryParse(targstr, out ushort tagno))
                                    {
                                        if (PUB.mapctl.SetCurrentPosition(tagno) == true)
                                        {
                                            PUB.log.AddI($"Set Position:{tagno}");
                                        }
                                        else PUB.log.AddE($"Position Set Error:{tagno}");
                                    }
                                    else PUB.log.AddE($"Position Param(tagstr) Error:{dataStr}");
                                }
                                else PUB.log.AddI($"Another Target {tID}:{PUB.setting.XBE_ID}");
                            }
                            else PUB.log.AddE($"Position Param(targetid) Error:{dataStr}");

                        }
                        else PUB.log.AddE($"Position Param Error:{dataStr}");

                        break;
                    case ENIGProtocol.AGVCommandHE.Goto:  //move to tag
                        if (uint.TryParse(dataStr, out uint tagno2))
                        {
                            var currPos = PUB.mapctl.Manager.agv.CurrentRFID;///.AGVMoveToRFID(;
                            if (PUB.mapctl.SetTargetPosition(tagno2))
                                PUB.log.AddI($"New Target {tagno2}");
                            else
                                PUB.log.AddE($"Path Error {tagno2}");
                        }
                        else PUB.log.AddE($"Path Param Error :{dataStr}");
                        break;
                    case ENIGProtocol.AGVCommandHE.Stop:  //stop
                        PUB.AGV.AGVMoveStop("xbee");
                        break;
                    case ENIGProtocol.AGVCommandHE.Reset: //Error Reset
                        PUB.AGV.AGVErrorReset();
                        break;

                    case ENIGProtocol.AGVCommandHE.Manual: //Manual Move (Direction, speed, runtime)
                        var Direction = data[0];     //0=back, 1=forward, 2=left, 3=right
                        var Speed = data[1];         //0=slow, 1=normal, 2=fast
                        var Runtime = data[2];       // running seconds

                        arDev.Narumi.ManulOpt opt = arDev.Narumi.ManulOpt.BS;
                        arDev.Narumi.Speed spd = arDev.Narumi.Speed.Low;
                        if (Speed == 1) spd = arDev.Narumi.Speed.Mid;
                        else if (Speed == 2) spd = arDev.Narumi.Speed.High;

                        //0.자동모드가 아니라면 실행하지 않는다
                        //1.입력된 파라미터로 AGV를 이동한다
                        if (Direction == 0) opt = arDev.Narumi.ManulOpt.BS;
                        else if (Direction == 1) opt = arDev.Narumi.ManulOpt.FS;
                        else if (Direction == 2) opt = arDev.Narumi.ManulOpt.LT;
                        else if (Direction == 3) opt = arDev.Narumi.ManulOpt.RT;

                        PUB.AGV.AGVMoveManual(opt, spd, arDev.Narumi.Sensor.PBSOn);
                        break;
                    case ENIGProtocol.AGVCommandHE.MarkStop: //Set MarkStop
                        var MarkStop = data[0];    //0=off, 1=on

                        //마크센서에서 멈추게 한다
                        PUB.AGV.AGVMoveStop("Xbee", arDev.Narumi.eStopOpt.MarkStop);
                        break;
                    case ENIGProtocol.AGVCommandHE.LiftControl: //Lift Control
                        var LiftCommand = data[0];    //0=stop, 1=up, 2=down
                        arDev.Narumi.LiftCommand LCmd = arDev.Narumi.LiftCommand.STP;
                        if (LiftCommand == 1) LCmd = arDev.Narumi.LiftCommand.UP;
                        else if (LiftCommand == 2) LCmd = arDev.Narumi.LiftCommand.DN;

                        //리프트제어
                        PUB.AGV.LiftControl(LCmd);
                        break;




                }
            }
        }

        private void XBE_MessageReceived(object sender, Device.Xbee.MessageArgs e)
        {
            if (e.IsError) PUB.log.AddE(e.Message);
            else PUB.log.Add(e.Message);
        }


    }
}

