using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FObjBase;
using Misc;
using System.ComponentModel;

namespace FLY.Thick.RemoteHistory
{
    public class RemoteHistory_OBJProxy: FObj
    {
        #region 延时推送 MARKNO
        const int MARKNO_PUSH_PARAMS = 1;
        const int MARKNO_PUSH_STATE = 2;
        #endregion

        IRemoteHistory mRemoteHistory;
        public RemoteHistory_OBJProxy(int objsys_idx, UInt32 objid, IRemoteHistory remotehistory)
            : base(objsys_idx)
        {
            mRemoteHistory = remotehistory;

            ID = objid;

            mRemoteHistory.PropertyChanged += new PropertyChangedEventHandler(mRemoteHistory_PropertyChanged);
        }

        void mRemoteHistory_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if ((e.PropertyName == "KeeyDay") ||
                (e.PropertyName == "SaveRows") ||
                (e.PropertyName == "MinSaveInterval"))
            {
                FObjBase.PollModule.Current.Poll_JustOnce(
                    new PollModule.PollHandler(delegate()
                    {
                        byte[] buf;
                        GetValue(null, 0, REMOTEHISTORY_OBJ_INTERFACE.GET_PARAMS, out buf);
                        CurrObjSys.PushObjInfoEx(
                            this, REMOTEHISTORY_OBJ_INTERFACE.PUSH_PARAMS,
                            buf);
                    }), this, MARKNO_PUSH_PARAMS);
            }
            else if (
                (e.PropertyName == "CurrDays")||
                (e.PropertyName == "CurrRows") ||
                (e.PropertyName == "CurrPath"))
            {
                FObjBase.PollModule.Current.Poll_JustOnce(
                    new PollModule.PollHandler(delegate()
                    {
                        byte[] buf;
                        GetValue(null, 0, REMOTEHISTORY_OBJ_INTERFACE.GET_STATE, out buf);
                        CurrObjSys.PushObjInfoEx(
                            this, REMOTEHISTORY_OBJ_INTERFACE.PUSH_STATE,
                            buf);
                    }), this, MARKNO_PUSH_STATE);
            }
        }

        public override void CallFunction(IFConn from, UInt32 srcid, UInt32 magic, UInt16 funcid, byte[] infodata)
        {
            switch (funcid) 
            {
                case REMOTEHISTORY_OBJ_INTERFACE.CALL_FLUSH:
                    {
                        mRemoteHistory.Flush();
                        return;
                    }
                    break;
                case REMOTEHISTORY_OBJ_INTERFACE.CALL_GETPATHS:
                    {
                        REMOTEHISTORY_OBJ_INTERFACE.Pack_GetPathsRequest p = new REMOTEHISTORY_OBJ_INTERFACE.Pack_GetPathsRequest();
                        if (!p.TryParse(infodata))
                            return;
                        mRemoteHistory.GetPaths(p.dt_begin, p.dt_end, p.profilename,
                            new GetPathsReponseHandler(delegate(List<string> paths, object state)
                                {
                                    ConnContext c = (ConnContext)state;
                                    CurrObjSys.PushCallFunctionEx(c.from, c.srcid, ID, c.magic,
                                        REMOTEHISTORY_OBJ_INTERFACE.CALL_GETPATHS,
                                        new REMOTEHISTORY_OBJ_INTERFACE.Pack_GetPathsResponse() { paths = paths }.ToBytes());
                                }),
                            new ConnContext(from, srcid, magic));
                        return;
                    }break;
                case REMOTEHISTORY_OBJ_INTERFACE.CALL_GETROOTPATH:
                    {
                        mRemoteHistory.GetRootPath(
                            new GetRootPathReponseHandler(delegate(string path, object state)
                            {
                                ConnContext c = (ConnContext)state;
                                CurrObjSys.PushCallFunctionEx(c.from, c.srcid, ID, c.magic,
                                    REMOTEHISTORY_OBJ_INTERFACE.CALL_GETROOTPATH,
                                    new REMOTEHISTORY_OBJ_INTERFACE.Pack_GetRootPathResponse() { rootpath = path }.ToBytes());
                            }),
                            new ConnContext(from, srcid, magic));
                        return;
                    }break;
            }
            return;
        }

        public override void GetValue(IFConn from, uint srcid, ushort memid, out byte[] infodata)
        {
            switch (memid)
            {
                case  REMOTEHISTORY_OBJ_INTERFACE.GET_PARAMS:
                    {
                        REMOTEHISTORY_OBJ_INTERFACE.Pack_Params p = new REMOTEHISTORY_OBJ_INTERFACE.Pack_Params()
                        {
                            keeyday = mRemoteHistory.KeeyDay,
                            saverows = mRemoteHistory.SaveRows,
                            minSaveInterval = mRemoteHistory.MinSaveInterval
                        };
                        infodata = p.ToBytes();
                    } break;
                case REMOTEHISTORY_OBJ_INTERFACE.GET_STATE:
                    {
                        REMOTEHISTORY_OBJ_INTERFACE.Pack_State p = new REMOTEHISTORY_OBJ_INTERFACE.Pack_State()
                        {
                            currdays = mRemoteHistory.CurrDays,
                            currrows = mRemoteHistory.CurrRows,
                            currpath = mRemoteHistory.CurrPath
                        };
                        infodata = p.ToBytes();
                    }break;
                default:
                    infodata = null;
                    break;
            }
        }

        public override void SetValue(IFConn from, uint srcid, ushort memid, byte[] infodata)
        {
            switch (memid)
            {
                case REMOTEHISTORY_OBJ_INTERFACE.SET_PARAMS:
                    {
                        REMOTEHISTORY_OBJ_INTERFACE.Pack_Params p = new REMOTEHISTORY_OBJ_INTERFACE.Pack_Params();
                        if (p.TryParse(infodata))
                        {
                            mRemoteHistory.KeeyDay = p.keeyday;
                            mRemoteHistory.SaveRows = p.saverows;
                            mRemoteHistory.MinSaveInterval = p.minSaveInterval; 
                        }
                    } break;
            }
        }
    }
}
