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 : FObj, IWarningService
    {
        IFConn mConn;
        UInt32 mServerID;

        #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; } }

        public event PropertyChangedEventHandler PropertyChanged;
        #endregion
        public WarningServiceClient(UInt32 serverid)
        {
            mServerID = serverid;
            this.PropertyChanged += WarningService_PropertyChanged;

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

            newestlist = new BufferServiceClient<FlyData_WarningHistory>(serverid+2);
        }
        public 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>();
            p.Add(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)
        {
            mConn = 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;
            }
        }
    }
}
