using System;
using System.ComponentModel;
using DDUtilityApp.DATA;
using JWH;
using JWH.DATA;

namespace DDUtilityApp.LOGPARSER.DATA
{

    /// <summary>
    /// EIS 설비 정보
    /// </summary>
    public class EisEquipment : DataTableBase
    {

        /// <summary>LogServer Information</summary>
        public LogServer Server { get; set; }

        /// <summary>MES.FacilityName</summary>
        public string Facility { get; set; }

        /// <summary>MES.FacilityName</summary>
        public string OrderByKey { get { return string.IsNullOrEmpty(this.Facility) ? "" : this.Facility.Substring(0, 2); } }

        /// <summary>EIS.Line</summary>
        public string Line { get; set; }

        /// <summary>MES.ProcessSegmentID</summary>
        public string ProcessSegmentID { get; set; }

        /// <summary>MES.ProcessSegmentName</summary>
        public string ProcessSegmentName { get; set; }

        /// <summary>EIS.ModelID</summary>
        public string ModelID { get; set; }

        /// <summary>MES.Maker</summary>
        public string Maker { get; set; }

        /// <summary>EIS.ModelVersion (From EIS.EquipmentModelDetails)</summary>
        public string ModelVersion { get; set; }

        /// <summary>EIS.Version</summary>
        public string Version { get; set; }

        /// <summary>EIS.RunningVersion</summary>
        public string RunningVersion { get; set; }

        /// <summary>Empty</summary>
        public string CusLibVersion { get; set; }

        /// <summary>EIS.EquipmentID</summary>
        public string EquipmentID { get; set; }

        /// <summary>Name is Select(MES, EIS)</summary>
        [ReadOnly(true)]
        public string DisplayName
        {
            get
            {
                string value = string.Empty;
                foreach(string name in  this.DisplayNameOrder.Split(';'))
                {
                    value = this.PropertyGet(name);
                    if (!string.IsNullOrEmpty(value)) break;
                }
                return value;
            }
        }

        /// <summary>EIS.Description</summary>
        public string Description { get; set; }

        /// <summary>EIS.GemSettingID (From DriverParameter)</summary>
        public string GemSettingID { get; set; }

        /// <summary>EIS.DriverFileName (From DriverParameter)</summary>
        public string DriverFileName { get; set; }

        /// <summary>EIS.EquipmentIP (From DriverParameter)</summary>
        public string EquipmentIP { get; set; }

        /// <summary>EIS.Port (From DriverParameter)</summary>
        public int Port { get; set; }

        /// <summary>Empty</summary>
        public string ServerName { get; set; }

        /// <summary>EIS.ServerIP</summary>
        public string ServerIP { get; set; }

        /// <summary>EIS.OriginServerIP</summary>
        public string OriginServerIP { get; set; }

        /// <summary>LogServerIP</summary>
        public string LogServerIP { get; set; }

        private string m_LogPath = string.Empty;

        /// <summary>EIS.LogPath</summary>
        public string LogPath
        {
            get { return this.m_LogPath; }
            set { this.m_LogPath = this.SetLogPath(value); } 
        }

        /// <summary>MES.Description</summary>
        public string MesName { get; set; }

        /// <summary>MES.OperationMode</summary>
        public string OperationMode { get; set; }

        private string m_ControlMode = string.Empty;

        /// <summary>MES.ControlMode</summary>
        public string ControlMode
        {
            get { return this.m_ControlMode; }
            set { this.m_ControlMode = value.ToTitleCase(); } 
        }

        private string m_EquipmentState = string.Empty;

        /// <summary>MES.EqpState</summary>
        public string State
        {
            get { return this.m_EquipmentState; }
            set { this.m_EquipmentState = value.ToTitleCase(); } 
        }

        /// <summary>MES.LastTrackInLotID</summary>
        public string LastTrackInLotID { get; set; }

        /// <summary>MES.LastTrackOutLotID</summary>
        public string LastTrackOutLotID { get; set; }

        /// <summary>DisplayName OrderBy</summary>
        public string DisplayNameOrder { get; set; } = "MesName;Description;";

        /// <summary>MES 기준등록 존재여부</summary>
        public bool MesRegistration { get; set; } = true;

        /// <summary>MES Daemon</summary>
        public string MesDaemon { get; set; }

        /// <summary>MES Service</summary>
        public string MesService { get; set; }

        /// <summary>MES Subject</summary>
        public string MesSubject { get; set; }

        /// <summary>PLC_TYPE</summary>
        public string PlcType { get; set; }

        /// <summary>PM Date</summary>
        public DateTime PMDate { get; set; }

        private string SetLogPath(string value)
        {
            try
            {
                if (value.StartsWith(@"\"))
                {
                    string[] values = value.Split(new string[] { @"\" }, StringSplitOptions.RemoveEmptyEntries);
                    this.LogServerIP = values[0];
                }
                else
                {
                    if (value.StartsWith("X:"))
                        this.LogServerIP = "192.168.7.150";
                    else 
                        this.LogServerIP = this.ServerIP;
                }

                return value;
            }
            catch
            {
                return value;
            }
        }

    }

}
