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 == "OnePointIsOffset") ||
                (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,
                            interval = mGetSampleService.Interval,
                            range = mGetSampleService.Range,
                            velocity = mGetSampleService.Velocity,
                            window = mGetSampleService.Window,
                            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
                                         }).ToArray();

                        p.features = (from feature in mGetSampleService.Features
                                       select
                                           new GETSAMPLE_OBJ_INTERFACE.Pack_Params_SampleFeature()
                                           {
                                               enable = feature.Enable,
                                               startpos = feature.StartPos,
                                               endpos = feature.EndPos
                                           }).ToArray(); 
                        infodata = p.ToBytes();
                    } break;
                case GETSAMPLE_OBJ_INTERFACE.GET_STATE:
                    {
                        GETSAMPLE_OBJ_INTERFACE.Pack_State p = new GETSAMPLE_OBJ_INTERFACE.Pack_State()
                        {
                           posOfGrid = mGetSampleService.PosOfGrid
                        };

                        p.samples = (from sample in mGetSampleService.Samples
                                     select
                                         new GETSAMPLE_OBJ_INTERFACE.Pack_State_SampleCell()
                                         {
                                             ad = sample.AD,
                                             value = sample.SampleValue
                                         }).ToArray();

                        p.features = (from feature in mGetSampleService.Features
                                      select
                                          new GETSAMPLE_OBJ_INTERFACE.Pack_State_SampleFeature()
                                          {
                                              maxRelevancy = feature.MaxRelevancy,
                                              maxOffset = feature.MaxOffset,
                                              scanData = feature.ScanData
                                          }).ToArray();
                        infodata = p.ToBytes();
                    }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:
                    {
                        GETSAMPLE_OBJ_INTERFACE.Pack_Params p = new GETSAMPLE_OBJ_INTERFACE.Pack_Params();
                        if(p.TryParse(infodata))
                        {
                        
                            mGetSampleService.Enable = p.enable;
                            mGetSampleService.Interval = p.interval;
                            mGetSampleService.Range = p.range;
                            mGetSampleService.Velocity = p.velocity;
                            mGetSampleService.Window = p.window;
                            mGetSampleService.Search = p.search;
                        
                            for(int i=0;i<mGetSampleService.Samples.Count() && i<p.samples.Count();i++)
                            {
                                mGetSampleService.Samples[i].Enable = p.samples[i].enable;
                                mGetSampleService.Samples[i].JustForCheck = p.samples[i].justForCheck;
                                mGetSampleService.Samples[i].OrgAD = p.samples[i].orgad;
                                mGetSampleService.Samples[i].Position = p.samples[i].position;
                            }
                            for (int i = 0; i < mGetSampleService.Features.Count() && i < p.features.Count(); i++)
                            {
                                mGetSampleService.Features[i].Enable = p.features[i].enable;
                                mGetSampleService.Features[i].StartPos = p.features[i].startpos;
                                mGetSampleService.Features[i].EndPos = p.features[i].endpos;
                            }
                            mGetSampleService.Apply();
                        }
                    } break;
            }
        }
    }
}
