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

namespace FLY.Thick.RemoteHistory
{
    public class RemoteHistorySerivceClient : FObj, IRemoteHistory
    {
        IFConn mConn = null;
        UInt32 mServerID;
        public bool IsConnected;
        #region IHistory<T> 成员

        private int currdays = 0;
        /// <summary>
        /// 当前保存的数据天数
        /// </summary>
        public int CurrDays
        {
            get { return currdays; }
            protected set
            {
                if (currdays != value)
                {
                    currdays = value;
                    NotifyPropertyChanged("CurrDays");
                }
            }
        }

        private int keeyday;
        /// <summary>
        /// 数据能保存的天数
        /// </summary>
        public int KeeyDay
        {
            get
            {
                return keeyday;
            }
            set
            {
                if (value <= 0)
                    value = 1;

                if (keeyday != value)
                {
                    keeyday = value;
                    NotifyPropertyChanged("KeeyDay");
                }
            }
        }

        private int saverows;
        /// <summary>
        /// 多少行保存一次
        /// </summary>
        public int SaveRows
        {
            get
            {
                return saverows;
            }
            set
            {
                if (saverows != value)
                {
                    saverows = value;
                    NotifyPropertyChanged("SaveRows");
                }
            }
        }

        private int currrows;
        /// <summary>
        /// 当前总行数
        /// </summary>
        public int CurrRows
        {
            get
            {
                return currrows;
            }
            protected set
            {
                if (currrows != value)
                {
                    currrows = value;
                    NotifyPropertyChanged("CurrRows");
                }
            }
        }

        private string currpath = null;
        public string CurrPath
        {
            get
            {
                return currpath;
            }
            protected set
            {
                if (currpath != value)
                {
                    currpath = value;
                    NotifyPropertyChanged("CurrPath");
                }
            }
        }

        #endregion

        public RemoteHistorySerivceClient(UInt32 serverid)
        {
            mServerID = serverid;

        }
        public void Apply() 
        {
            REMOTEHISTORY_OBJ_INTERFACE.Pack_Params p
                = new REMOTEHISTORY_OBJ_INTERFACE.Pack_Params()
            {
                keeyday = KeeyDay,
                saverows = SaveRows
            };
            CurrObjSys.SetValueEx(
                mConn, mServerID, ID, 
                REMOTEHISTORY_OBJ_INTERFACE.SET_PARAMS, p.ToBytes());
        }
        public void Flush()
        {
            CurrObjSys.CallFunctionEx(
                mConn, mServerID, ID, 
                REMOTEHISTORY_OBJ_INTERFACE.CALL_FLUSH, null);
        }
        public void GetPaths(DateTime dt_begin, DateTime dt_end, string profilename, GetPathsReponseHandler return_func, object state)
        {
            REMOTEHISTORY_OBJ_INTERFACE.Pack_GetPathsRequest p = new REMOTEHISTORY_OBJ_INTERFACE.Pack_GetPathsRequest()
            {
                dt_begin = dt_begin,
                dt_end = dt_end,
                profilename = profilename
            };
            CurrObjSys.CallFunctionEx(
                mConn, mServerID, ID,
                REMOTEHISTORY_OBJ_INTERFACE.CALL_GETPATHS, p.ToBytes(), return_func, state);
        }
        /// <summary>
        /// 获取根目录
        /// </summary>
        public void GetRootPath(GetRootPathReponseHandler return_func, object state) 
        {
            CurrObjSys.CallFunctionEx(
                mConn, mServerID, ID,
                REMOTEHISTORY_OBJ_INTERFACE.CALL_GETROOTPATH, null, return_func, state);
        }
        #region INotifyPropertyChanged 成员

        public event PropertyChangedEventHandler PropertyChanged;
        protected void NotifyPropertyChanged(string propertyname)
        {
            if (PropertyChanged != null)
                PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyname));
        }
        #endregion

        public override void Dispose()
        {
            //获取所有数据，设置推送
            CurrObjSys.ObjRemove(this, mConn);
        }
        public override void ConnectNotify(IFConn from)
        {
            mConn = from;
            IsConnected = from.IsConnected;
            if (from.IsConnected)
            {
                //获取所有数据，设置推送
                CurrObjSys.GetValueEx(
                    mConn, mServerID, ID, 
                    REMOTEHISTORY_OBJ_INTERFACE.GET_PARAMS);

                CurrObjSys.GetValueEx(
                    mConn, mServerID, ID, 
                    REMOTEHISTORY_OBJ_INTERFACE.GET_STATE);

                CurrObjSys.SenseConfigEx(
                    mConn, mServerID, ID, 0xffffffff,
                    SENSE_CONFIG.ADD);
            }
        }
        public override void PushGetValue(IFConn from, uint srcid, ushort memid, byte[] infodata)
        {
            switch (memid)
            {
                case REMOTEHISTORY_OBJ_INTERFACE.GET_PARAMS:
                    {
                        REMOTEHISTORY_OBJ_INTERFACE.Pack_Params p = new REMOTEHISTORY_OBJ_INTERFACE.Pack_Params();
                        if (p.TryParse(infodata))
                        {
                            KeeyDay = p.keeyday;
                            SaveRows = p.saverows;
                        }
                    } break;
                case REMOTEHISTORY_OBJ_INTERFACE.GET_STATE:
                    {
                        REMOTEHISTORY_OBJ_INTERFACE.Pack_State p = new REMOTEHISTORY_OBJ_INTERFACE.Pack_State();
                        if (p.TryParse(infodata))
                        {
                            CurrRows = p.currrows;
                            CurrPath = p.currpath;
                            CurrDays = p.currdays;
                        }
                    } break;
            }
        }
        public override void PushInfo(IFConn from, uint srcid, ushort infoid, byte[] infodata)
        {
            PushGetValue(from, srcid, infoid, infodata);
        }
        public override void PushCallFunction(IFConn from, uint srcid, UInt32 magic, ushort funcid, byte[] retdata, object AsyncDelegate, object AsyncState)
        {
            switch (funcid)
            {
                case REMOTEHISTORY_OBJ_INTERFACE.CALL_GETPATHS:
                    {
                        REMOTEHISTORY_OBJ_INTERFACE.Pack_GetPathsResponse p = new REMOTEHISTORY_OBJ_INTERFACE.Pack_GetPathsResponse();
                        if (!p.TryParse(retdata))
                            return;
                        GetPathsReponseHandler func = (GetPathsReponseHandler)AsyncDelegate;
                        func(p.paths, AsyncState);
                    }break;
                case REMOTEHISTORY_OBJ_INTERFACE.CALL_GETROOTPATH:
                    {
                        REMOTEHISTORY_OBJ_INTERFACE.Pack_GetRootPathResponse p = new REMOTEHISTORY_OBJ_INTERFACE.Pack_GetRootPathResponse();
                        if (!p.TryParse(retdata))
                            return;
                        GetRootPathReponseHandler func = (GetRootPathReponseHandler)AsyncDelegate;
                        func(p.rootpath, AsyncState);
                    }break;
            }
        }
    }
}
