using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FObjBase
{
    /// <summary>
    /// FObj 客户端
    /// </summary>
    public class FObjServiceClient : FObj,INotifyPropertyChanged
    {
        /// <summary>
        /// 服务Id
        /// </summary>
        protected UInt32 mServerID;
        /// <summary>
        /// 连接器
        /// </summary>
        protected IFConn mConn;

        /// <summary>
        /// 连接名称
        /// </summary>
        public string ConnName;
        /// <summary>
        /// 连接成功
        /// </summary>
        public bool IsConnected { get; set; }

        /// <summary>
        /// 使用 服务Id初始化， 还需要 Connect_to_Another_OBJSys
        /// </summary>
        /// <param name="id"></param>
        public FObjServiceClient(UInt32 id)
        {
            mServerID = id;
        }

        /// <summary>
        ///  使用全局地址字典初始化, 不需要再 手动 Connect_to_Another_OBJSys
        /// </summary>
        /// <param name="serviceId"></param>
        /// <param name="connName"></param>
        public FObjServiceClient(UInt32 serviceId, string connName) : this(serviceId)
        {
            ConnName = connName;
            FObjServiceClientManager.Instance.Connect_to_Another_OBJSys(connName, this);
        }

        public event PropertyChangedEventHandler PropertyChanged;
        protected void NotifyPropertyChanged(string propertyName) 
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
        /// <summary>
        /// 连接通知
        /// </summary>
        /// <param name="from"></param>
        public override void ConnectNotify(IFConn from)
        {
            mConn = from;
            IsConnected = from.IsConnected;
        }

        /// <summary>
        /// 注销
        /// </summary>
        public override void Dispose()
        {
            if (!string.IsNullOrEmpty(ConnName))
                FObjServiceClientManager.Instance.ObjClientDisponse(this);
            
            CurrObjSys.ObjRemove(
                this, mConn);
        }

        public virtual UInt32[] GetIDs() 
        {
            return new UInt32[] { ID };
        }
    }
}
