using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Misc;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Collections.ObjectModel;

namespace FLY.Thick.Base.Common
{
    /// <summary>
    /// 动态数据，推送当前所有状态
    /// </summary>
    public class DynArea : INotifyPropertyChanged
    {
        public DynArea() 
        {
            this.PropertyChanged += new PropertyChangedEventHandler(DynArea_PropertyChanged);
        }

        void DynArea_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "Hrs") 
            {
                if (Hrs == 0)
                {
                    SecuteLock = true;
                }
                else 
                {
                    SecuteLock = false;
                }
            }
        }

        #region BASE

        private int thick = 3210;
        public int Thick 
        {
            get { return thick; }
            set {
                if (thick != value)
                {
                    thick = value;
                    NotifyPropertyChanged("Thick");
                }
            }
        }


        private int position = 0;
        public int Position 
        {
            get { return position; }
            set
            {
                if (position != value)
                {
                    position = value;
                    NotifyPropertyChanged("Position");
                }
            }            
        }

        private int boltIndex = 0;
        public int BoltIndex
        {
            get { return boltIndex; }
            set
            {
                if (boltIndex != value)
                {
                    boltIndex = value;
                    NotifyPropertyChanged("BoltIndex");
                    NotifyPropertyChanged("BoltNo");
                }
            }
        }
        
        public int BoltNo
        {
            get { return BoltIndex + FirstBoltNo; }
        }

        private int ad = 12345;
        public int AD
        {
            get { return ad; }
            set
            {
                if (ad != value)
                {
                    ad = value;
                    NotifyPropertyChanged("AD");
                }
            }
        }

        private int admax = 65535;
        public int ADMax {
            get { return admax; }
            set
            {
                if (admax != value)
                {
                    admax = value;
                    NotifyPropertyChanged("ADMax");
                }
            }
        }
        #endregion

        #region POSITION2
        private int position2=0;
        public int Position2 
        {
            get { return position2; }
            set {
                if (position2 != value)
                {
                    position2 = value;
                    NotifyPropertyChanged("Position2");
                }
            }
        }
        #endregion
        #region 扫描
        private ObservableCollection<int> sampleAD = new ObservableCollection<int>(new int[3] { -1, -1,-1 });
        /// <summary>
        /// 最新样品AD
        /// </summary>
        public ObservableCollection<int> SampleAD
        {
            get { return sampleAD; }
        }

        private bool tempState = false;
        /// <summary>
        /// 动态温修
        /// </summary>
        public bool TempState
        {
            get { return tempState; }
            set
            {
                if (tempState != value)
                {
                    tempState = value;
                    NotifyPropertyChanged("TempState");
                }
            }
        }


        //TODO
        private bool datavalid=false;
        public bool DataValid//数据是否有效， 当膜没有动时，为无效。 通过PLC获取，或编码器判断
        {
            get
            {
                return datavalid;
            }
            set
            {
                if (datavalid != value)
                {
                    datavalid = value;
                    NotifyPropertyChanged("DataValid");
                }
            }
        }

        private int rolldown=0;
        public int RollDown 
        {
            get
            {
                return rolldown;
            }
            set
            {
                if (rolldown != value)
                {
                    rolldown = value;
                    NotifyPropertyChanged("RollDown");
                }
            }
        }

        private Misc.DIRECTION direction=DIRECTION.FIX;
        public Misc.DIRECTION Direction//扫描方向
        {
            get
            {
                return direction;
            }
            set
            {
                if (direction != value)
                {
                    direction = value;
                    NotifyPropertyChanged("Direction");
                }
            }
        }

        private double width = 600;//膜宽 mm
        public double Width 
        {
            get { return width; }
            set {
                if (width != value)
                {
                    width = value;
                    NotifyPropertyChanged("Width");
                }
            }
        }
        private double filmVelocity = 0;//m/min
        public double FilmVelocity
        {
            get { return filmVelocity; }
            set
            {
                if (filmVelocity != value)
                {
                    filmVelocity = value;
                    NotifyPropertyChanged("FilmVelocity");
                }
            }
        }
        private double velocity = 0;//m/min
        public double Velocity
        {
            get { return velocity; }
            set
            {
                if (velocity != value)
                {
                    velocity = value;
                    NotifyPropertyChanged("Velocity");
                }
            }
        }
        #endregion

        #region 运行状态
        private CTRL_STATE _controllerState;
        public CTRL_STATE ControllerState//运行状态
        {
            get {
                return _controllerState;
            }
            set {
                if (_controllerState != value) 
                {
                    _controllerState = value;
                    NotifyPropertyChanged("ControllerState");
                }
            }
        }
        private int autoscancounter=5;
        public int AutoScanCounter 
        {
            get
            {
                return autoscancounter;
            }
            set
            {
                if (autoscancounter != value)
                {
                    autoscancounter = value;
                    NotifyPropertyChanged("AutoScanCounter");
                }
            }
        }
        #endregion

        #region IO
        private UInt16 iStatus = 0xffff;
        public UInt16 IStatus
        {
            get { return iStatus; }
            set
            {
                if (iStatus != value)
                {
                    iStatus = value;
                    NotifyPropertyChanged("IStatus");
                }
            }
        }
        public UInt16 oStatus = 0xffff;
        public UInt16 OStatus
        {
            get { return oStatus; }
            set
            {
                if (oStatus != value)
                {
                    oStatus = value;
                    NotifyPropertyChanged("OStatus");
                }
            }
        }
        #endregion

        #region 系统
        private int hrs=3;
        /// <summary>
        /// 系统剩余小时数
        /// </summary>
        public int Hrs 
        {
            get { return hrs; }
            set
            {
                if (hrs != value)
                {
                    hrs = value;
                    NotifyPropertyChanged("Hrs");
                }
            }            
        }
        private string version = "null";
        /// <summary>
        /// 版本号
        /// </summary>
        public string Version 
        {
            get { return version; }
            set
            {
                if (version != value)
                {
                    version = value;
                    NotifyPropertyChanged("Version");
                }
            }
        }
        private bool isConnect = false;
        public bool FLYADIsConnect//FLYAD 连接状态
        {
            get { return isConnect; }
            set
            {
                if (isConnect != value)
                {
                    isConnect = value;
                    NotifyPropertyChanged("FLYADIsConnect");
                }
            }
        }
        private bool secuteLock = false;
        public bool SecuteLock
        {
            get { return secuteLock; }
            set
            {
                if (secuteLock != value)
                {
                    secuteLock = value;
                    NotifyPropertyChanged("SecuteLock");
                }
            }
        }
        private bool serverIsConnect = false;
        public bool ServerIsConnected 
        {
            get { return serverIsConnect; }
            set
            {
                serverIsConnect = value;
                NotifyPropertyChanged("ServerIsConnected");
            }           
        }

        private GAGE_TYPE gagetype = GAGE_TYPE.Normal;
        public GAGE_TYPE GageType
        {
            get { return gagetype; }
            set
            {
                if (gagetype != value)
                {
                    gagetype = value;
                    NotifyPropertyChanged("GageType");
                }
            }
        }
        #endregion

        #region PROFILE 运行参数

        private string productName = "default";
        public string ProductName//产品名称
        {
            get { return productName; }
            set 
            {
                if (productName != value) 
                {
                    productName = value;
                    
                    NotifyPropertyChanged("ProductName");
                }
            }
        }
        private int target = 3000;
        /// <summary>
        /// 目标值
        /// </summary>
        public int Target
        {
            get { return target; }
            set
            {
                if (target != value)
                {
                    target = value;
                    
                    NotifyPropertyChanged("Target");
                }
            }
        }

        private int alarm = 1000;
        /// <summary>
        /// 报警值
        /// </summary>
        public int Alarm
        {
            get { return alarm; }
            set
            {
                if (alarm != value)
                {
                    alarm = value;
                    NotifyPropertyChanged("Alarm");
                    NotifyPropertyChanged("YRange");
                }
            }
        }
        //Y轴范围
        public int YRange 
        {
            get { return alarm*3; }
            set { 
                
            }
        }
        private double comp=1;
        public double Comp
        {
            get { return comp; }
            set
            {
                if (comp != value)
                {
                    comp = value;
                    NotifyPropertyChanged("Comp");
                }
            }
        }
        private int shift=0;
        public int Shift
        {
            get { return shift; }
            set
            {
                if (shift != value)
                {
                    shift = value;
                    NotifyPropertyChanged("Shift");
                }
            }
        }

        private int _dataBoltNoBegin = 0;
        /// <summary>
        /// 数据开始分区号
        /// </summary>
        public int DataBoltNoBegin 
        {
            get { return _dataBoltNoBegin; }
            set
            {
                if (_dataBoltNoBegin != value)
                {
                    _dataBoltNoBegin = value;
                    NotifyPropertyChanged("DataBoltNoBegin");
                }
            }       
        }
        private int dataBoltNoEnd = 30;
        /// <summary>
        /// 数据结束分区号
        /// </summary>
        public int DataBoltNoEnd 
        {
            get { return dataBoltNoEnd; }
            set
            {
                if (dataBoltNoEnd != value)
                {
                    dataBoltNoEnd = value;
                    NotifyPropertyChanged("DataBoltNoEnd");
                }
            }            
        }


        private int _scanBoltNoBegin=0;

        public int ScanBoltNoBegin
        {
            get { return _scanBoltNoBegin; }
            set { _scanBoltNoBegin = value;
            NotifyPropertyChanged("ScanBoltNoBegin");
            }
        }
        private int _scanBoltNoEnd=30;

        public int ScanBoltNoEnd
        {
            get { return _scanBoltNoEnd; }
            set
            {
                _scanBoltNoEnd = value;
                NotifyPropertyChanged("ScanBoltNoEnd");
            }
        }
        #endregion

        #region BOLTMAP 分区信息
        private int nbolts = 50;
        /// <summary>
        /// 总分区数
        /// </summary>
        public int NBolts 
        {
            get { return nbolts; }
            set 
            { 
                if (nbolts != value)
                {
                    nbolts = value;
                    
                    NotifyPropertyChanged("NBolts");
                }
            }
        }

        private int _firstBoltNo = 0;
        /// <summary>
        /// 开始分区号
        /// </summary>
        public int FirstBoltNo
        {
            get { return _firstBoltNo; }
            set
            {
                if (_firstBoltNo != value)
                {
                    _firstBoltNo = value;
                    NotifyPropertyChanged("FirstBoltNo");
                }
            }
        }
        #endregion


        #region INotifyPropertyChanged 成员
        protected void NotifyPropertyChanged(string propertyName) 
        {
            if (PropertyChanged != null)
            {
                PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        public event PropertyChangedEventHandler PropertyChanged;

        #endregion
    }
}
