using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;

using FObjBase;
using FLY.Thick.Base.IService;
using FLY.Thick.Base.OBJ_INTERFACE;
using FLY.Thick.Base.Common;
namespace FLY.Thick.Base.Client
{
    public class GetSampleService : FObj, IGetSampleService
    {
        IFConn mConn;
        UInt32 mServerID;

        public GetSampleService()
        {
            mServerID = GETSAMPLE_OBJ_INTERFACE.ID;

            for (int i = 0; i < Samples.Count(); i++)
                Samples[i] = new SampleCell();

            for (int i = 0; i < Features.Count(); i++)
                Features[i] = new SampleFeature();

        }
        public class SampleCell : ISampleCell
        {
            private bool enable;
            /// <summary>
            /// 使能
            /// </summary>
            public bool Enable
            {
                get { return enable; }
                set
                {
                    if (enable != value)
                    {
                        enable = value;
                        NotifyPropertyChanged("Enable");
                    }
                }
            }

            private bool justForCheck;
            /// <summary>
            /// 参数：只检查不标定
            /// </summary>
            public bool JustForCheck 
            {
                get { return justForCheck; }
                set {
                    if (justForCheck != value) 
                    {
                        justForCheck = value;
                        NotifyPropertyChanged("JustForCheck");
                    }
                }
            }

            private int orgad;
            /// <summary>
            /// 原始AD值
            /// </summary>
            public int OrgAD
            {
                get { return orgad; }
                set
                {
                    if (this.orgad != value)
                    {
                        this.orgad = value;
                        NotifyPropertyChanged("OrgAD");
                    }
                }
            }

            private int position;
            /// <summary>
            /// 位置
            /// </summary>
            public int Position
            {
                get { return position; }
                set
                {
                    if (position != value)
                    {
                        position = value;
                        NotifyPropertyChanged("Position");
                    }
                }
            }

            private int ad;
            /// <summary>
            /// 状态：当前测量的AD值
            /// </summary>
            public int AD
            {
                get { return ad; }
                set
                {
                    if (ad != value)
                    {
                        ad = value;
                        NotifyPropertyChanged("AD");
                    }
                }
            }
            private int sampleValue;
            /// <summary>
            /// 状态：当前测量的AD值 对应的 样品值
            /// </summary>
            public int SampleValue
            {
                get
                {
                    return sampleValue;
                }
                set
                {
                    if (sampleValue != value)
                    {
                        sampleValue = value;
                        NotifyPropertyChanged("SampleValue");
                    }
                }
            }
            #region INotifyPropertyChanged 成员
            protected void NotifyPropertyChanged(string propertyName)
            {
                if (this.PropertyChanged != null)
                    this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
            }
            public event PropertyChangedEventHandler PropertyChanged;

            #endregion
        }
        public class SampleFeature : ISampleFeature
        {
            private bool enable = false;
            /// <summary>
            /// 使能
            /// </summary>
            public bool Enable
            {
                get { return enable; }
                set
                {
                    if (enable != value)
                    {
                        enable = value;
                        NotifyPropertyChanged("Enable");
                    }
                }
            }
            private int startpos;
            /// <summary>
            /// 开始位置
            /// </summary>
            public int StartPos
            {
                get { return startpos; }
                set
                {
                    if (startpos != value)
                    {
                        startpos = value;
                        NotifyPropertyChanged("StartPos");
                    }
                }
            }
            private int endpos;
            /// <summary>
            /// 结束位置
            /// </summary>
            public int EndPos
            {
                get { return endpos; }
                set
                {
                    if (endpos != value)
                    {
                        endpos = value;
                        NotifyPropertyChanged("EndPos");
                    }
                }
            }

            private double maxRelevancy = -1;
            /// <summary>
            /// 状态：相似度必须高于0.8，失败-1
            /// </summary>
            public double MaxRelevancy
            {
                get { return maxRelevancy; }
                set
                {
                    if (maxRelevancy != value)
                    {
                        maxRelevancy = value;
                        NotifyPropertyChanged("MaxRelevancy");
                    }
                }
            }

            private int maxOffset;
            /// <summary>
            /// 状态：最大相似度对应的偏移
            /// </summary>
            public int MaxOffset
            {
                get { return maxOffset; }
                set
                {
                    if (maxOffset != value)
                    {
                        maxOffset = value;
                        NotifyPropertyChanged("MaxOffset");
                    }
                }
            }



            private int[] scanData;
            /// <summary>
            /// 状态：扫描数据，从扫描数据提取的相似的一段
            /// 它的单位是 grid, 需要 posOfGrid, 转换为 pos
            /// 开始位置是 StartPos - Search
            /// </summary>
            public int[] ScanData
            {
                get
                {
                    return scanData;
                }
                set
                {
                    scanData = value;
                    NotifyPropertyChanged("ScanData");
                }
            }

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

            #endregion
        }
        #region IGetSampleService 成员
        
        private bool enable;
        public bool Enable
        {
            get { return enable; }
            set
            {
                if (enable != value)
                {
                    enable = value;
                    NotifyPropertyChanged("Enable");
                }
            }
        }
        private int interval;
        public int Interval
        {
            get { return interval; }
            set
            {
                if (interval != value)
                {
                    interval = value;
                    NotifyPropertyChanged("Interval");
                }
            }
        }
        private UInt32 velocity;
        public UInt32 Velocity
        {
            get { return velocity; }
            set
            {
                if (velocity != value)
                {
                    velocity = value;
                    NotifyPropertyChanged("Velocity");
                }
            }
        }
        private int range;
        public int Range
        {
            get { return range; }
            set
            {
                if (range != value)
                {
                    range = value;
                    NotifyPropertyChanged("Range");
                }
            }
        }
        //滤波移动窗口
        private int window;
        /// <summary>
        /// 滤波移动窗口
        /// </summary>
        public int Window
        {
            get { return window; }
            set
            {
                if (window != value)
                {
                    window = value;
                    NotifyPropertyChanged("Window");
                }
            }
        }


        //样品点
        private ISampleCell[] samples = new ISampleCell[3];
        /// <summary>
        /// 样品点
        /// </summary>
        public ISampleCell[] Samples
        {
            get { return samples; }
        }



        private int search;
        public int Search
        {
            get { return search; }
            set
            {
                if (search != value)
                {
                    search = value;
                    NotifyPropertyChanged("Search");
                }
            }
        }

        private ISampleFeature[] features = new ISampleFeature[2];
        /// <summary>
        /// 相识度 0 正， 1 反
        /// </summary>
        public ISampleFeature[] Features
        {
            get { return features; }
        }

        private int posOfGrid = 10;
        /// <summary>
        /// 状态：从flyad7 得到的。 用于绘制图
        /// </summary>
        public int PosOfGrid
        {
            get { return posOfGrid; }
            set
            {
                if (posOfGrid != value)
                {
                    posOfGrid = value;
                    NotifyPropertyChanged("PosOfGrid");
                }
            }
        }
        public void Apply() 
        {
            GETSAMPLE_OBJ_INTERFACE.Pack_Params p = new GETSAMPLE_OBJ_INTERFACE.Pack_Params();
            
            p.enable = Enable;
            p.interval = Interval;
            p.velocity = Velocity;
            p.range = Range;
            p.window = Window;
            p.samples = new GETSAMPLE_OBJ_INTERFACE.Pack_Params_SampleCell[Samples.Count()];

            for (int i = 0; i < Samples.Count(); i++)
            {
                p.samples[i] = new GETSAMPLE_OBJ_INTERFACE.Pack_Params_SampleCell();
                p.samples[i].enable = Samples[i].Enable;
                p.samples[i].justForCheck = Samples[i].JustForCheck;
                p.samples[i].orgad = Samples[i].OrgAD;
                p.samples[i].position = Samples[i].Position;
            }
            p.search = Search;

            p.features = new GETSAMPLE_OBJ_INTERFACE.Pack_Params_SampleFeature[Features.Count()];

            for (int i = 0; i < Features.Count(); i++)
            {
                p.features[i] = new GETSAMPLE_OBJ_INTERFACE.Pack_Params_SampleFeature();
                p.features[i].enable = Features[i].Enable;
                p.features[i].startpos = Features[i].StartPos;
                p.features[i].endpos = Features[i].EndPos;
            }

            //获取所有数据，设置推送
            CurrObjSys.SetValueEx(
                mConn, mServerID, ID, 
                GETSAMPLE_OBJ_INTERFACE.SET_PARAMS,
                p.ToBytes()
                );
        }
        #endregion




        #region INotifyPropertyChanged 成员

        public event PropertyChangedEventHandler PropertyChanged;
        protected void NotifyPropertyChanged(string propertyname)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyname));
            }
        }
        #endregion

        public override void Dispose()
        {
            CurrObjSys.ObjRemove(
                this, mConn);
        }
        public override void ConnectNotify(IFConn from)
        {
            mConn = from;
            if (from.IsConnected)
            {
                //获取所有数据，设置推送
                CurrObjSys.GetValueEx(
                    mConn, mServerID, ID, 
                    GETSAMPLE_OBJ_INTERFACE.GET_PARAMS);
                CurrObjSys.GetValueEx(
                    mConn, mServerID, ID,
                    GETSAMPLE_OBJ_INTERFACE.GET_STATE);

                CurrObjSys.SenseConfigEx(
                    mConn, mServerID, ID,
                    0xffffffff, SENSE_CONFIG.ADD);

            }
        }
        public override void PushGetValue(IFConn from, uint srcid, ushort memid, byte[] infodata)
        {
            switch (memid)
            {
                case GETSAMPLE_OBJ_INTERFACE.GET_PARAMS:
                    {
                        GETSAMPLE_OBJ_INTERFACE.Pack_Params p = new GETSAMPLE_OBJ_INTERFACE.Pack_Params();
                        if (!p.TryParse(infodata))
                            return;

                        Enable = p.enable;
                        Interval = p.interval;
                        Velocity = p.velocity;
                        Range = p.range;
                        Window = p.window;
                        for (int i = 0; i < Samples.Count() && i < p.samples.Count(); i++)
                        {
                            Samples[i].Enable = p.samples[i].enable;
                            Samples[i].JustForCheck = p.samples[i].justForCheck;
                            Samples[i].OrgAD = p.samples[i].orgad;
                            Samples[i].Position = p.samples[i].position;
                        }
                        Search = p.search;
                        for (int i = 0; i < Features.Count() && i < p.features.Count(); i++)
                        {
                            Features[i].Enable = p.features[i].enable;
                            Features[i].StartPos = p.features[i].startpos;
                            Features[i].EndPos = p.features[i].endpos;
                        }
                    } break;
                case GETSAMPLE_OBJ_INTERFACE.GET_STATE:
                    {
                        GETSAMPLE_OBJ_INTERFACE.Pack_State p = new GETSAMPLE_OBJ_INTERFACE.Pack_State();
                        
                        if (!p.TryParse(infodata ))
                            return;
                        PosOfGrid = p.posOfGrid;

                        for (int i = 0; i < Samples.Count() && i < p.samples.Count(); i++)
                        {
                            Samples[i].AD = p.samples[i].ad;
                            Samples[i].SampleValue = p.samples[i].value;
                        }
                        for (int i = 0; i < Features.Count() && i < p.features.Count(); i++)
                        {
                            Features[i].MaxRelevancy = p.features[i].maxRelevancy;
                            Features[i].MaxOffset = p.features[i].maxOffset;
                            Features[i].ScanData = p.features[i].scanData;
                        }
                    } break;
            }
        }
        public override void PushInfo(IFConn from, uint srcid, ushort infoid, byte[] infodata)
        {
            PushGetValue(from, srcid, infoid, infodata);
        }
    }

}
