using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using Misc;
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 BorderSearchServiceClient: FObj, IBorderSearchService
    {
        IFConn mConn;
        UInt32 mServerID;


        public BorderSearchServiceClient(UInt32 id)
        {
            mServerID = id;
        }

        #region IBorderSearchService 成员
        private bool enable;
        public bool Enable
        {
            get
            {
                return enable;
            }
            set
            {
                if (enable != value) 
                {
                    enable = value;
                    NotifyPropertyChanged("Enable");
                }
            }
        }
        bool isbreakdetect = true;
        /// <summary>
        /// 边界拐点检测，找到的边界更加精确
        /// </summary>
        public bool IsBreakDetect
        {
            get
            {
                return isbreakdetect;
            }
            set
            {
                if (isbreakdetect != value)
                {
                    isbreakdetect = value;
                    NotifyPropertyChanged("IsBreakDetect");
                }
            }
        }
        private Range valid = new Range();
        public Range Valid
        {
            get
            {
                return valid;
            }
        }
        private Range border_backw = new Range();
        public Range Border_Backw
        {
            get
            {
                return border_backw;
            }
        }
        private Range border_forw = new Range();
        public Range Border_Forw
        {
            get
            {
                return border_forw;
            }
        }
        private int width;
        /// <summary>
        /// 当前膜宽，测量出来的，单位是 脉冲
        /// </summary>
        public int Width
        {
            get { return width; }
            protected set
            {
                if (width != value)
                {
                    width = value;
                    NotifyPropertyChanged("Width");
                }
            }
        }
        private int mid;
        /// <summary>
        /// 膜中间位置 单位 脉冲
        /// </summary>
        public int Mid
        {
            get { return mid; }
            protected set
            {
                if (mid != value)
                {
                    mid = value;
                    NotifyPropertyChanged("Mid");
                }
            }
        }

        bool tempADBySet = false;
        /// <summary>
        /// 手动设置温修AD值
        /// </summary>
        public bool TempADBySet
        {
            get
            {
                return tempADBySet;
            }
            set
            {
                if (tempADBySet != value)
                {
                    tempADBySet = value;
                    NotifyPropertyChanged("TempADBySet");
                }

            }
        }

        private int tempad;
        public int TempAD
        {
            get
            {
                return tempad;
            }
            set
            {
                if (tempad != value) 
                {
                    tempad = value;
                    NotifyPropertyChanged("TempAD");
                }
            }
        }
        private int temprange;
        public int TempRange
        {
            get
            {
                return temprange;
            }
            set
            {
                if (temprange != value)
                {
                    temprange = value;
                    NotifyPropertyChanged("TempRange");
                }
            }
        }
        private int n;
        public int N
        {
            get
            {
                return n;
            }
            set
            {
                if (n != value)
                {
                    n = value;
                    NotifyPropertyChanged("N");
                }
            }
        }

        private int sensorwidth;
        /// <summary>
        /// 探头直径，单位脉冲， 膜宽 = 边界范围 - 探头直径
        /// </summary>
        public int SensorWidth
        {
            get
            {
                return sensorwidth;
            }
            set
            {
                if (sensorwidth != value)
                {
                    sensorwidth = value;
                    NotifyPropertyChanged("SensorWidth");
                }
            }
        }
        private int n2;
        public int N2
        {
            get
            {
                return n2;
            }
            set
            {
                if (n2 != value) 
                {
                    n2 = value;
                    NotifyPropertyChanged("N2");
                }
            }
        }
        private int n3;
        public int N3
        {
            get
            {
                return n3;
            }
            set
            {
                if (n3 != value)
                {
                    n3 = value;
                    NotifyPropertyChanged("N3");
                }
            }
        }

        private int[] datas = null;
        /// <summary>
        /// 用于调试， 进入到边界查找的数据
        /// </summary>
        public int[] Datas
        {
            get { return datas; }
            protected set
            {
                datas = value;
                NotifyPropertyChanged("Datas");
            }
        }
        #endregion

        #region INotifyPropertyChanged 成员

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


        public void Apply() 
        {
            BORDERSEARCH_OBJ_INTERFACE.Pack_Params p = new BORDERSEARCH_OBJ_INTERFACE.Pack_Params();
            p.enable = Enable;
            p.tempADBySet = TempADBySet;
            p.tempad = TempAD;
            p.temprange = TempRange;
            p.n = N;
            p.n2 = N2;
            p.n3 = N3;
            p.valid = Valid;
            p.sensorwidth = SensorWidth;
            p.isBreakDetect = IsBreakDetect;

            //获取所有数据，设置推送
            CurrObjSys.SetValueEx(
                mConn, mServerID, ID, 
                BORDERSEARCH_OBJ_INTERFACE.SET_PARAMS,
                p.ToBytes());
        }
        public override void Dispose()
        {
            CurrObjSys.ObjRemove(
                this, mConn);
        }
        public override void ConnectNotify(IFConn from)
        {
            mConn = from;
            if (from.IsConnected)
            {
                //获取所有数据，设置推送
                CurrObjSys.GetValueEx(
                    mConn, mServerID, ID, 
                    BORDERSEARCH_OBJ_INTERFACE.GET_PARAMS);
                CurrObjSys.GetValueEx(
                    mConn, mServerID, ID, 
                    BORDERSEARCH_OBJ_INTERFACE.GET_RESULT);
                CurrObjSys.GetValueEx(
                    mConn, mServerID, ID,
                    BORDERSEARCH_OBJ_INTERFACE.GET_DATAS);
                CurrObjSys.SenseConfigEx(
                    mConn, mServerID, ID, 0xffffffff, SENSE_CONFIG.ADD);
            }
        }
        public override void PushGetValue(IFConn from, uint srcid, ushort memid, byte[] infodata)
        {
            switch (memid)
            {
                case BORDERSEARCH_OBJ_INTERFACE.GET_PARAMS:
                    {
                        BORDERSEARCH_OBJ_INTERFACE.Pack_Params p = new BORDERSEARCH_OBJ_INTERFACE.Pack_Params();
                        if (!p.TryParse(infodata))
                            return;

                        Enable = p.enable;
                        Valid.Begin = p.valid.Begin;
                        Valid.End = p.valid.End;
                        TempADBySet = p.tempADBySet;
                        TempAD = p.tempad;
                        TempRange = p.temprange;
                        N=p.n;
                        SensorWidth = p.sensorwidth;
                        N2=p.n2;
                        N3=p.n3;
                        IsBreakDetect = p.isBreakDetect;
                    } break;
                case BORDERSEARCH_OBJ_INTERFACE.GET_RESULT:
                    {
                        BORDERSEARCH_OBJ_INTERFACE.Pack_Result p = new BORDERSEARCH_OBJ_INTERFACE.Pack_Result();
                        if (!p.TryParse(infodata))
                            return;
                        Border_Forw.Begin = p.borderForw.Begin;
                        Border_Forw.End = p.borderForw.End;
                        Border_Backw.Begin = p.borderBackw.Begin;
                        Border_Backw.End = p.borderBackw.End;
                        Width = p.width;
                        Mid = p.mid;
                    } break;
                case BORDERSEARCH_OBJ_INTERFACE.GET_DATAS:
                    {
                        BORDERSEARCH_OBJ_INTERFACE.Pack_Data p = new BORDERSEARCH_OBJ_INTERFACE.Pack_Data();
                        if (!p.TryParse(infodata))
                            return;
                        Datas = p.data;
                    }
                    break;
            }
        }
        public override void PushInfo(IFConn from, uint srcid, ushort infoid, byte[] infodata)
        {
            PushGetValue(from, srcid, infoid, infodata);
        }


    }
}
