using System;
using System.Runtime.Serialization;
using MECF.Framework.Common.CommonData;
using MECF.Framework.Common.Jobs;

namespace Aitex.Core.Common
{
    [Serializable]
    [DataContract]
    public class WaferInfoRt : NotifiableItem
    {
        #region WaferInfo

        public bool IsWaferEmpty => WaferStatus == WaferStatus.Empty;

        [DataMember]
        public string WaferID { get; set; }

        [DataMember]
        public Guid WaferInnerID { get; set; } = Guid.Empty;

        [DataMember]
        public string LotId { get; set; }

        [DataMember]
        public ProcessJobInfo ProcessJob { get; set; }

        [DataMember]
        public int NextSequenceStep { get; set; }

        [DataMember]
        public string WaferOriginStation { get; set; }

        [DataMember]
        public int WaferOriginSlot { get; set; }

        [DataMember]
        public string WaferOrigin { get; set; }

        [DataMember]
        public WaferSize Size { get; set; }

        [DataMember]
        public bool HasWarning { get; set; }

        [DataMember]
        public string PPID { get; set; }

        [DataMember]
        public string TransFlag { get; set; }


        private string processJobID;
        [DataMember]
        public string ProcessJobID
        {
            get => processJobID;
            set
            {
                processJobID = value;
                InvokePropertyChanged();
            }
        }

        private string controlJobID;
        [DataMember]
        public string ControlJobID
        {
            get => controlJobID;
            set
            {
                controlJobID = value;
                InvokePropertyChanged();
            }
        }

        private WaferStatus waferStatus = WaferStatus.Empty;
        [DataMember]
        public WaferStatus WaferStatus
        {
            get => waferStatus;
            set
            {
                waferStatus = value;
                InvokePropertyChanged();
            }
        }

        private bool isChecked;
        public bool IsChecked
        {
            get => isChecked;
            set
            {
                isChecked = value;
                InvokePropertyChanged();
            }
        }

        private WaferProcessStatus processState;
        [DataMember]
        public WaferProcessStatus ProcessState
        {
            get => processState;
            set
            {
                processState = value;
                InvokePropertyChanged();
            }
        }

        private SubstrateTransportStatus substtransstatus;
        [DataMember]
        public SubstrateTransportStatus SubstTransStatus
        {
            get => substtransstatus;
            set
            {
                substtransstatus = value;
                InvokePropertyChanged();
            }
        }

        private EnumE90Status Subste90status;
        [DataMember]
        public EnumE90Status SubstE90Status
        {
            get => Subste90status;
            set
            {
                Subste90status = value;
                InvokePropertyChanged();
            }
        }

        private SubstHistory[] substhists;
        [DataMember]
        public SubstHistory[] SubstHists
        {
            get => substhists;
            set
            {
                substhists = value;
                InvokePropertyChanged();
            }
        }

        #endregion

        #region TrayInfo

        public bool IsTrayEmpty => TrayStatus == TrayStatus.Empty;

        [DataMember]
        public Guid TrayInnerID { get; set; } = Guid.Empty;

        [DataMember]
        public int TrayProcessCount { get; set; }

        [DataMember]
        public int TrayUsedForWhichPM { get; set; }

        [DataMember]
        public string TrayBelongTo { get; set; }

        [DataMember]
        public string TrayOriginStation { get; set; }

        [DataMember]
        public int TrayOriginSlot { get; set; }

        private TrayStatus trayStatus = TrayStatus.Empty;
        [DataMember]
        public TrayStatus TrayStatus
        {
            get => trayStatus;
            set
            {
                trayStatus = value;
                InvokePropertyChanged();
            }
        }

        #endregion

        [DataMember]
        public int Station { get; set; }

        [DataMember]
        public int Slot { get; set; }

        [DataMember]
        public string CurrentCarrierID { get; set; }

        [DataMember]
        public string OriginCarrierID { get; set; }

        [DataMember]
        public int DestinationStation { get; set; }

        [DataMember]
        public string DestinationCarrierID { get; set; }

        [DataMember]
        public int DestinationSlot { get; set; }

        [DataMember]
        public int NextStation { get; set; }

        [DataMember]
        public int NextStationSlot { get; set; }


        private bool isSource;
        [DataMember]
        public bool IsSource
        {
            get => isSource;
            set
            {
                isSource = value;
                InvokePropertyChanged();
            }
        }

        private bool isDestination;
        [DataMember]
        public bool IsDestination
        {
            get => isDestination;
            set
            {
                isDestination = value;
                InvokePropertyChanged();
            }
        }

        public void MoveWafer(WaferInfoRt source)
        {
            //WaferInfo
            WaferID = source.WaferID;
            WaferInnerID = source.WaferInnerID;
            LotId = source.LotId;
            ProcessJob = source.ProcessJob;
            NextSequenceStep = source.NextSequenceStep;
            WaferOriginStation = source.WaferOriginStation;
            WaferOriginSlot = source.WaferOriginSlot;
            WaferOrigin = source.WaferOrigin;
            Size = source.Size;
            HasWarning = source.HasWarning;
            PPID = source.PPID;
            TransFlag = source.TransFlag;
            ProcessJobID = source.ProcessJobID;
            ControlJobID = source.ControlJobID;
            WaferStatus = source.WaferStatus;
            IsChecked = source.IsChecked;
            ProcessState = source.ProcessState;
            SubstTransStatus = source.SubstTransStatus;
            SubstE90Status = source.SubstE90Status;
            SubstHists = source.SubstHists;
        }

        public void MoveTray(WaferInfoRt source)
        {
            //TrayInfo
            TrayInnerID = source.TrayInnerID;
            TrayProcessCount = source.TrayProcessCount;
            TrayUsedForWhichPM = source.TrayUsedForWhichPM;
            TrayBelongTo = source.TrayBelongTo;
            TrayOriginStation = source.TrayOriginStation;
            TrayOriginSlot = source.TrayOriginSlot;
            TrayStatus = source.TrayStatus;
        }

        public void MoveWaferTray(WaferInfoRt source)
        {
            //WaferInfo
            WaferID = source.WaferID;
            WaferInnerID = source.WaferInnerID;
            LotId = source.LotId;
            ProcessJob = source.ProcessJob;
            NextSequenceStep = source.NextSequenceStep;
            WaferOriginStation = source.WaferOriginStation;
            WaferOriginSlot = source.WaferOriginSlot;
            WaferOrigin = source.WaferOrigin;
            Size = source.Size;
            HasWarning = source.HasWarning;
            PPID = source.PPID;
            TransFlag = source.TransFlag;
            ProcessJobID = source.ProcessJobID;
            ControlJobID = source.ControlJobID;
            WaferStatus = source.WaferStatus;
            IsChecked = source.IsChecked;
            ProcessState = source.ProcessState;
            SubstTransStatus = source.SubstTransStatus;
            SubstE90Status = source.SubstE90Status;
            SubstHists = source.SubstHists;

            //TrayInfo
            TrayInnerID = source.TrayInnerID;
            TrayProcessCount = source.TrayProcessCount;
            TrayUsedForWhichPM = source.TrayUsedForWhichPM;
            TrayBelongTo = source.TrayBelongTo;
            TrayOriginStation = source.TrayOriginStation;
            TrayOriginSlot = source.TrayOriginSlot;
            TrayStatus = source.TrayStatus;
        }

        public void SetWaferEmpty()
        {
            WaferID = string.Empty;
            WaferInnerID = Guid.Empty;
            LotId = string.Empty;
            ProcessJob = null;
            NextSequenceStep = 0;
            WaferOriginStation = string.Empty;
            WaferOriginSlot = 0;
            WaferOrigin = string.Empty;
            Size = WaferSize.WS0;
            HasWarning = false;
            PPID = string.Empty;
            TransFlag = string.Empty;
            ProcessJobID = string.Empty;
            ControlJobID = string.Empty;
            WaferStatus = WaferStatus.Empty;
            IsChecked = false;
            ProcessState = WaferProcessStatus.Idle;
            SubstTransStatus = SubstrateTransportStatus.None;
            SubstE90Status = EnumE90Status.None;
            SubstHists = null;
        }

        public void SetTrayEmpty()
        {
            //TrayInfo
            TrayInnerID = Guid.Empty;
            TrayProcessCount = 0;
            TrayUsedForWhichPM = 0;
            TrayBelongTo = string.Empty;
            TrayOriginStation = string.Empty;
            TrayOriginSlot = 0;
            TrayStatus = TrayStatus.Empty;
        }

        public void SetWaferTrayEmpty()
        {
            //WaferInfo
            WaferID = string.Empty;
            WaferInnerID = Guid.Empty;
            LotId = string.Empty;
            ProcessJob = null;
            NextSequenceStep = 0;
            WaferOriginStation = string.Empty;
            WaferOriginSlot = 0;
            WaferOrigin = string.Empty;
            Size = WaferSize.WS0;
            HasWarning = false;
            PPID = string.Empty;
            TransFlag = string.Empty;
            ProcessJobID = string.Empty;
            ControlJobID = string.Empty;
            WaferStatus = WaferStatus.Empty;
            IsChecked = false;
            ProcessState = WaferProcessStatus.Idle;
            SubstTransStatus = SubstrateTransportStatus.None;
            SubstE90Status = EnumE90Status.None;
            SubstHists = null;

            //TrayInfo
            TrayInnerID = Guid.Empty;
            TrayProcessCount = 0;
            TrayUsedForWhichPM = 0;
            TrayBelongTo = string.Empty;
            TrayOriginStation = string.Empty;
            TrayOriginSlot = 0;
            TrayStatus = TrayStatus.Empty;
        }

        public WaferInfoRt Clone()
        {
            return new WaferInfoRt
            {
                //WaferInfo
                WaferID = WaferID,
                WaferInnerID = WaferInnerID,
                LotId = LotId,
                ProcessJob = ProcessJob,
                NextSequenceStep = NextSequenceStep,
                WaferOriginStation = WaferOriginStation,
                WaferOriginSlot = WaferOriginSlot,
                WaferOrigin = WaferOrigin,
                Size = Size,
                HasWarning = HasWarning,
                PPID = PPID,
                TransFlag = TransFlag,
                ProcessJobID = ProcessJobID,
                ControlJobID = ControlJobID,
                WaferStatus = WaferStatus,
                IsChecked = IsChecked,
                ProcessState = ProcessState,
                SubstTransStatus = SubstTransStatus,
                SubstE90Status = SubstE90Status,
                SubstHists = SubstHists,

                //TrayInfo
                TrayInnerID = TrayInnerID,
                TrayProcessCount = TrayProcessCount,
                TrayUsedForWhichPM = TrayUsedForWhichPM,
                TrayBelongTo = TrayBelongTo,
                TrayOriginStation = TrayOriginStation,
                TrayOriginSlot = TrayOriginSlot,
                TrayStatus = TrayStatus,
            };
        }

    }
}
