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

using FObjBase;
using FLY.OBJComponents.Common;
using FLY.OBJComponents.IService;
using FLY.OBJComponents.OBJ_INTERFACE;
using Newtonsoft.Json;

namespace FLY.OBJComponents.Client
{
    public class WarningServiceClient : FObjServiceClient, IWarningService
    {

        #region IWarningService
        
        public bool Enable { get; set; }
        public bool IsRinging { get; protected set; }
        private BufferServiceClient<FlyData_WarningHistory> reasonlist;

        /// <summary>
        /// 当前正在报警的！！！！！！
        /// </summary>
        public IBuffer<FlyData_WarningHistory> ReasonList { get { return reasonlist; } }

        private BufferServiceClient<FlyData_WarningHistory> newestlist;

        /// <summary>
        /// 最近报警列表
        /// </summary>
        public IBuffer<FlyData_WarningHistory> NewestList { get { return newestlist; } }

        #endregion
        public WarningServiceClient(UInt32 serviceId) : base(serviceId) 
        {
            Init(serviceId);
        }
        public WarningServiceClient(UInt32 serviceId, string connName) : this(serviceId) 
        {
            FObjServiceClientManager.Instance.Connect_to_Another_OBJSys(connName, this);
        }
        void Init(UInt32 serviceId) 
        {
            this.PropertyChanged += WarningService_PropertyChanged;

            reasonlist = new BufferServiceClient<FlyData_WarningHistory>(serviceId + 1);

            newestlist = new BufferServiceClient<FlyData_WarningHistory>(serviceId + 2);
        }
        public override UInt32[] GetIDs()
        {
            return new UInt32[] { ID,
                reasonlist.ID,
                newestlist.ID
            };
        }
        private void WarningService_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            Dictionary<string, object> p = new Dictionary<string, object>
            {
                { e.PropertyName, Misc.PropertiesManager.GetValue(this, e.PropertyName) }
            };

            string json = JsonConvert.SerializeObject(p);
            CurrObjSys.PushObjInfoEx(
                this, WARNING_OBJ_INTERFACE.PUSH_PARAMS,
                Misc.Converter.StringToBytes(json)
                );
        }
        public void Reset()
        {
            CurrObjSys.CallFunctionEx(mConn, mServerID, ID,
                WARNING_OBJ_INTERFACE.CALL_RESET, null);
        }
        public void Silence()
        {
            CurrObjSys.CallFunctionEx(mConn, mServerID, ID,
                WARNING_OBJ_INTERFACE.CALL_SILENCE, null);
        }
        public override void Dispose()
        {
            CurrObjSys.ObjRemove(
                this, mConn);
        }
        public override void ConnectNotify(IFConn from)
        {
            base.ConnectNotify(from);
            if (from.IsConnected)
            {
                //获取所有数据，设置推送
                CurrObjSys.GetValueEx(
                    mConn, mServerID, ID,
                    WARNING_OBJ_INTERFACE.GET_PARAMS);

                CurrObjSys.SenseConfigEx(
                    mConn, mServerID, ID,
                    0xffffffff,
                    SENSE_CONFIG.ADD);
            }
        }
        public override void PushGetValue(IFConn from, uint srcid, ushort memid, byte[] infodata)
        {
            switch (memid)
            {
                case WARNING_OBJ_INTERFACE.GET_PARAMS:
                    {
                        string json = Misc.Converter.BytesToString(infodata);

                        Dictionary<string, object> datas = JsonConvert.DeserializeObject<Dictionary<string, object>>(json);

                        this.PropertyChanged -= WarningService_PropertyChanged;
                        foreach (KeyValuePair<string, object> kv in datas)
                        {
                            PropertiesManager_JSON.SetValue(this, kv.Key, kv.Value);
                        }
                        this.PropertyChanged += WarningService_PropertyChanged;
                    }
                    break;
            }
        }
        public override void PushInfo(IFConn from, uint srcid, ushort infoid, byte[] infodata)
        {
            switch (infoid)
            {
                case WARNING_OBJ_INTERFACE.GET_PARAMS:
                    PushGetValue(from, srcid, infoid, infodata);
                    break;
            }
        }
    }
}
