using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FObjBase;
using FLY.Thick.Base.OBJ_INTERFACE;
using FLY.Thick.Base.Common;
using FLY.Thick.Base.IService;
using FLY.Thick.Base.Server;

namespace FLY.Thick.Base.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_CURR = 3;
        #endregion
        WarningSystem mWarningSystem;
        int reasonlist_newcnt = 0;
        public WarningSystem_OBJProxy(int objsys_idx, WarningSystem warningSystem)
            : base(objsys_idx)
        {
            ID = WARNING_OBJ_INTERFACE.ID;
            mWarningSystem = warningSystem;
            mWarningSystem.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(mWarningSystem_PropertyChanged);
            mWarningSystem.ReasonList.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(ReasonList_CollectionChanged);
        }

        void ReasonList_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add) 
            {
                reasonlist_newcnt++;

                FObjBase.PollModule.Current.Poll_JustOnce(
                    new PollModule.PollHandler(delegate()
                    {
                        //reasonlist_newcnt
                        WARNING_OBJ_INTERFACE.Pack_ReasonList p = new WARNING_OBJ_INTERFACE.Pack_ReasonList();
                        if (reasonlist_newcnt > mWarningSystem.ReasonList.Count())
                            reasonlist_newcnt = mWarningSystem.ReasonList.Count();

                        p.list = new FlyData_WarningHistory[reasonlist_newcnt];
                        for (int i = 0; i < p.list.Count(); i++)
                        { 
                            int index = mWarningSystem.ReasonList.Count() - reasonlist_newcnt;
                            p.list[i] = mWarningSystem.ReasonList[index];
                        }
                        reasonlist_newcnt = 0;

                       
                        CurrObjSys.PushObjInfoEx(
                            this, WARNING_OBJ_INTERFACE.PUSH_REASONLIST,
                            p.ToBytes());
                    }), this, MARKNO_PUSH_REASONLIST);
            }
        }

        void mWarningSystem_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if ((e.PropertyName == "OutIdxOfWarning") ||
                (e.PropertyName == "Duration") ||
                (e.PropertyName == "Enable") ||
                (e.PropertyName == "EnSave") ||
                (e.PropertyName == "WhiteList") ||
                (e.PropertyName == "BlackList")
                )
            {
                FObjBase.PollModule.Current.Poll_JustOnce(
                    new PollModule.PollHandler(delegate()
                    {
                        byte[] buf;
                        GetValue(null, 0, WARNING_OBJ_INTERFACE.GET_PARAMS, out buf);
                        CurrObjSys.PushObjInfoEx(
                            this, WARNING_OBJ_INTERFACE.PUSH_PARAMS,
                            buf);
                    }), this, MARKNO_PUSH_PARAMS);
            }
            else if ((e.PropertyName == "ErrTime") ||
                (e.PropertyName == "ErrType") ||
                (e.PropertyName == "ErrCode") ||
                (e.PropertyName == "ErrDescription")) 
            {
                FObjBase.PollModule.Current.Poll_JustOnce(
                    new PollModule.PollHandler(delegate()
                    {
                        byte[] buf;
                        GetValue(null, 0, WARNING_OBJ_INTERFACE.GET_CURR, out buf);
                        CurrObjSys.PushObjInfoEx(
                            this, WARNING_OBJ_INTERFACE.PUSH_CURR,
                            buf);
                    }), this, MARKNO_PUSH_CURR);
            }

        }
        public override void CallFunction(IFConn from, uint srcid, UInt32 magic, ushort funcid, byte[] infodata)
        {
            switch (funcid)
            {
                case WARNING_OBJ_INTERFACE.CALL_RESET:
                    mWarningSystem.Reset();
                    break;
                case WARNING_OBJ_INTERFACE.CALL_ADD:
                    {
                        FlyData_WarningHistory p = new FlyData_WarningHistory();
                        int cnt;
                        if (p.TryParse(infodata, 0, out cnt)) 
                        {
                            mWarningSystem.Add(p.ErrType, p.ErrCode, p.Description, p.State);
                        }
                    }break;
                case WARNING_OBJ_INTERFACE.CALL_QUIET:
                    mWarningSystem.Quiet();
                    break;
            }
        }
        public override void GetValue(IFConn from, uint srcid, ushort memid, out byte[] infodata)
        {
            switch (memid)
            {
                case WARNING_OBJ_INTERFACE.GET_PARAMS:
                    {
                        WARNING_OBJ_INTERFACE.Pack_Params p = new WARNING_OBJ_INTERFACE.Pack_Params()
                        {
                            outIdxOfWarning =  mWarningSystem.OutIdxOfWarning,
                            duration =  mWarningSystem.Duration,
                            enable = mWarningSystem.Enable,
                            enSave = mWarningSystem.EnSave,
                            whitelist = mWarningSystem.WhiteList.ToArray(),
                            blacklist = mWarningSystem.BlackList.ToArray()
                        };

                        infodata = p.ToBytes();
                    } break;
                case WARNING_OBJ_INTERFACE.GET_REASONLIST:
                    {
                        WARNING_OBJ_INTERFACE.Pack_ReasonList p = new WARNING_OBJ_INTERFACE.Pack_ReasonList()
                        {
                            list = mWarningSystem.ReasonList.ToArray()
                        };
                        infodata = p.ToBytes();
                    } break;
                case WARNING_OBJ_INTERFACE.GET_CURR:
                    {
                        FlyData_WarningHistory p = new FlyData_WarningHistory(){
                            ErrType = mWarningSystem.ErrType,
                            ErrCode = mWarningSystem.ErrCode,
                            Time = mWarningSystem.ErrTime,
                            Description = mWarningSystem.ErrDescription
                        };
                        infodata = p.ToBytes();
                    }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:
                    {
                        WARNING_OBJ_INTERFACE.Pack_Params p = new WARNING_OBJ_INTERFACE.Pack_Params();
                        if (!p.TryParse(infodata))
                            return;
                        mWarningSystem.Enable = p.enable;
                        mWarningSystem.EnSave = p.enSave;
                        mWarningSystem.OutIdxOfWarning = p.outIdxOfWarning;
                        mWarningSystem.Duration = p.duration;
                        mWarningSystem.WhiteList.Clear();
                        for(int i=0;i<p.whitelist.Count();i++)
                            mWarningSystem.WhiteList.Add(p.whitelist[i]);

                        mWarningSystem.BlackList.Clear();
                        for(int i=0;i<p.blacklist.Count();i++)
                            mWarningSystem.BlackList.Add(p.blacklist[i]);
                        mWarningSystem.Apply();
                        return;
                    } break;
            }
        }
    }

}
