using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FObjBase;
using FLY.Thick.Base.IService;
using FLY.Thick.Base.OBJ_INTERFACE;
using FLY.Thick.Base.Common;
using FLY.Thick.Base.Server;
namespace FLY.Thick.Base.Server.OBJProxy
{
    public class CoatingSegmentSearch_OBJProxy : FObj
    {
        #region 延时推送 MARKNO
        const int MARKNO_PUSH_PARAMS = 1;
        const int MARKNO_PUSH_PROFILE = 2;
        const int MARKNO_PUSH_LAST = 3;
        #endregion

        CoatingSegmentSearch mCoatingSegmentService;
        FLY.Thick.RemoteHistory.RemoteHistory_OBJProxy mRemoteHistoryOBJProxy;
        public CoatingSegmentSearch_OBJProxy(int objsys_idx, CoatingSegmentSearch coatingSegmentService)
            : base(objsys_idx)
        {
            ID = COATINGSEGMENT_OBJ_INTERFACE.ID;
            mCoatingSegmentService = coatingSegmentService;

            mRemoteHistoryOBJProxy = new FLY.Thick.RemoteHistory.RemoteHistory_OBJProxy(objsys_idx, OBJ_INTERFACE_ID.CS_HISTORY_ID, mCoatingSegmentService.mHistory);


            mCoatingSegmentService.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(mCoatingSegmentService_PropertyChanged);
            mCoatingSegmentService.Profile.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(Profile_PropertyChanged);
            mCoatingSegmentService.Recent.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(Recent_CollectionChanged);
        }

        void Recent_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add)
            {
                CurrObjSys.PushObjInfoEx(
                    this, COATINGSEGMENT_OBJ_INTERFACE.PUSH_LAST,
                    mCoatingSegmentService.Recent.Last().ToBytes());
            }
        }

        void Profile_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            FObjBase.PollModule.Current.Poll_JustOnce(
                new PollModule.PollHandler(delegate()
                {
                    byte[] buf;
                    GetValue(null, 0, COATINGSEGMENT_OBJ_INTERFACE.GET_PROFILE, out buf);
                    CurrObjSys.PushObjInfoEx(
                        this, COATINGSEGMENT_OBJ_INTERFACE.PUSH_PROFILE,
                        buf);
                }), this, MARKNO_PUSH_PROFILE);
        }

        void mCoatingSegmentService_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (
                (e.PropertyName == "Enable") ||
                (e.PropertyName == "EnSave") ||
                (e.PropertyName == "SaveCnt") ||
                (e.PropertyName == "RecentCnt") ||
                (e.PropertyName == "InIdxOfVSide") ||
                (e.PropertyName == "Mmpr") ||
                (e.PropertyName == "WarningTol_Total") ||
                (e.PropertyName == "WarningTol_Distance")
                )
            {
                FObjBase.PollModule.Current.Poll_JustOnce(
                    new PollModule.PollHandler(delegate()
                    {
                        byte[] buf;
                        GetValue(null, 0, COATINGSEGMENT_OBJ_INTERFACE.GET_PARAMS, out buf);
                        CurrObjSys.PushObjInfoEx(
                            this, COATINGSEGMENT_OBJ_INTERFACE.PUSH_PARAMS,
                            buf);
                    }), this, MARKNO_PUSH_PARAMS);
            }
            else if(e.PropertyName == "Last")
            {
                byte[] buf;
                GetValue(null, 0, COATINGSEGMENT_OBJ_INTERFACE.GET_LAST, out buf);
                CurrObjSys.PushObjInfoEx(
                    this, COATINGSEGMENT_OBJ_INTERFACE.PUSH_LAST,
                    buf);
            }
                
        }

        public override void GetValue(IFConn from, uint srcid, ushort memid, out byte[] infodata)
        {
            switch (memid)
            {
                case COATINGSEGMENT_OBJ_INTERFACE.GET_PARAMS:
                    {
                        COATINGSEGMENT_OBJ_INTERFACE.Pack_Params p = new COATINGSEGMENT_OBJ_INTERFACE.Pack_Params()
                        {
                            enable = mCoatingSegmentService.Enable,
                            enSave = mCoatingSegmentService.EnSave,
                            saveCnt = mCoatingSegmentService.SaveCnt,
                            recentCnt = mCoatingSegmentService.RecentCnt,
                            inIdxOfVSide = mCoatingSegmentService.InIdxOfVSide,
                            warningtol_distance = mCoatingSegmentService.WarningTol_Distance,
                            warningtol_total = mCoatingSegmentService.WarningTol_Total
                        };
                        infodata = p.ToBytes();
                    } break;
                case COATINGSEGMENT_OBJ_INTERFACE.GET_PROFILE:
                    {
                        infodata = mCoatingSegmentService.Profile.ToBytes();
                    } break;
                case COATINGSEGMENT_OBJ_INTERFACE.GET_LAST:
                    {
                        if (mCoatingSegmentService.Last != null)
                        {
                            infodata = mCoatingSegmentService.Last.ToBytes();
                        }
                        else 
                        {
                            infodata = null;
                        }
                    }break;
                default:
                    infodata = null;
                    break;
            }
        }
        public override void SetValue(IFConn from, uint srcid, ushort memid, byte[] infodata)
        {
            switch (memid)
            {
                case COATINGSEGMENT_OBJ_INTERFACE.SET_PARAMS:
                    {
                        COATINGSEGMENT_OBJ_INTERFACE.Pack_Params p = new COATINGSEGMENT_OBJ_INTERFACE.Pack_Params();
                        if (!p.TryParse(infodata))
                            break;
                        mCoatingSegmentService.Enable = p.enable;
                        mCoatingSegmentService.EnSave = p.enSave;
                        mCoatingSegmentService.SaveCnt = p.saveCnt;
                        mCoatingSegmentService.RecentCnt = p.recentCnt;
                        mCoatingSegmentService.InIdxOfVSide = p.inIdxOfVSide;
                        mCoatingSegmentService.WarningTol_Distance = p.warningtol_distance;
                        mCoatingSegmentService.WarningTol_Total = p.warningtol_total;
                        mCoatingSegmentService.Apply();
                    } break;
                case COATINGSEGMENT_OBJ_INTERFACE.SET_PROFILE:
                    {
                        int count;
                        if (!mCoatingSegmentService.Profile.TryParse(infodata, 0, out count))
                            break;
                        mCoatingSegmentService.ProfileApply();
                    } break;
            }
        }
        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 p = new COATINGSEGMENT_OBJ_INTERFACE.Pack_Recent();
                        p.data = mCoatingSegmentService.Recent.ToArray();

                        this.CurrObjSys.PushCallFunctionEx(from, srcid, ID, magic, funcid, p.ToBytes());
                    }
                    break;
            }
        }
    }
}
