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;
using FLY.Thick.Base.Common;

namespace FLY.Thick.Base.Client
{
    public class PasswordServiceClient: FObjServiceClient, IPasswordService
    {

        public PasswordServiceClient(UInt32 serviceId) : base(serviceId) { }

        public PasswordServiceClient(UInt32 serviceId, string connName) : base(serviceId, connName) { }
        #region IPasswordService 成员
        /// <summary>
        /// 密码列表。 储存在 服务器
        /// </summary>
        [PropertyChanged.DoNotCheckEquality]
        public List<PasswordCell> PWs { get; set; } = new List<PasswordCell>();

        [PropertyChanged.DoNotCheckEquality]
        public List<UiLvCell> UiLvs { get; set; } = new List<UiLvCell>();
        public void Apply()
        {
            PASSWORD_OBJ_INTERFACE.Pack_Params p = new PASSWORD_OBJ_INTERFACE.Pack_Params()
            {
                PWs = PWs,
                UiLvs = UiLvs
            };

            string json = Newtonsoft.Json.JsonConvert.SerializeObject(p);

            CurrObjSys.SetValueEx(
               mConn, mServerID, ID, 
               PASSWORD_OBJ_INTERFACE.SET_PWLIST,
               Misc.Converter.StringToBytes(json)
               );    
        }

        #endregion


        public override void ConnectNotify(IFConn from)
        {
            base.ConnectNotify(from);
            if (from.IsConnected)
            {
                CurrObjSys.SenseConfigEx(
                    mConn, mServerID, ID, 0xffffffff, SENSE_CONFIG.ADD);
                CurrObjSys.GetValueEx(
                    mConn, mServerID, ID, 
                    PASSWORD_OBJ_INTERFACE.GET_PWLIST);
            }
        }
        public override void PushGetValue(IFConn from, uint srcid, ushort memid, byte[] infodata)
        {
            PushInfo(from, srcid, memid, infodata);
        }
        public override void PushInfo(IFConn from, uint srcid, ushort infoid, byte[] infodata)
        {
            switch (infoid)
            {
                case PASSWORD_OBJ_INTERFACE.PUSH_PWLIST:
                    {
                        string json = Misc.Converter.BytesToString(infodata);

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