using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FObjBase;
using FLY.Thick.Base.OBJ_INTERFACE;
using FLY.Thick.Base.Common;
using FLY.Thick.Base.IService;

namespace FLY.Thick.Base.Server.OBJProxy
{
    public class Password_OBJProxy : FObj
    {
        #region 延时推送 MARKNO
        const int PUSH_PARAMS = 1;
        #endregion

        IPasswordService mPassword;

        public Password_OBJProxy(int objsys_idx, UInt32 id, IPasswordService password)
            : base(objsys_idx)
        {
            ID = id;
            mPassword = password;
            mPassword.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(mPassword_PropertyChanged);
        }

        void mPassword_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            FObjBase.PollModule.Current.Poll_JustOnce(() =>
            {
                byte[] buf;
                GetValue(null, 0, PASSWORD_OBJ_INTERFACE.GET_PWLIST, out buf);
                CurrObjSys.PushObjInfoEx(
                    this, PASSWORD_OBJ_INTERFACE.PUSH_PWLIST,
                    buf);
            }, this, PUSH_PARAMS);
        }

        #region FObj 重载
        public override void GetValue(IFConn from, uint srcid, ushort memid, out byte[] infodata)
        {
            switch (memid)
            {
                case PASSWORD_OBJ_INTERFACE.GET_PWLIST:
                    {
                        PASSWORD_OBJ_INTERFACE.Pack_Params p = new PASSWORD_OBJ_INTERFACE.Pack_Params()
                        {
                            PWs = mPassword.PWs,
                            UiLvs = mPassword.UiLvs
                        };
                        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 PASSWORD_OBJ_INTERFACE.SET_PWLIST:
                    {
                        string json = Misc.Converter.BytesToString(infodata);

                        var p = Newtonsoft.Json.JsonConvert.DeserializeObject< PASSWORD_OBJ_INTERFACE.Pack_Params>(json);
                        mPassword.PWs = p.PWs;
                        mPassword.UiLvs = p.UiLvs;
                        mPassword.Apply();
                    } break;
            }
        }

        #endregion

    }
}
