using AGVControl.Models;
using AR;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Diagnostics.Tracing;

namespace AGVControl
{
    public class RFIDConnection
    {
        /// <summary>
        /// 시작지점
        /// </summary>
        public RFIDPoint P1 { get; set; }

        /// <summary>
        /// 종료지점
        /// </summary>
        public RFIDPoint P2 { get; set; }

        /// <summary>
        /// 도로의 사용여부
        /// </summary>
        public bool EnableP { get; set; }
        public bool EnableN { get; set; }

        /// <summary>
        /// AGV의 이동방향(리프트방향)
        /// 목적지 방향과의 일치를 위해 해당 방향을 설정할 수 있따
        /// </summary>
        public AgvDir? LiftDirectionP { get; set; }
        public AgvDir? LiftDirectionN { get; set; }

        /// <summary>
        /// AGV이동시 속도 (high, middle, low)
        /// </summary>
        public AgvSpeed? MoveSpeedP { get; set; }
        public AgvSpeed? MoveSpeedN { get; set; }

        /// <summary>
        /// AGV이동시 방향모드(좌분기, 전진, 우분기)
        /// </summary>
        public AgvSts? MoveDirectionP { get; set; }
        public AgvSts? MoveDirectionN { get; set; }

        /// <summary>
        /// 파일저장 및 불러오기시 사용하는 문자열로 반환
        /// </summary>
        [Browsable(false)]
        public string DataFileString
        {
            get
            {
                var str_enbP = EnableP ? "1" : "0";
                var str_liftP = "";
                if (LiftDirectionP != null) str_liftP = ((int)LiftDirectionP).ToString();

                var str_movespeedP = "";
                if (MoveSpeedP != null) str_movespeedP = ((int)MoveSpeedP).ToString();

                var str_movdirP = "";
                if (MoveDirectionP != null) str_movdirP = ((int)MoveDirectionP).ToString();

                var str_enbN = EnableP ? "1" : "0";
                var str_liftN = "";
                if (LiftDirectionN != null) str_liftN = ((int)LiftDirectionN).ToString();

                var str_movespeedN = "";
                if (MoveSpeedN != null) str_movespeedN = ((int)MoveSpeedN).ToString();

                var str_movdirN = "";
                if (MoveDirectionN != null) str_movdirN = ((int)MoveDirectionN).ToString();


                var PStr= $"{P1.Location.X},{P1.Location.Y},{P2.Location.X},{P2.Location.Y}," +   //location
                 $"{P1.Value},{P2.Value}," +   //rfid values
                 $"{str_enbP};{str_liftP};{str_movespeedP};{str_movdirP}," +
                 $"{str_enbN};{str_liftN};{str_movespeedN};{str_movdirN}";


                return $"{PStr}";


            }
            set
            {
                var buf = value.Split(',');
                if (buf.Length >= 2)
                {
                    var p1x = int.Parse(buf[0]);
                    var p1y = int.Parse(buf[1]);
                    var p2x = int.Parse(buf[2]);
                    var p2y = int.Parse(buf[3]);
                    var p1v = ushort.Parse(buf[4]);
                    var p2v = ushort.Parse(buf[5]);

                    if (P1 == null) P1 = new RFIDPoint();
                    P1.Location = new System.Drawing.Point(p1x, p1y);
                    P1.Value = p1v;

                    if (P2 == null) P2 = new RFIDPoint();
                    P2.Location = new System.Drawing.Point(p2x, p2y);
                    P2.Value = p2v;


                    if (buf[6].Contains(";"))   //양방향 정보
                    {
                        var StrP = buf[6].Split(';');
                        var StrN = buf[7].Split(';');

                        //Positive
                        this.EnableP = StrP[0] == "1";

                        if (StrP[1].isEmpty()) LiftDirectionP = null;
                        else LiftDirectionP = (AgvDir)int.Parse(StrP[1]);

                        if (StrP[2].isEmpty()) MoveSpeedP = null;
                        else MoveSpeedP = (AgvSpeed)int.Parse(StrP[2]);

                        if (StrP[3].isEmpty()) MoveDirectionP = null;
                        else MoveDirectionP = (AgvSts)int.Parse(StrP[3]);

                        //Negative
                        this.EnableN = StrN[0] == "1";

                        if (StrN[1].isEmpty()) LiftDirectionN = null;
                        else LiftDirectionN = (AgvDir)int.Parse(StrN[1]);

                        if (StrN[2].isEmpty()) MoveSpeedN = null;
                        else MoveSpeedN = (AgvSpeed)int.Parse(StrN[2]);

                        if (StrN[3].isEmpty()) MoveDirectionN = null;
                        else MoveDirectionN = (AgvSts)int.Parse(StrN[3]);
                    }
                    else
                    {
                        this.EnableP = buf[6] == "1";
                        
                        if (buf[7].isEmpty()) LiftDirectionP = null;
                        else LiftDirectionP = (AgvDir)int.Parse(buf[7]);

                        if (buf[8].isEmpty()) MoveSpeedP = null;
                        else MoveSpeedP = (AgvSpeed)int.Parse(buf[8]);

                        if (buf[9].isEmpty()) MoveDirectionP = null;
                        else MoveDirectionP = (AgvSts)int.Parse(buf[9]);

                        this.EnableN = this.EnableP;
                        this.LiftDirectionN = this.LiftDirectionP;
                        this.MoveSpeedN = this.MoveSpeedP;
                        this.MoveDirectionN = this.MoveDirectionP;
                    }

                    

                

                }
            }
        }

        public RFIDConnection(string dataline = "")
        {
            P1 = null;
            P2 = null;
            LiftDirectionP = null;
            MoveSpeedP = null;
            MoveDirectionP = null;
            EnableP = false;

            LiftDirectionN = null;
            MoveSpeedN = null;
            MoveDirectionP = null;
            EnableP = false;


            if (dataline.isEmpty() == false) DataFileString = dataline;
        }

        /// <summary>
        /// 값이 설정되어있는지
        /// </summary>
        public bool HasValue
        {
            get
            {
                if (P1 == null || P2 == null) return false;
                if (LiftDirectionP == null && MoveSpeedP == null && MoveDirectionP == null &&
                    LiftDirectionN == null && MoveSpeedN == null && MoveDirectionN == null) return false;
                return true;
            }
        }


        public override bool Equals(object obj)
        {
            if (obj is RFIDConnection other)
            {
                return (P1 == other.P1 && P2 == other.P2);
            }
            return false;
        }

        public override int GetHashCode()
        {
            return P1.GetHashCode() ^ P2.GetHashCode();
        }
        public override string ToString()
        {
            //연결정보를 확인
            return $"{P1.Value} ↔ {P2.Value},ENB:{(EnableP ? "O" : "X")}|{(EnableN ? "O" : "X")}";
        }
    }
}
