using FLY.OBJComponents.Common;
using FLY.OBJComponents.OBJ_INTERFACE;
using FObjBase;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FLY.OBJComponents.Server.OBJProxy
{
    public class WarningSystem_OBJProxy : FObj
    {
        #region 延时推送 MARKNO
        const int MARKNO_PUSH_PARAMS = 1;
        const int MARKNO_PUSH_REASONLIST = 2;
        const int MARKNO_PUSH_NEWESTLIST = 3;
        #endregion
        WarningSystem mWarningSystem;
        Buffer_OBJProxy<FlyData_WarningHistory> mReasonListOBJProxy;
        Buffer_OBJProxy<FlyData_WarningHistory> mNewestListOBJProxy;

        /// <summary>
        /// 使用id，id+1，id+2
        /// </summary>
        /// <param name="objsys_idx"></param>
        /// <param name="id"></param>
        /// <param name="warningSystem"></param>
        public WarningSystem_OBJProxy(int objsys_idx, uint id, WarningSystem warningSystem)
            : base(objsys_idx)
        {
            ID = id;
            mWarningSystem = warningSystem;
            mWarningSystem.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(mWarningSystem_PropertyChanged);

            mReasonListOBJProxy = new Buffer_OBJProxy<FlyData_WarningHistory>(objsys_idx, id+1, mWarningSystem.ReasonList);
            mNewestListOBJProxy = new Buffer_OBJProxy<FlyData_WarningHistory>(objsys_idx, id+2, mWarningSystem.NewestList);
        }
        void mWarningSystem_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            Dictionary<string, object> p = new Dictionary<string, object>();
            p.Add(e.PropertyName, Misc.PropertiesManager.GetValue(mWarningSystem, e.PropertyName));

            string json = JsonConvert.SerializeObject(p);
            CurrObjSys.PushObjInfoEx(
                this, WARNING_OBJ_INTERFACE.PUSH_PARAMS,
                Misc.Converter.StringToBytes(json)
                );
        }
        public override void GetValue(IFConn from, uint srcid, ushort memid, out byte[] infodata)
        {
            switch (memid)
            {
                case WARNING_OBJ_INTERFACE.GET_PARAMS:
                    {
                        Dictionary<string, object> p = new Dictionary<string, object>();
                        //IEnumerable<string> propertynames = Misc.PropertiesManager.GetAllPropertyNames(mWarningSystem);
                        string[] propertynames = mWarningSystem.GetPropertys();
                        foreach (string pn in propertynames)
                        {
                            p.Add(pn, Misc.PropertiesManager.GetValue(mWarningSystem, pn));
                        }
                        string json = JsonConvert.SerializeObject(p);
                        infodata = Misc.Converter.StringToBytes(json);
                    }
                    break;
                default:
                    infodata = null;
                    break;
            }
        }
        public override void SetValue(IFConn from, uint srcid, ushort memid, byte[] infodata)
        {
            switch (memid)
            {
                case WARNING_OBJ_INTERFACE.SET_PARAMS:
                    {
                        
                        string json = Misc.Converter.BytesToString(infodata);

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

                        foreach (KeyValuePair<string, object> kv in datas)
                        {
                            PropertiesManager_JSON.SetValue(this, kv.Key, kv.Value);
                        }

                        return;
                    }
                    break;
            }
        }

        public override void CallFunction(IFConn from, uint srcid, uint magic, ushort funcid, byte[] infodata)
        {
            switch (funcid)
            {
                case WARNING_OBJ_INTERFACE.CALL_RESET:
                    mWarningSystem.Reset();
                    break;
                case WARNING_OBJ_INTERFACE.CALL_SILENCE:
                    mWarningSystem.Silence();
                    break;
            }
        }
    }
}
