using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using Aitex.Core.RT.Log;
using MECF.Framework.Common.CommonData;
using MECF.Framework.Common.Jobs;
namespace Aitex.Core.Common
{
    public enum WaferSize
    {
        WS0,    //undefine, initialize status
        WS3,
        WS4=100,
        WS6=150,
        WS150,  //150mm
        WS159,  //150mm with tray
        WS8=200,
        WS12=300,
    }
    
    public enum WaferStatus
	{
		Empty = 0,
		Normal = 1,
		Crossed = 3,
		Thin=4,
		Double = 7,
		Unknown = 8,
		Dummy = 4
	}
	public enum WaferType
	{
		/// 
		/// 生产片
		/// 
		Production=0,
		/// 
		/// 辅助片
		/// 
		Assit=1
	}
	public enum EnumWaferProcessStatus
	{
		Idle = 0,
		Dummy,
		Canceled,
		InProcess, 
		Completed,
		Failed,
		MisProcessed,
		MisSrdProcess
	}
    public enum EnumWaferChuckStatus
    {
        Init = 0,
        Chucked,
        Dechucked,
    }
	[Serializable]
	[DataContract]
	public class WaferInfo : NotifiableItem
	{
		public bool IsEmpty
		{
			get { return Status == WaferStatus.Empty; }
		}
		private string waferID;
		[DataMember]
		public string WaferID
		{
			get
			{
				return waferID;
			}
			set
			{
				waferID = value;
				InvokePropertyChanged("WaferID");
			}
		}
		private string _waferOrigin;
		[DataMember]
		public string WaferOrigin
		{
			get
			{
				return _waferOrigin;
			}
			set
			{
				_waferOrigin = value;
				InvokePropertyChanged("WaferOrigin");
			}
		}
		/// 
		/// Wafer类型
		/// 
		private WaferType _waferType;
		[DataMember]
		public WaferType WaferType
		{
			get { return _waferType; }
			set { _waferType = value; InvokePropertyChanged(nameof(WaferType)); }	
		}
		private string laserMarker;
		[DataMember]
		public string LaserMarker
		{
			get
			{
				return laserMarker;
			}
			set
			{
				laserMarker = value;
			    InvokePropertyChanged("LaserMarker");
			}
		}
		private string t7Code;
		[DataMember]
		public string T7Code
		{
			get
			{
				return t7Code;
			}
			set
			{
				t7Code = value;
			    InvokePropertyChanged("T7Code");
			}
		}
		
	    [DataMember]
	    public string LotId { get; set; }
	    [DataMember]
	    public string TransFlag { get; set; }
        private WaferStatus status;
		[DataMember]
		public WaferStatus Status
		{
			get
			{
				return status;
			}
			set
			{
				status = value;
			    InvokePropertyChanged("Status");
			}
		}
        [DataMember]
		public int Station
		{
			get;
			set;
		}
		[DataMember]
		public int Slot
		{
			get;
			set;
		}
		[DataMember]
		public int OriginStation
		{
			get;
			set;
		}
		[DataMember]
		public int OriginSlot
		{
			get;
			set;
		}
        [DataMember]
        public string OriginCarrierID
        {
            get;
            set;
        }
        [DataMember]
		public int SourceStation
		{
			get;
			set;
		}
		[DataMember]
		public int SourceSlot
		{
			get;
			set;
		}
		[DataMember]
		public int DestinationStation
		{
			get;
			set;
		}
		[DataMember]
		public int DestinationSlot
		{
			get;
			set;
		}
		[DataMember]
		public int Notch
		{
			get;
			set;
		}
        [DataMember]
        public WaferSize Size
        {
            get;
            set;
        }
		private string _sequenceName;
		[DataMember]
		public string SequenceName
		{
			get { return _sequenceName; }
			set 
			{
				_sequenceName = value;
                InvokePropertyChanged(nameof(SequenceName));	
			}
		}
		private EnumWaferProcessStatus processState;
		[DataMember]
		public EnumWaferProcessStatus ProcessState
		{
			get
			{
				return processState;
			}
			set
			{
				processState = value;
			    InvokePropertyChanged("ProcessState");
			}
		}
		private EnumWaferChuckStatus chuckState;
		[DataMember]
		public EnumWaferChuckStatus ChuckState
        {
			get
            {
				return chuckState;
            }
			set
            {
				chuckState = value;
				InvokePropertyChanged("ChuckState");
            }
        }
		private bool isSource;
	    [DataMember]
        public bool IsSource
		{
			get
			{
				return isSource;
			}
			set
			{
				isSource = value;
			    InvokePropertyChanged("IsSource");
			}
		}
		private bool isDestination;
	    [DataMember]
        public bool IsDestination
		{
			get
			{
				return isDestination;
			}
			set
			{
				isDestination = value;
				InvokePropertyChanged("IsDestination");
			}
		}
	    [DataMember]
        public Guid InnerId { get; set; }
	    [DataMember]
        public ProcessJobInfo ProcessJob { get; set; }
	    [DataMember]
		public int NextSequenceStep
		{
			get { return _nextSequenceStep; }
			set { _nextSequenceStep = value; LOG.Write((eEvent)99999, Station.ToString(), _nextSequenceStep.ToString()); }
		}
		private int _nextSequenceStep;
        private string _lotTrackPath;
        [DataMember]
        public string LotTrackPath
        {
            get
            {
                return _lotTrackPath;
            }
            set
            {
                _lotTrackPath = value;
                InvokePropertyChanged("LotTrackPath");
            }
        }
		private string _orginalWaferHolder;
		public string OrginalWaferHolder
		{
			get { return _orginalWaferHolder; }
			set 
			{
				_orginalWaferHolder=value;
				InvokePropertyChanged(nameof(OrginalWaferHolder));
			}
		}
		private string _loaderSide;
		public string LoaderSide
		{
			get { return _loaderSide; }
			set { _loaderSide = value; }
		}
        public WaferInfo()
		{
			InnerId = Guid.Empty;
			WaferType = WaferType.Production;
		}
		public WaferInfo(string waferID, WaferStatus status = WaferStatus.Empty,WaferType waferType=WaferType.Production) : this()
		{
			this.WaferID = waferID;
			this.Status = status;
			this.WaferType = waferType;
		}
 
		public void Update(WaferInfo source)
		{
		    InnerId = source.InnerId;
			WaferID = source.waferID;
			WaferOrigin = source.WaferOrigin;
			LaserMarker = source.LaserMarker;
			T7Code = source.T7Code;
			Status = source.Status;
			ProcessState = source.ProcessState;
			ChuckState = source.ChuckState;
			IsSource = source.IsSource;
			IsDestination = source.IsDestination;
			Station = source.Station;
			Slot = source.Slot;
			OriginStation = source.OriginStation;
			OriginSlot = source.OriginSlot;
            if(source.OriginCarrierID!=null) OriginCarrierID = source.OriginCarrierID;
			SourceStation = source.SourceStation;
			SourceSlot = source.SourceSlot;
			DestinationStation = source.DestinationStation;
			DestinationSlot = source.DestinationSlot;
			Notch = source.Notch;
            Size  = source.Size;
		    TransFlag = source.TransFlag;
		    LotId = source.LotId;
		    ProcessJob = source.ProcessJob;
		    NextSequenceStep = source.NextSequenceStep;
			OrginalWaferHolder = source.OrginalWaferHolder;
			SequenceName = source.SequenceName;
			WaferType= source.WaferType;
			LotTrackPath = source.LotTrackPath;
			LoaderSide= source.LoaderSide;
		}
	    public void SetEmpty()
	    {
            this.InnerId = Guid.Empty;
	        
	        this.WaferID = string.Empty;
	        this.WaferOrigin = string.Empty;
	        this.LaserMarker = string.Empty;
	        this.T7Code = string.Empty;
            this.Status = (int)WaferStatus.Empty;
 
	        this.ProcessState = EnumWaferProcessStatus.Idle;
			this.ChuckState = EnumWaferChuckStatus.Init;
	        this.IsSource = false;
	        this.IsDestination = false;
            
	        this.Station = 0;
	        this.Slot = 0;
			this.SequenceName = "";
			this.OrginalWaferHolder = "";
	        this.OriginStation = 0;
	        this.OriginSlot = 0;
	        this.SourceStation = 0;
	        this.SourceSlot = 0;
	        this.DestinationStation = 0;
	        this.DestinationSlot = 0;
	        this.Notch = 0;
	        //this.Size = WaferSize.WS0; //wafersize 保留最后的一次的记录
            this.TransFlag = string.Empty;
	        this.LotId = string.Empty;
	        this.ProcessJob = null;
	        this.NextSequenceStep = 0;
			this.LotTrackPath = string.Empty;
			this.LoaderSide = "";
	    }
		public WaferInfo Clone(WaferInfo source)
		{
			WaferInfo newValue = new WaferInfo();
			newValue.InnerId = source.InnerId;
			newValue.WaferID = source.waferID;
			newValue.WaferOrigin = source.WaferOrigin;
			newValue.LaserMarker = source.LaserMarker;
			newValue.T7Code = source.T7Code;
			newValue.Status = source.Status;
			newValue.ProcessState = source.ProcessState;
			newValue.ChuckState = source.ChuckState;
			newValue.IsSource = source.IsSource;
			newValue.IsDestination = source.IsDestination;
			newValue.Station = source.Station;
			newValue.Slot = source.Slot;
			newValue.OriginStation = source.OriginStation;
			newValue.OriginSlot = source.OriginSlot;
			if (source.OriginCarrierID != null) newValue.OriginCarrierID = source.OriginCarrierID;
			newValue.SourceStation = source.SourceStation;
			newValue.SourceSlot = source.SourceSlot;
			newValue.DestinationStation = source.DestinationStation;
			newValue.DestinationSlot = source.DestinationSlot;
			newValue.Notch = source.Notch;
			newValue.Size = source.Size;
			newValue.TransFlag = source.TransFlag;
			newValue.LotId = source.LotId;
			newValue.WaferType = source.WaferType;
			newValue.NextSequenceStep = source.NextSequenceStep;
			newValue.OrginalWaferHolder = source.OrginalWaferHolder;
			newValue.SequenceName= source.SequenceName;
			newValue.LoaderSide = source.LoaderSide;
			//if(source.ProcessJob != null)
   //         {
			//	newValue.ProcessJob = new ProcessJobInfo();
			//	if(source.ProcessJob.Sequence != null)
   //             {
			//		newValue.ProcessJob.Sequence = new SequenceInfo(source.ProcessJob.Sequence.Name);
			//		newValue.ProcessJob.Sequence.InnerId = source.ProcessJob.Sequence.InnerId;
			//		newValue.ProcessJob.Sequence.ThicknessType = source.ProcessJob.Sequence.ThicknessType;
			//		foreach(var step in source.ProcessJob.Sequence.Steps)
   //                 {
			//			var newStep = new SequenceStepInfo();
			//			newStep.AlignAngle = step.AlignAngle;
			//			newStep.StepModules = new List(step.StepModules.ToArray());
			//			newStep.StepParameter = step.StepParameter.ToDictionary(entry => entry.Key, entry => entry.Value);
			//			newValue.ProcessJob.Sequence.Steps.Add(newStep);
   //                 }
   //             }
   //         }
			return newValue;
		}
	}
}