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


using System.ComponentModel;
using FObjBase;
using FLY.Thick.Base.IService;
using FLY.Thick.Base.OBJ_INTERFACE;
namespace FLY.Thick.Base.Server.OBJProxy
{
    public class GetSample_OBJProxy : FObj
    {
        #region 延时推送 MARKNO
        const int MARKNO_PUSH_PARAMS = 1;
        const int MARKNO_PUSH_STATE = 2;
        #endregion

        IGetSampleService mGetSampleService;

        public GetSample_OBJProxy(int objsys_idx, UInt32 id, IGetSampleService getSampleService) :base(objsys_idx)
        {
            ID = id;
            mGetSampleService = getSampleService;


            mGetSampleService.PropertyChanged += new PropertyChangedEventHandler(mGetSampleService_PropertyChanged);
            for(int i=0;i<mGetSampleService.Samples.Length;i++)
            {
                mGetSampleService.Samples[i].PropertyChanged += new PropertyChangedEventHandler(mSampleCell_PropertyChanged);
            }
            for (int i = 0; i < mGetSampleService.Features.Length; i++)
            {
                mGetSampleService.Features[i].PropertyChanged += new PropertyChangedEventHandler(mSampleFeature_PropertyChanged);
            }
        }
        void push_params() 
        {
            FObjBase.PollModule.Current.Poll_JustOnce(
                new PollModule.PollHandler(delegate()
                {
                    byte[] buf;
                    GetValue(null, 0, GETSAMPLE_OBJ_INTERFACE.GET_PARAMS, out buf);
                    CurrObjSys.PushObjInfoEx(
                        this, GETSAMPLE_OBJ_INTERFACE.PUSH_PARAMS,
                        buf);
                }), this, MARKNO_PUSH_PARAMS);
        }
        void push_state()
        {
            FObjBase.PollModule.Current.Poll_JustOnce(
                new PollModule.PollHandler(delegate()
                {
                    byte[] buf;
                    GetValue(null, 0, GETSAMPLE_OBJ_INTERFACE.GET_STATE, out buf);
                    CurrObjSys.PushObjInfoEx(
                        this, GETSAMPLE_OBJ_INTERFACE.PUSH_STATE,
                        buf);
                }), this, MARKNO_PUSH_STATE);
        }
        void mSampleFeature_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if ((e.PropertyName == "Enable") ||
                (e.PropertyName == "StartPos") ||
                (e.PropertyName == "EndPos"))
            {
                push_params();
            }
            else if ((e.PropertyName == "MaxRelevancy") ||
                (e.PropertyName == "MaxOffset") ||
                (e.PropertyName == "ScanData"))
            {
                push_state();
            }
        }
        void mSampleCell_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if ((e.PropertyName == "Enable") ||
                (e.PropertyName == "JustForCheck") ||
                (e.PropertyName == "OrgAD") ||
                (e.PropertyName == "Position")) 
            {
                push_params();
            }
            else if ((e.PropertyName == "AD") ||
                (e.PropertyName == "SampleValue"))
            {
                push_state();
            }
        }

        void mGetSampleService_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if ((e.PropertyName == "Enable") ||
                (e.PropertyName == "Interval") ||
                (e.PropertyName == "Velocity") ||
                (e.PropertyName == "Range") ||
                (e.PropertyName == "Window") ||
                (e.PropertyName == "IsCheckByPercent") ||
                (e.PropertyName == "ErrPercent") ||
                (e.PropertyName == "ErrValue") ||
                (e.PropertyName == "Search"))
            {
                push_params();
            }
            else if (e.PropertyName == "PosOfGrid") 
            {
                push_state();
            }
        }

        public override void GetValue(IFConn from, uint srcid, ushort memid, out byte[] infodata)
        {
            infodata = null;
            switch (memid)
            {
                case GETSAMPLE_OBJ_INTERFACE.GET_PARAMS:
                    {
                        GETSAMPLE_OBJ_INTERFACE.Pack_Params p = new GETSAMPLE_OBJ_INTERFACE.Pack_Params()
                        {
                            enable = mGetSampleService.Enable,
                            range = mGetSampleService.Range,
                            velocity = mGetSampleService.Velocity,
                            window = mGetSampleService.Window,
                            IsCheckByPercent = mGetSampleService.IsCheckByPercent,
                            ErrPercent = mGetSampleService.ErrPercent,
                            ErrValue = mGetSampleService.ErrValue,
                            search = mGetSampleService.Search
                        };

                        p.samples = (from sample in mGetSampleService.Samples
                                     select
                                         new GETSAMPLE_OBJ_INTERFACE.Pack_Params_SampleCell()
                                         {
                                             enable = sample.Enable,
                                             justForCheck = sample.JustForCheck,
                                             orgad = sample.OrgAD,
                                             position = sample.Position
                                         });

                        p.features = (from feature in mGetSampleService.Features
                                      select
                                          new GETSAMPLE_OBJ_INTERFACE.Pack_Params_SampleFeature()
                                          {
                                              enable = feature.Enable,
                                              startpos = feature.StartPos,
                                              endpos = feature.EndPos
                                          });
                       
                        string json = Newtonsoft.Json.JsonConvert.SerializeObject(p);
                        infodata = Misc.Converter.StringToBytes(json);
                    } break;
                case GETSAMPLE_OBJ_INTERFACE.GET_STATE:
                    {
                        GETSAMPLE_OBJ_INTERFACE.Pack_State p = new GETSAMPLE_OBJ_INTERFACE.Pack_State()
                        {
                           posOfGrid = mGetSampleService.PosOfGrid,
                            samples = from sample in mGetSampleService.Samples
                                    select
                                        new GETSAMPLE_OBJ_INTERFACE.Pack_State_SampleCell()
                                        {
                                            ad = sample.AD,
                                            value = sample.SampleValue
                                        },
                            features = from feature in mGetSampleService.Features
                                        select
                                            new GETSAMPLE_OBJ_INTERFACE.Pack_State_SampleFeature()
                                            {
                                                maxRelevancy = feature.MaxRelevancy,
                                                maxOffset = feature.MaxOffset,
                                                scanData = feature.ScanData
                                            }
                        };
                        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 GETSAMPLE_OBJ_INTERFACE.SET_PARAMS:
                    {
                        string json = Misc.Converter.BytesToString(infodata);
                        var p = Newtonsoft.Json.JsonConvert.DeserializeObject<GETSAMPLE_OBJ_INTERFACE.Pack_Params>(json);

                        mGetSampleService.Enable = p.enable;
                        mGetSampleService.Range = p.range;
                        mGetSampleService.Velocity = p.velocity;
                        mGetSampleService.Window = p.window;
                        mGetSampleService.IsCheckByPercent = p.IsCheckByPercent;
                        mGetSampleService.ErrPercent = p.ErrPercent;
                        mGetSampleService.ErrValue = p.ErrValue;
                        mGetSampleService.Search = p.search;
                        
                        for(int i=0;i<mGetSampleService.Samples.Count() && i<p.samples.Count();i++)
                        {
                            var sample_src = p.samples.ElementAt(i);
                            var sample_desp = mGetSampleService.Samples[i];

                            sample_desp.Enable = sample_src.enable;
                            sample_desp.JustForCheck = sample_src.justForCheck;
                            sample_desp.OrgAD = sample_src.orgad;
                            sample_desp.Position = sample_src.position;
                        }
                        for (int i = 0; i < mGetSampleService.Features.Count() && i < p.features.Count(); i++)
                        {
                            var feature_src = p.features.ElementAt(i);
                            var feature_desp = mGetSampleService.Features[i];


                            feature_desp.Enable = feature_src.enable;
                            feature_desp.StartPos = feature_src.startpos;
                            feature_desp.EndPos = feature_src.endpos;
                        }
                        mGetSampleService.Apply();
                        
                    } break;
            }
        }

        public override void CallFunction(IFConn from, uint srcid, uint magic, ushort funcid, byte[] infodata)
        {
            switch (funcid) {
                case GETSAMPLE_OBJ_INTERFACE.CALL_GET_TEMPDATAS:
                    {
                        mGetSampleService.GetTempFilterDatas((context,retdata) =>
                        {
                            string json = Newtonsoft.Json.JsonConvert.SerializeObject(retdata);
                            
                            CurrObjSys.PushCallFunctionEx(from, srcid, ID, magic, funcid, Misc.Converter.StringToBytes(json));
                        }, null);
                    }break;
            }
        }
    }
}
