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

namespace FLY.Thick.RemoteHistory
{
    public class Buffer_OBJProxy<T> : FObj
        where T : IFlyData, IPack2, new()
    {
        #region 延时推送 MARKNO
        const int MARKNO_PUSH_PARAMS = 1;
        #endregion

        IBuffer<T> buffer;

        public Buffer_OBJProxy(int objsys_idx, IBuffer<T> buffer, UInt32 id)
            : base(objsys_idx)
        {
            ID = id;
            this.buffer = buffer;
            buffer.RecordChanged += Buffer_RecordChanged;
            buffer.PropertyChanged += Buffer_PropertyChanged;
        }


        private void Buffer_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (
                (e.PropertyName == "Size"))
            {
                FObjBase.PollModule.Current.Poll_JustOnce(
                    new PollModule.PollHandler(delegate()
                    {
                        byte[] buf;
                        GetValue(null, 0, BUFFER_OBJ_INTERFACE.GET_PARAMS, out buf);
                        CurrObjSys.PushObjInfoEx(
                            this, BUFFER_OBJ_INTERFACE.PUSH_PARAMS,
                            buf);
                    }), this, MARKNO_PUSH_PARAMS);
            }
        }

        private void Buffer_RecordChanged(object sender, BufferChangedEventArgs e)
        {
            BUFFER_OBJ_INTERFACE.Pack_List<T> p = new BUFFER_OBJ_INTERFACE.Pack_List<T>();
            p.isAdd = e.IsAdd;

            if (e.Cnt > 0)
            {
                p.list = new T[e.Cnt];
                for (int i = 0; i < p.list.Count(); i++)
                {
                    int index = buffer.Record.Count() - p.list.Count() + i;
                    p.list[i] = buffer.Record[index];
                }
            }

            CurrObjSys.PushObjInfoEx(
                this, BUFFER_OBJ_INTERFACE.PUSH_LIST,
                p.ToBytes());
        }

        public override void GetValue(IFConn from, uint srcid, ushort memid, out byte[] infodata)
        {
            switch (memid)
            {
                case BUFFER_OBJ_INTERFACE.GET_PARAMS:
                    {
                        BUFFER_OBJ_INTERFACE.Pack_Params p = new BUFFER_OBJ_INTERFACE.Pack_Params()
                        {
                            size = buffer.Size
                        };
                        infodata = p.ToBytes();
                    }
                    break;
                case BUFFER_OBJ_INTERFACE.GET_LIST:
                    {
                        BUFFER_OBJ_INTERFACE.Pack_List<T> p = new BUFFER_OBJ_INTERFACE.Pack_List<T>()
                        {
                            isAdd = false,
                            list = buffer.Record.ToArray()
                        };
                        infodata = p.ToBytes();
                    }
                    break;
                default:
                    infodata = null;
                    break;
            }
        }

        public override void SetValue(IFConn from, uint srcid, ushort memid, byte[] infodata)
        {
            switch (memid)
            {
                case BUFFER_OBJ_INTERFACE.SET_PARAMS:
                    {
                        BUFFER_OBJ_INTERFACE.Pack_Params p = new BUFFER_OBJ_INTERFACE.Pack_Params();
                        if (!p.TryParse(infodata))
                            return;
                        buffer.Size = p.size;
                        return;
                    }
                    break;
            }
        }
    }
}
