using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.ComponentModel;
using System.Collections.ObjectModel;

using FObjBase;
using FLY.Thick.Base.IService;
using FLY.Thick.Base.OBJ_INTERFACE;
using FLY.Thick.Base.Common;

namespace FLY.Thick.Base.Client
{
    public class WarningService : FObj, IWarningService
    {
        IFConn mConn;
        UInt32 mServerID;

        public WarningService()
        {
            mServerID = WARNING_OBJ_INTERFACE.ID;

        }

        #region IWarningService
        int outIdxOfWarning;
        public int OutIdxOfWarning
        {
            get
            {
                return outIdxOfWarning;
            }
            set
            {
                if (outIdxOfWarning != value) 
                {
                    outIdxOfWarning = value;
                    NotifyPropertyChanged("OutIdxOfWarning");
                }
            }
        }
        int duration = 5;
        public int Duration
        {
            get
            {
                return duration;
            }
            set
            {
                if (duration != value)
                {
                    duration = value;
                    NotifyPropertyChanged("Duration");
                }
            }
        }

        bool enable = false;
        public bool Enable
        {
            get
            {
                return enable;
            }
            set
            {
                if (enable != value)
                {
                    enable = value;
                    NotifyPropertyChanged("Enable");
                }
            }
        }
        bool ensave = false;
        public bool EnSave
        {
            get
            {
                return ensave;
            }
            set
            {
                if (ensave != value)
                {
                    ensave = value;
                    NotifyPropertyChanged("EnSave");
                }
            }
        }

        private ObservableCollection<int> whitelist = new ObservableCollection<int>();
        public ObservableCollection<int> WhiteList
        {
            get { return whitelist; }
        }
        private ObservableCollection<int> blacklist = new ObservableCollection<int>();
        public ObservableCollection<int> BlackList
        {
            get { return blacklist; }
        }


        private int ReasonListSize = 40;
        private ObservableCollection<Common.FlyData_WarningHistory> reasonlist = new ObservableCollection<Common.FlyData_WarningHistory>();
        public ObservableCollection<Common.FlyData_WarningHistory> ReasonList
        {
            get { return reasonlist; }
        }

        #endregion

        #region INotifyPropertyChanged 成员

        public event PropertyChangedEventHandler PropertyChanged;
        protected void NotifyPropertyChanged(string propertyname)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyname));
            }
        }
        #endregion

        public void Apply() 
        {
            WARNING_OBJ_INTERFACE.Pack_Params p = new WARNING_OBJ_INTERFACE.Pack_Params();
            p.outIdxOfWarning = OutIdxOfWarning;
            p.duration = Duration;
            p.enable = Enable;
            p.enSave = EnSave;
            p.whitelist = WhiteList.ToArray();
            p.blacklist = BlackList.ToArray();


            CurrObjSys.SetValueEx(
                mConn, mServerID, ID, 
                WARNING_OBJ_INTERFACE.SET_PARAMS,
                p.ToBytes()
                );
        }
        public void Reset() 
        {
            CurrObjSys.CallFunctionEx(
                mConn, mServerID, ID,
                WARNING_OBJ_INTERFACE.CALL_RESET,
                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.GetValueEx(
                    mConn, mServerID, ID, 
            WARNING_OBJ_INTERFACE.GET_REASONLIST);

                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:
                    {
                        WARNING_OBJ_INTERFACE.Pack_Params p = new WARNING_OBJ_INTERFACE.Pack_Params();
                        if (!p.TryParse(infodata))
                            return;
                        OutIdxOfWarning = p.outIdxOfWarning;
                        Duration = p.duration;
                        Enable = p.enable;
                        EnSave = p.enSave;
                        WhiteList.Clear();

                        for (int i = 0; i < p.whitelist.Count(); i++)
                        {
                            WhiteList.Add(p.whitelist[i]);
                        }
                        BlackList.Clear();
                        for (int i = 0; i < p.blacklist.Count(); i++)
                        {
                            BlackList.Add(p.blacklist[i]);
                        }
                    } break;
                case WARNING_OBJ_INTERFACE.GET_REASONLIST:
                    {
                        WARNING_OBJ_INTERFACE.Pack_ReasonList p = new WARNING_OBJ_INTERFACE.Pack_ReasonList();
                        if (!p.TryParse(infodata))
                            return;
                        ReasonList.Clear();
                        for (int i = 0; i < p.list.Count(); i++)
                            ReasonList.Add(p.list[i]);
                    }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;
                case WARNING_OBJ_INTERFACE.GET_REASONLIST:
                    {
                        WARNING_OBJ_INTERFACE.Pack_ReasonList p = new WARNING_OBJ_INTERFACE.Pack_ReasonList();
                        if (!p.TryParse(infodata))
                            return;
                        if(p.list.Count()==0)
                            ReasonList.Clear();

                        for (int i = 0; i < p.list.Count(); i++)
                            ReasonList.Add(p.list[i]);

                        if (ReasonList.Count() > ReasonListSize) 
                        {
                            int cnt = ReasonList.Count() - ReasonListSize;
                            for (int i = 0; i < cnt; i++)
                                ReasonList.RemoveAt(0);
                        }
                    } break;
            }
        }
    }

    public class WarningServiceSimple : FObj, IWarningServiceSimple
    {
        IFConn mConn;
        UInt32 mServerID;

        public WarningServiceSimple()
        {
            mServerID = WARNING_OBJ_INTERFACE.ID;

        }

        DateTime errtime = DateTime.MinValue;
        /// <summary>
        /// 当前报警 发生的时间
        /// </summary>
        public DateTime ErrTime
        {
            get
            {
                return errtime;
            }
            private set
            {
                if (errtime != value)
                {
                    errtime = value;
                    NotifyPropertyChanged("ErrTime");
                }
            }
        }

        bool enable = false;
        public bool Enable
        {
            get
            {
                return enable;
            }
            protected set
            {
                if (enable != value)
                {
                    enable = value;
                    NotifyPropertyChanged("Enable");
                }
            }
        }
        byte errtype = 0;
        /// <summary>
        /// 当前报警类型  0，没报警
        /// </summary>
        public byte ErrType
        {
            get
            {
                return errtype;
            }
            private set
            {
                if (errtype != value)
                {
                    errtype = value;
                    NotifyPropertyChanged("ErrType");
                }
            }
        }
        byte errcode = 0;
        /// <summary>
        /// 报警编码
        /// </summary>
        public byte ErrCode
        {
            get
            {
                return errcode;
            }
            private set
            {
                if (errcode != value)
                {
                    errcode = value;
                    NotifyPropertyChanged("ErrCode");
                }
            }
        }
        string errdescription = "";
        /// <summary>
        /// 报警描述
        /// </summary>
        public string ErrDescription
        {
            get
            {
                return errdescription;
            }
            private set
            {
                if (errdescription != value)
                {
                    errdescription = value;
                    NotifyPropertyChanged("ErrDescription");
                }
            }
        }


        public void Add(byte errtype, byte errcode, string description, ERR_STATE state)
        {
            CurrObjSys.CallFunctionEx(
                mConn, mServerID, ID,
                WARNING_OBJ_INTERFACE.CALL_ADD,
                new FlyData_WarningHistory() { ErrType = errtype, ErrCode = errcode, State = state, Description = description }.ToBytes());
        }
        public void Quiet() 
        {
            CurrObjSys.CallFunctionEx(
                mConn, mServerID, ID,
                WARNING_OBJ_INTERFACE.CALL_QUIET,
                null);
        }
        #region INotifyPropertyChanged 成员

        public event PropertyChangedEventHandler PropertyChanged;
        protected void NotifyPropertyChanged(string propertyname)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyname));
            }
        }
        #endregion

        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_CURR);

                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_CURR:
                    {
                        FlyData_WarningHistory p = new FlyData_WarningHistory();
                        int cnt;
                        if (!p.TryParse(infodata,0,out cnt ))
                            return;
                        ErrType = p.ErrType;
                        ErrTime = p.Time;
                        ErrCode = p.ErrCode;
                        ErrDescription = p.Description;
                       
                    } break;
                case WARNING_OBJ_INTERFACE.GET_PARAMS:
                    {
                        WARNING_OBJ_INTERFACE.Pack_Params p = new WARNING_OBJ_INTERFACE.Pack_Params();
                        if (!p.TryParse(infodata))
                            return;
                        Enable = p.enable;
                    }break;
            }
        }
        public override void PushInfo(IFConn from, uint srcid, ushort infoid, byte[] infodata)
        {
            PushGetValue(from, srcid, infoid, infodata);
        }
    }
}
