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

using System.ComponentModel;


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

namespace FLY.Thick.Base.Server.OBJProxy
{
    public class BoltMap_OBJProxy : FObj
    {
        #region 延时推送 MARKNO
        const int MARKNO_PUSH_PARAMS = 1;
        const int MARKNO_PUSH_BORDER = 2;
        const int MARKNO_PUSH_INFO = 3;
        #endregion

        IBoltMapService mBoltMapService;

        public BoltMap_OBJProxy(int objsys_idx, IBoltMapService boltMapService) :base(objsys_idx)
        {
            ID = BOLTMAP_OBJ_INTERFACE.ID;
            mBoltMapService = boltMapService;
            mBoltMapService.PropertyChanged += new PropertyChangedEventHandler(mBoltMapService_PropertyChanged);
            mBoltMapService.ActiveEvent += new ActiveEventHandler(mBoltMapService_ActiveEvent);
        }

        void mBoltMapService_ActiveEvent(object sender)
        {
            FObjBase.PollModule.Current.Poll_JustOnce(
                new PollModule.PollHandler(delegate()
                {
                    byte[] buf;
                    GetValue(null, 0, BOLTMAP_OBJ_INTERFACE.GET_PARAMS, out buf);
                    CurrObjSys.PushObjInfoEx(
                        this, BOLTMAP_OBJ_INTERFACE.PUSH_PARAMS,
                        buf);
                }), this, MARKNO_PUSH_PARAMS);
        }

        void mBoltMapService_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            //if ((e.PropertyName == "FirstBoltNo")||
            //    (e.PropertyName == "Bolts")
            //    )
            //{

            //}
            //else 
            if ((e.PropertyName == "BorderBoltNo_B") ||
                (e.PropertyName == "BorderBoltNo_E") ||
                (e.PropertyName == "BorderType") ||(e.PropertyName == "Smooth") )
            {
                FObjBase.PollModule.Current.Poll_JustOnce(
                    new PollModule.PollHandler(delegate()
                    {
                        byte[] buf;
                        GetValue(null, 0, BOLTMAP_OBJ_INTERFACE.GET_BORDERPARAMS, out buf);
                        CurrObjSys.PushObjInfoEx(
                            this, BOLTMAP_OBJ_INTERFACE.PUSH_BORDERPARAMS,
                            buf);
                    }), this, MARKNO_PUSH_BORDER);
            }
            else if ((e.PropertyName == "LastBoltNo") ||
                (e.PropertyName == "BoltInterval") ||
                (e.PropertyName == "BoltWidth") ||
                (e.PropertyName == "PosBegin") ||
                (e.PropertyName == "PosEnd"))
            {
                FObjBase.PollModule.Current.Poll_JustOnce(
                    new PollModule.PollHandler(delegate()
                    {
                        byte[] buf;
                        GetValue(null, 0, BOLTMAP_OBJ_INTERFACE.GET_BOLTSINFO, out buf);
                        CurrObjSys.PushObjInfoEx(
                            this, BOLTMAP_OBJ_INTERFACE.PUSH_BOLTSINFO,
                            buf);
                    }), this, MARKNO_PUSH_INFO);
            }
        }

        public override void GetValue(IFConn from, uint srcid, ushort memid, out byte[] infodata)
        {
            switch (memid)
            {
                case BOLTMAP_OBJ_INTERFACE.GET_PARAMS:
                    {
                        BOLTMAP_OBJ_INTERFACE.Pack_Params p = new BOLTMAP_OBJ_INTERFACE.Pack_Params()
                        {
                            boltNo1st = mBoltMapService.FirstBoltNo,
                            bolts = mBoltMapService.Bolts.ToArray(),
                        };

                        infodata = p.ToBytes();
                    } break;

                case BOLTMAP_OBJ_INTERFACE.GET_BORDERPARAMS:
                    {
                        BOLTMAP_OBJ_INTERFACE.Pack_BorderParams p = new BOLTMAP_OBJ_INTERFACE.Pack_BorderParams()
                        {
                            bordertype = mBoltMapService.BorderType,
                            borderboltno_b = mBoltMapService.BorderBoltNo_B,
                            borderboltno_e = mBoltMapService.BorderBoltNo_E,
                            smooth = mBoltMapService.Smooth
                        };

                        infodata = p.ToBytes();
                    } break;
                case BOLTMAP_OBJ_INTERFACE.GET_BOLTSINFO:
                    {
                        BOLTMAP_OBJ_INTERFACE.Pack_BoltsInfo p = new BOLTMAP_OBJ_INTERFACE.Pack_BoltsInfo()
                        {
                            LastBoltNo = mBoltMapService.LastBoltNo,
                            BoltInterval = mBoltMapService.BoltInterval,
                            BoltWidth = mBoltMapService.BoltWidth,
                            PosBegin = mBoltMapService.PosBegin,
                            PosEnd = mBoltMapService.PosEnd
                        };
                        infodata = p.ToBytes();
                    }break;
                default:
                    infodata = null;
                    break;
            }
        }
        public override void SetValue(IFConn from, uint srcid, ushort memid, byte[] infodata)
        {
            switch (memid)
            {
                case BOLTMAP_OBJ_INTERFACE.SET_PARAMS:
                    {
                        BOLTMAP_OBJ_INTERFACE.Pack_Params p = new BOLTMAP_OBJ_INTERFACE.Pack_Params();
                        if (p.TryParse(infodata))
                        {
                            mBoltMapService.FirstBoltNo = p.boltNo1st;
                            mBoltMapService.Bolts.Clear();

                            for(int i=0;i<p.bolts.Length;i++)
                            {
                                Range r = p.bolts[i];
                                mBoltMapService.Bolts.Add(
                                    new Range(){
                                        Begin = r.Begin,
                                        End = r.End});
                            }
                            
                            mBoltMapService.Apply();
                        }
                    } break;

                case BOLTMAP_OBJ_INTERFACE.SET_BORDERPARAMS:
                    {
                        BOLTMAP_OBJ_INTERFACE.Pack_BorderParams p = new BOLTMAP_OBJ_INTERFACE.Pack_BorderParams();
                        if (p.TryParse(infodata))
                        {
                            mBoltMapService.BorderType = p.bordertype;
                            mBoltMapService.BorderBoltNo_B = p.borderboltno_b;
                            mBoltMapService.BorderBoltNo_E = p.borderboltno_e;
                            mBoltMapService.Smooth = p.smooth;
                            mBoltMapService.ApplyBorder();
                        }
                    } break;
            }
        }

    }
}
