using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using Project.StateMachine;
using AR;

namespace Project
{
    public class CResult
    {

        public bool SMSG_ProgressEnable = false;
        //public string SMSG_Text = string.Empty;
        public float SMSG_ProgressMax = 0;
        public int SMSG_ProgressMin = 0;
        public Color SMSG_ProgressForeColor = Color.Red;
        public Color SMSG_ForeColor = Color.Black;
        public Color SMSG_BackColor = Color.White;
        public Color SMSG_BackColor2 = Color.White;
        public Color SMSG_ShadowColor = Color.Transparent;
        public float SMSG_ProgressValue = 0;
        public string SMSG_Tag = string.Empty;
        /// <summary>
        /// 이동대상위치(상차,하차,충전)
        /// </summary>
        private ePosition _targetPos = ePosition.NONE;
        public string result_message = "";
        public double result_progressmax = 0;
        public double result_progressvalue = 0;
        public DateTime StopMessageTimePLC = DateTime.Parse("1982-11-23");
        public DateTime StopMessageTimeSWR = DateTime.Parse("1982-11-23");
        public string StopMessagePLC = string.Empty;
        public string StopMessageSWR = string.Empty;
        private ePosition _currentpos = ePosition.NONE;
        private string _currentposcw = string.Empty;
        public ushort LastTAG { get; set; } = 0;
        public ePosition NextPos = ePosition.NONE;
        private char _comandKit { get; set; }
        public string Memo;
        public eResult ResultCode { get; set; }
        public eECode ResultErrorCode;
        public ENIGProtocol.AGVErrorCode RunStepErrorCode { get; set; }
        public string ResultMessage { get; set; }
        public int retry = 0;
        public DateTime retryTime;
        public Device.Socket.Message RecvMessage;

        /// <summary>
        /// 작업시작시간
        /// </summary>
        public DateTime JobStartTime;

        /// <summary>
        /// 작업종료시간
        /// </summary>
        public DateTime JobEndTime;

        //public DateTime IdleStartTime = DateTime.Parse("1982-11-23");

        /// <summary>
        /// 작업진행시간
        /// </summary>
        public TimeSpan JobRunTime
        {
            get
            {
                if (JobStartTime.Year == 1982) return new TimeSpan(0);
                if (JobEndTime.Year == 1982) return DateTime.Now - JobStartTime;
                else return JobEndTime - JobStartTime;
            }
        }

        #region "AGV Status Value"
        public string PLC1_RawData { get; set; }
        public string PLC2_RawData { get; set; }


        #endregion

    
        public ePosition TargetPos
        {
            get
            {
                return _targetPos;
            }
            set
            {
                _targetPos = value;
                PUB.log.Add(string.Format("대상위치설정:{0}", value));
            }
        }

      
        public char CommandKit
        {
            get
            {
                return _comandKit;
            }
            set
            {
                _comandKit = value;
             
                PUB.log.Add(string.Format("명령을 내린 KIT 설정 No={0}", value));
            }
        }

    
        public ePosition CurrentPos
        {
            get
            {
                return _currentpos;            }
            set
            {
              
                if (_currentpos != value)   //값이 바뀔때만 메세지  220628
                    PUB.log.Add(string.Format("TAG위치 설정:{0}->{1}", _currentpos, value));
                _currentpos = value;
            }
        }
      
     
        public string CurrentPosCW
        {
            get
            {
             
                return _currentposcw;
            }
            set
            {
              
                if (_currentposcw != value) //값이바뀔때만 메세지  220628
                    PUB.log.Add(string.Format("TAG방향 설정:{0}->{1}", _currentposcw, value));
                _currentposcw = value;
            }
        }
    

        #region "SetResultMessage"

        public void SetResultMessage(eResult code, eECode err, eNextStep systempause, params object[] args)
        {

            var rltMsg = PUB.GetResultCodeMessage(code);
            var codeMSg = $"[E{(int)err}] ";// + Util.GetResultCodeMessage(code);
            if (err == eECode.MESSAGE_ERROR)
            {
                codeMSg = $"[{rltMsg} ERROR MESSAGE]\n";
            }
            else if (err == eECode.MESSAGE_INFO)
            {
                codeMSg = $"[{rltMsg} INFORMATION]\n";
            }



            var erMsg = PUB.GetErrorMessage(err, args);
            var msg = codeMSg + erMsg;

            this.ResultCode = code;
            this.ResultErrorCode = err;
            this.ResultMessage = msg;

            if (systempause == eNextStep.PAUSENOMESAGE) this.ResultMessage = string.Empty; //210129

            PUB.log.AddE(msg);
            if (systempause == eNextStep.PAUSE) PUB.sm.SetNewStep(eSMStep.PAUSE);
            else if (systempause == eNextStep.PAUSENOMESAGE) PUB.sm.SetNewStep(eSMStep.PAUSE);
            else if (systempause == eNextStep.ERROR) PUB.sm.SetNewStep(eSMStep.ERROR);
        }


        #endregion

   
        public CResult()
        {
            this.Clear();
        }


        public void Clear()
        {
            JobStartTime = DateTime.Parse("1982-11-23");
            JobEndTime = DateTime.Parse("1982-11-23");

            Memo = string.Empty;
            ResultCode = eResult.NoError;
            RunStepErrorCode = ENIGProtocol.AGVErrorCode.None;
            ResultMessage = string.Empty;
        }


    }

}
