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:FObj, IPasswordService
    {
        UInt32 mServerID;
        IFConn mConn;
        public PasswordServiceClient(UInt32 id)
        {
            mServerID = id;
        }

        #region IPasswordService 成员
        private List<PasswordCell> pws = new List<PasswordCell>();
        /// <summary>
        /// 密码列表。 储存在 服务器
        /// </summary>
        public List<PasswordCell> PWs
        {
            get
            {
                return pws;
            }
        }
        public void Apply()
        {
            PASSWORD_OBJ_INTERFACE.Pack_List p = new PASSWORD_OBJ_INTERFACE.Pack_List();
            p.list = PWs;

            CurrObjSys.SetValueEx(
               mConn, mServerID, ID, 
               PASSWORD_OBJ_INTERFACE.SET_PWLIST,
               p.ToBytes()
               );    
        }

        public AUTHORIZE_RESULT Authorize(string pw, int level)
        {
            if (pws.Count() == 0)
            { 
                //没有密码
                return AUTHORIZE_RESULT.OK;
            }

            var v = from p in pws where p.Password == pw select p;
            if (v.Count() > 0) 
            {
                if (v.First().Level >= level)
                    return AUTHORIZE_RESULT.OK;
                else
                    return AUTHORIZE_RESULT.ERR_LEVEL;
            }
            return AUTHORIZE_RESULT.ERR_PW;
        }
        #endregion


        public override void Dispose()
        {
            CurrObjSys.ObjRemove(
                this, mConn);
        }
        public override void ConnectNotify(IFConn from)
        {
            mConn = 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:
                    {
                        PASSWORD_OBJ_INTERFACE.Pack_List p = new PASSWORD_OBJ_INTERFACE.Pack_List();
                        p.list = PWs;
                        if(!p.TryParse(infodata))
                            return;
                        NotifyPropertyChanged("PWs");
                    } break;
            }
        }
        
        #region INotifyPropertyChanged 成员

        public event PropertyChangedEventHandler PropertyChanged;

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