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 GageInfo_OBJProxy : FObj
    {
        #region 延时推送 MARKNO
        protected const int MARKNO_PUSH_STATE = 1;
        protected const int MARKNO_PUSH_DATA = 2;
        #endregion

        protected IGageInfoService mGageInfoService;



        public GageInfo_OBJProxy(int objsys_idx, UInt32 id, IGageInfoService gageInfoService)
            : base(objsys_idx)
        {
            ID = id;
            mGageInfoService = gageInfoService;

            mGageInfoService.PropertyChanged += new PropertyChangedEventHandler(mGageInfoService_PropertyChanged);
        }

        protected virtual void mGageInfoService_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if ((e.PropertyName == "Progress") ||
                (e.PropertyName == "IsRunning") ||
                (e.PropertyName == "DataOK"))
            {
                push_state();
            }
            else if ((e.PropertyName == "Data"))
            {
                push_data();
            }
        }
        void push_state() 
        {
            FObjBase.PollModule.Current.Poll_JustOnce(
                new PollModule.PollHandler(delegate()
                {
                    byte[] buf;
                    GetValue(null, 0, GAGEINFO_OBJ_INTERFACE.GET_STATE, out buf);
                    CurrObjSys.PushObjInfoEx(
                        this, GAGEINFO_OBJ_INTERFACE.PUSH_STATE,
                        buf);
                }), this, MARKNO_PUSH_STATE);
        }
        protected virtual void push_data()
        {
            FObjBase.PollModule.Current.Poll_JustOnce(
                new PollModule.PollHandler(delegate()
                {
                    byte[] buf;
                    GetValue(null, 0, GAGEINFO_OBJ_INTERFACE.GET_DATA, out buf);
                    CurrObjSys.PushObjInfoEx(
                        this, GAGEINFO_OBJ_INTERFACE.PUSH_DATA,
                        buf);
                }), this, MARKNO_PUSH_DATA);
        }

        public override void GetValue(IFConn from, uint srcid, ushort memid, out byte[] infodata)
        {
            infodata = null;
            switch (memid)
            {
                case GAGEINFO_OBJ_INTERFACE.GET_STATE:
                    {
                        GAGEINFO_OBJ_INTERFACE.Pack_State p = new GAGEINFO_OBJ_INTERFACE.Pack_State()
                        {
                            progress = mGageInfoService.Progress,
                            ing = mGageInfoService.IsRunning,
                            dataOk = mGageInfoService.DataOK
                        };

                        infodata = p.ToBytes();
                    }break;
                case GAGEINFO_OBJ_INTERFACE.GET_DATA:
                    {
                        GAGEINFO_OBJ_INTERFACE.Pack_Data p = new GAGEINFO_OBJ_INTERFACE.Pack_Data()
                        {
                            posLen = mGageInfoService.PosLen,
                            posOfGrid = mGageInfoService.PosOfGrid,
                            forwData = mGageInfoService.ForwData.ToArray(),
                            backwData = mGageInfoService.BackwData.ToArray()
                        };

                        infodata = p.ToBytes();
                    }break;
                default:
                    infodata = null;
                    break;
            }
        }
        public override void SetValue(IFConn from, uint srcid, ushort memid, byte[] infodata)
        {

        }
        public override void CallFunction(IFConn from, uint srcid, uint magic, ushort funcid, byte[] infodata)
        {
            switch (funcid) 
            {
                case GAGEINFO_OBJ_INTERFACE.CALL_START:
                    {
                        mGageInfoService.Start();
                    }break;
                case GAGEINFO_OBJ_INTERFACE.CALL_STOP: 
                    {
                        mGageInfoService.Stop();
                    }break;
            }
        }
    }
}
