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 BoltMapServiceClient:FObj, IBoltMapService
    {

         IFConn mConn;
        UInt32 mServerID;
        public BoltMapServiceClient() 
        {
            mServerID = BOLTMAP_OBJ_INTERFACE.ID;
        }
        #region BoltMapService
        private int firstboltno;
        public int FirstBoltNo
        {
            get
            {
                return firstboltno;
            }
            set
            {
                if (firstboltno != value)
                {
                    firstboltno = value;
                    NotifyPropertyChanged("FirstBoltNo");
                }
            }
        }
        private ObservableCollection<Range> bolts = new ObservableCollection<Range>();
        public ObservableCollection<Range> Bolts
        {
            get { return bolts; }
        }

        #region 只用于描述 Bolts，没什么用
        private int lastboltno;
        /// <summary>
        /// 最后一个分区号
        /// </summary>
        public int LastBoltNo
        {
            get
            {
                return lastboltno;
            }
            protected set
            {
                if (lastboltno != value)
                {
                    lastboltno = value;
                    NotifyPropertyChanged("LastBoltNo");
                }
            }
        }
        private int boltinterval;
        /// <summary>
        /// 平均分区间隔
        /// </summary>
        public int BoltInterval
        {
            get
            {
                return boltinterval;
            }
            set
            {
                if (boltinterval != value)
                {
                    boltinterval = value;
                    NotifyPropertyChanged("BoltInterval");
                }
            }
        }
        private int boltwidth;
        /// <summary>
        /// 平均分区宽度
        /// </summary>
        public int BoltWidth
        {
            get
            {
                return boltwidth;
            }
            protected set
            {
                if (boltwidth != value)
                {
                    boltwidth = value;
                    NotifyPropertyChanged("BoltWidth");
                }
            }
        }
        private int posbegin;
        /// <summary>
        /// 脉冲范围 开始
        /// </summary>
        public int PosBegin
        {
            get
            {
                return posbegin;
            }
            protected set
            {
                if (posbegin != value)
                {
                    posbegin = value;
                    NotifyPropertyChanged("PosBegin");
                }
            }
        }
        private int posend;
        /// <summary>
        /// 脉冲范围 结束
        /// </summary>
        public int PosEnd
        {
            get
            {
                return posend;
            }
            protected set
            {
                if (posend != value)
                {
                    posend = value;
                    NotifyPropertyChanged("PosEnd");
                }
            }
        }
        #endregion
        #endregion
        #region INotifyPropertyChanged 成员
        protected void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        public event PropertyChangedEventHandler PropertyChanged;

        #endregion


        public event ActiveEventHandler ActiveEvent;

        private BORDER_TYPE bordertype;
        /// <summary>
        /// 找边界,边界类型
        /// </summary>
        public BORDER_TYPE BorderType
        {
            get { return bordertype; }
            set
            {
                if (bordertype != value)
                {
                    bordertype = value;
                    NotifyPropertyChanged("BorderType");
                }
            }
        }

        private int borderboltno_b;
        /// <summary>
        /// 开始边界分区号 
        /// </summary>
        public int BorderBoltNo_B
        {
            get { return borderboltno_b; }
            set
            {
                if (borderboltno_b != value)
                {
                    borderboltno_b = value;
                    NotifyPropertyChanged("BorderBoltNo_B");
                }
            }
        }
        private int borderboltno_e;
        /// <summary>
        /// 结束边界分区号
        /// </summary>
        public int BorderBoltNo_E
        {
            get { return borderboltno_e; }
            set
            {
                if (borderboltno_e != value)
                {
                    borderboltno_e = value;
                    NotifyPropertyChanged("BorderBoltNo_E");
                }
            }
        }
        private int borderboltno_mid;
        /// <summary>
        /// 边界分区号 中间
        /// </summary>
        public int BorderBoltNo_M
        {
            get { return borderboltno_mid; }
            set
            {
                if (borderboltno_mid != value)
                {
                    borderboltno_mid = value;
                    NotifyPropertyChanged("BorderBoltNo_Mid");
                }
            }
        }
        private int smooth;
        public int Smooth
        {
            get { return smooth; }
            set
            {
                if (smooth != value)
                {
                    smooth = value;
                    NotifyPropertyChanged("Smooth");
                }
            }
        }
        /// <summary>
        /// 转DataTable       
        /// </summary>
        /// <param name="dt"></param>
        public void Bolts2DataTable(DataTable dt)
        {
            dt.Clear();
            dt.Columns.Clear();
            dt.Columns.Add(new DataColumn("No", typeof(int)));
            dt.Columns.Add(new DataColumn("Begin", Type.GetType("System.Int32")));
            dt.Columns.Add(new DataColumn("End", Type.GetType("System.Int32")));

            for (int i = 0; i < Bolts.Count; i++)
            {
                DataRow dr = dt.NewRow();
                dr["No"] = FirstBoltNo + i;
                dr["Begin"] = Bolts[i].Begin;
                dr["End"] = Bolts[i].End;
                dt.Rows.Add(dr);
            }
        }
        public void Apply() 
        {
            BOLTMAP_OBJ_INTERFACE.Pack_Params p = new BOLTMAP_OBJ_INTERFACE.Pack_Params();
            p.boltNo1st = FirstBoltNo;
            p.bolts = (from c in Bolts select new Range() { Begin = c.Begin, End = c.End }).ToArray();
            CurrObjSys.SetValueEx(
                    mConn, mServerID, ID, 
                    BOLTMAP_OBJ_INTERFACE.SET_PARAMS,
                    p.ToBytes());
        }
        public void ApplyBorder() 
        {
            CurrObjSys.SetValueEx(
                mConn, mServerID, ID, 
                BOLTMAP_OBJ_INTERFACE.SET_BORDERPARAMS,
                new BOLTMAP_OBJ_INTERFACE.Pack_BorderParams()
                {
                    bordertype = BorderType,
                    borderboltno_b = BorderBoltNo_B,
                    borderboltno_e = BorderBoltNo_E,
                    smooth = Smooth
                }.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, 
                    BOLTMAP_OBJ_INTERFACE.GET_PARAMS);
                CurrObjSys.GetValueEx(
                    mConn, mServerID, ID, 
                    BOLTMAP_OBJ_INTERFACE.GET_BORDERPARAMS);
                CurrObjSys.GetValueEx(
                    mConn, mServerID, ID, 
                    BOLTMAP_OBJ_INTERFACE.GET_BOLTSINFO);

                CurrObjSys.SenseConfigEx(
                    mConn, mServerID, ID, 0xffffffff,
                    SENSE_CONFIG.ADD);
            }
        }
        public override void PushGetValue(IFConn from, uint srcid, ushort memid, byte[] infodata)
        {
            switch (memid)
            {
                case BOLTMAP_OBJ_INTERFACE.GET_PARAMS:
                    {
                        BOLTMAP_OBJ_INTERFACE.Pack_Params p = new BOLTMAP_OBJ_INTERFACE.Pack_Params();
                        if (p.TryParse(infodata))
                        {
                            FirstBoltNo = p.boltNo1st;
                            Bolts.Clear();
                            foreach (var c in p.bolts)
                            {
                                Bolts.Add(
                                    new Range() { Begin = c.Begin, 
                                        End = c.End });
                            }
                        }
                        if (ActiveEvent != null) 
                        {
                            ActiveEvent(this);
                        }
                    } break;
                case BOLTMAP_OBJ_INTERFACE.GET_BORDERPARAMS:
                    {
                        BOLTMAP_OBJ_INTERFACE.Pack_BorderParams p = new BOLTMAP_OBJ_INTERFACE.Pack_BorderParams();
                        if (!p.TryParse(infodata))
                            break;


                        BorderType = p.bordertype;
                        BorderBoltNo_B = p.borderboltno_b;
                        BorderBoltNo_E = p.borderboltno_e;
                        Smooth = p.smooth;
                    } break;
                case BOLTMAP_OBJ_INTERFACE.GET_BOLTSINFO:
                    {
                        BOLTMAP_OBJ_INTERFACE.Pack_BoltsInfo p = new BOLTMAP_OBJ_INTERFACE.Pack_BoltsInfo();
                        if (!p.TryParse(infodata))
                            break;
                        LastBoltNo = p.LastBoltNo;
                        PosBegin = p.PosBegin;
                        PosEnd = p.PosEnd;
                        BoltInterval = p.BoltInterval;
                        BoltWidth = p.BoltWidth;
                    }break;

            }
        }
        public override void PushInfo(IFConn from, uint srcid, ushort infoid, byte[] infodata)
        {
            PushGetValue(from, srcid, infoid, infodata);
        }
    }
}
