using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FObjBase;
using FLY.Thick.Base.OBJ_INTERFACE;
using FLY.Thick.Base.IService;

namespace FLY.Thick.Base.Server.OBJProxy
{
    public class Curve_OBJProxy : FObj
    {
        #region 延时推送 MARKNO
        const int MARKNO_PUSH_CURVELIST = 1;
        #endregion

        ICurveService mCurveService;



        public Curve_OBJProxy(int objsys_idx, UInt32 id, ICurveService CurveService) :base(objsys_idx)
        {
            ID = id;
            mCurveService = CurveService;
            mCurveService.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(mCurveService_PropertyChanged);

        }

        void mCurveService_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            FObjBase.PollModule.Current.Poll_JustOnce(
                new PollModule.PollHandler(delegate()
                {
                    byte[] buf;
                    GetValue(null, 0, CURVE_OBJ_INTERFACE.GET_CURVELIST, out buf);
                    CurrObjSys.PushObjInfoEx(
                        this, CURVE_OBJ_INTERFACE.PUSH_CURVELIST,
                        buf);
                }), this, MARKNO_PUSH_CURVELIST);
        }

        void Curves_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            FObjBase.PollModule.Current.Poll_JustOnce(
                new PollModule.PollHandler(delegate()
                {
                    byte[] buf;
                    GetValue(null, 0, CURVE_OBJ_INTERFACE.GET_CURVELIST, out buf);
                    CurrObjSys.PushObjInfoEx(
                        this, CURVE_OBJ_INTERFACE.PUSH_CURVELIST,
                        buf);
                }), this, MARKNO_PUSH_CURVELIST);
        }



        public override void GetValue(IFConn from, uint srcid, ushort memid, out byte[] infodata)
        {
            switch (memid)
            {
                case CURVE_OBJ_INTERFACE.GET_CURVELIST:
                    {
                        var p = new CURVE_OBJ_INTERFACE.Pack_CurveList()
                        {
                            correctway = mCurveService.CorrectWay,
                            flag = mCurveService.Flag,
                            list = mCurveService.Curves
                        };
                        string json = Newtonsoft.Json.JsonConvert.SerializeObject(p);
                        infodata = Misc.Converter.StringToBytes(json);
                    } break;
                default:
                    infodata = null;
                    break;
            }
        }
        public override void SetValue(IFConn from, uint srcid, ushort memid, byte[] infodata)
        {
            switch (memid)
            {
                case CURVE_OBJ_INTERFACE.SET_CURVELIST:
                    {
                        string json = Misc.Converter.BytesToString(infodata);

                        var p = Newtonsoft.Json.JsonConvert.DeserializeObject<CURVE_OBJ_INTERFACE.Pack_CurveList>(json);
                        
                        mCurveService.CorrectWay = p.correctway;
                        mCurveService.Flag = p.flag;
                        mCurveService.Curves = p.list;
                        mCurveService.Apply();
                        
                    } break;
            }
        }

    }
}
