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

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

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 CoatingSegmentServiceClient : FObj, ICoatingSegmentService
    {
        IFConn mConn;
        UInt32 mServerID;

        public CoatingSegmentServiceClient()
        {
            mServerID = COATINGSEGMENT_OBJ_INTERFACE.ID;
            Last.Time = DateTime.Now;
            Last.PartCount = 1;
            Last.CoatingPart.Add(new CoatingSegmentPart() { Distance = 0, Total = 0 });
        }

        #region ISegmentService
        private bool enable = false;
        public bool Enable
        {
            get { return enable; }
            set
            {
                if (enable != value)
                {
                    enable = value;
                    NotifyPropertyChanged("Enable");
                }
            }
        }
        private bool ensave = false;
        public bool EnSave
        {
            get
            {
                return ensave;
            }
            set
            {
                if (ensave != value)
                {
                    ensave = value;
                    NotifyPropertyChanged("EnSave");
                }
            }
        }
        private int savecnt = 500;
        public int SaveCnt
        {
            get
            {
                return savecnt;
            }
            set
            {
                if (savecnt != value)
                {
                    savecnt = value;
                    NotifyPropertyChanged("SaveCnt");
                }
            }
        }
        private int recentCnt = 20;
        /// <summary>
        /// 列表的行数
        /// </summary>
        public int RecentCnt
        {
            get { return recentCnt; }
            set
            {
                if (recentCnt != value)
                {
                    recentCnt = value;
                    NotifyPropertyChanged("RecentCnt");
                }
            }
        }
        private int inidxofvside = 11;
        public int InIdxOfVSide
        {
            get
            {
                return inidxofvside;
            }
            set
            {
                if (inidxofvside != value)
                {
                    inidxofvside = value;
                    NotifyPropertyChanged("InIdxOfVSide");
                }
            }
        }

        private double warningtol_total;
        public double WarningTol_Total
        {
            get
            {
                return warningtol_total;
            }
            set
            {
                if (warningtol_total != value)
                {
                    warningtol_total = value;
                    NotifyPropertyChanged("WarningTol_Total");
                }
            }
        }
        private double warningtol_distance;
        public double WarningTol_Distance
        {
            get
            {
                return warningtol_distance;
            }
            set
            {
                if (warningtol_distance != value)
                {
                    warningtol_distance = value;
                    NotifyPropertyChanged("WarningTol_Distance");
                }
            }
        }


        ObservableCollection<FlyData_CoatingSegmentHistory> m_coatingPartList = new ObservableCollection<FlyData_CoatingSegmentHistory>();
        public ObservableCollection<FlyData_CoatingSegmentHistory> Recent
        {
            get { return m_coatingPartList; }
        }
        FlyData_CoatingSegmentHistory last = new FlyData_CoatingSegmentHistory();
        public FlyData_CoatingSegmentHistory Last 
        {
            get {
                return last;
            }
        }
        #endregion
        #region 工艺
        CoatingSegmentProfile profile = new CoatingSegmentProfile();
        public CoatingSegmentProfile Profile { get { return profile; } }
        #endregion
        public void Apply() 
        {
            COATINGSEGMENT_OBJ_INTERFACE.Pack_Params p = new COATINGSEGMENT_OBJ_INTERFACE.Pack_Params();
            p.enable = Enable;
            p.enSave = EnSave;
            p.saveCnt = SaveCnt;
            p.inIdxOfVSide = InIdxOfVSide;
            p.warningtol_distance = WarningTol_Distance;
            p.warningtol_total = WarningTol_Total;
            CurrObjSys.SetValueEx(mConn, mServerID, ID,
                COATINGSEGMENT_OBJ_INTERFACE.SET_PARAMS,
                p.ToBytes());
        }
        public void ProfileApply() 
        {
            CurrObjSys.SetValueEx(mConn, mServerID, ID,
                COATINGSEGMENT_OBJ_INTERFACE.SET_PROFILE,
                Profile.ToBytes());
        }


        #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,
                    COATINGSEGMENT_OBJ_INTERFACE.GET_PARAMS);

                CurrObjSys.GetValueEx(
                    mConn, mServerID, ID,
                    COATINGSEGMENT_OBJ_INTERFACE.GET_PROFILE);

                CurrObjSys.GetValueEx(
                    mConn, mServerID, ID,
                    COATINGSEGMENT_OBJ_INTERFACE.GET_LAST);

                CurrObjSys.CallFunctionEx(
                    mConn, mServerID, ID,
                    COATINGSEGMENT_OBJ_INTERFACE.CALL_GETRECENT, null);

                CurrObjSys.SenseConfigEx(
                    mConn, mServerID, ID,
                    0xffffffff, SENSE_CONFIG.ADD);
            }
        }
        public override void PushGetValue(IFConn from, uint srcid, ushort memid, byte[] infodata)
        {
            switch (memid)
            {
                case COATINGSEGMENT_OBJ_INTERFACE.GET_PARAMS:
                    {
                        COATINGSEGMENT_OBJ_INTERFACE.Pack_Params p = new COATINGSEGMENT_OBJ_INTERFACE.Pack_Params();
                        if (!p.TryParse(infodata))
                            return;

                        Enable = p.enable;
                        EnSave = p.enSave;
                        SaveCnt = p.saveCnt;
                        RecentCnt = p.recentCnt;
                        InIdxOfVSide = p.inIdxOfVSide;
                        WarningTol_Distance = p.warningtol_distance;
                        WarningTol_Total = p.warningtol_total;
                    } break;
                case COATINGSEGMENT_OBJ_INTERFACE.GET_PROFILE:
                    {
                        int count;
                        if (!Profile.TryParse(infodata, 0, out count))
                            return;
                    } break;
                case COATINGSEGMENT_OBJ_INTERFACE.GET_LAST:
                    {
                        int count;
                        if (!Last.TryParse(infodata, 0, out count))
                            return;
                        if (Recent.Count > 0) 
                        {
                            if (Last.Time > Recent.Last().Time) 
                            {
                                ToSaveList(Last);
                            }
                        }
                    }break;
            }
        }
        public override void PushInfo(IFConn from, uint srcid, ushort infoid, byte[] infodata)
        {
            PushGetValue(from, srcid, infoid, infodata);
        }
        public override void CallFunction(IFConn from, uint srcid, uint magic, ushort funcid, byte[] infodata)
        {
            switch (funcid)
            {
                case COATINGSEGMENT_OBJ_INTERFACE.CALL_GETRECENT:
                    {
                        COATINGSEGMENT_OBJ_INTERFACE.Pack_Recent r = new COATINGSEGMENT_OBJ_INTERFACE.Pack_Recent();
                        if (!r.TryParse(infodata))
                            return;

                        Recent.Clear();
                        for (int i = 0; i < r.data.Count(); i++)
                            ToSaveList(r.data[i]);
                    } break;
            }
        }
        //压入数据列
        void ToSaveList(FlyData_CoatingSegmentHistory coatingpart) 
        {
            Recent.Add(coatingpart);
            if (Recent.Count > RecentCnt)
                Recent.RemoveAt(0);
        }
    }
}
