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

using System.ComponentModel;
using System.Data;
using System.Collections.ObjectModel;

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 BoltMapEasyService:FObj, IBoltMapEasy
    {
        IFConn mConn;
        UInt32 mServerID;
        public BoltMapEasyService() 
        {
            mServerID = BOLTMAPEASY_OBJ_INTERFACE.ID;
        }
        #region IBoltMapEasy 属性

        private int boltwidth = 100;
        /// <summary>
        /// 平均分区宽度
        /// </summary>
        public int BoltWidth
        {
            get
            {
                return boltwidth;
            }
            set
            {
                if (boltwidth != value)
                {
                    boltwidth = value;
                    NotifyPropertyChanged("BoltWidth");
                }
            }
        }

        private int boltcnt = 90;
        /// <summary>
        /// 平均分区宽度
        /// </summary>
        public int BoltCnt
        {
            get
            {
                return boltcnt;
            }
            set
            {
                if (boltcnt != value)
                {
                    boltcnt = value;
                    NotifyPropertyChanged("BoltCnt");
                }
            }
        }

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

        private int borderboltno_b = 0;
        /// <summary>
        /// 开始边界分区号 
        /// </summary>
        public int BorderBoltNo_B
        {
            get { return borderboltno_b; }
            set
            {
                if (borderboltno_b != value)
                {
                    borderboltno_b = value;
                    NotifyPropertyChanged("BorderBoltNo_B");
                }
            }
        }

        private BORDER_TYPE bordertype;
        /// <summary>
        /// 找边界,边界类型
        /// </summary>
        public BORDER_TYPE BorderType
        {
            get { return bordertype; }
            set
            {
                if (bordertype != value)
                {
                    bordertype = value;
                    NotifyPropertyChanged("BorderType");
                }
            }
        }
        #endregion
        #region INotifyPropertyChanged 成员
        protected void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        public void Apply() 
        {
            BOLTMAPEASY_OBJ_INTERFACE.Pack_Params p = new BOLTMAPEASY_OBJ_INTERFACE.Pack_Params()
            {
                boltwidth = BoltWidth,
                boltcnt = BoltCnt,
                bordertype = BorderType,
                borderboltno_b = BorderBoltNo_B,
                smooth = Smooth
            };
            CurrObjSys.SetValueEx(
                    mConn, mServerID, ID, 
                    BOLTMAPEASY_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, 
                    BOLTMAPEASY_OBJ_INTERFACE.GET_PARAMS);


                CurrObjSys.SenseConfigEx(
                    mConn, mServerID, ID, 0xffffffff,
                    SENSE_CONFIG.ADD);
            }
        }
        public override void PushGetValue(IFConn from, uint srcid, ushort memid, byte[] infodata)
        {
            switch (memid)
            {
                case BOLTMAPEASY_OBJ_INTERFACE.GET_PARAMS:
                    {
                        BOLTMAPEASY_OBJ_INTERFACE.Pack_Params p = new BOLTMAPEASY_OBJ_INTERFACE.Pack_Params();
                        if (p.TryParse(infodata))
                        {
                            BoltCnt = p.boltcnt;
                            BoltWidth = p.boltwidth;
                            BorderType = p.bordertype;
                            BorderBoltNo_B = p.borderboltno_b;
                            Smooth = p.smooth;
                        }
                    } break;
            }
        }
        public override void PushInfo(IFConn from, uint srcid, ushort infoid, byte[] infodata)
        {
            PushGetValue(from, srcid, infoid, infodata);
        }
    }
}
