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

namespace FLY.Thick.Base.Server.OBJProxy
{
    public class DynArea_OBJProxy : FObj
    {
        #region 延时推送 MARKNO
        const int MARKNO_PUSH_SCANINFO = 1;
        const int MARKNO_PUSH_ADPOS = 2;
        const int MARKNO_PUSH_IO = 3;
        const int MARKNO_PUSH_SYSTEM = 4;
        const int MARKNO_PUSH_PROFILE = 5;
        const int MARKNO_PUSH_BOLTMAP = 6;
        const int MARKNO_PUSH_CONTROLLER_STATE = 7;
        const int MARKNO_PUSH_POSITION2 = 8;
        #endregion
        DynArea mDynArea;
        public DynArea_OBJProxy(int objsys_idx,UInt32 id, DynArea dynarea):base(objsys_idx) 
        {
            ID = id;
            mDynArea = dynarea;
            mDynArea.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(mDynArea_PropertyChanged);
        }

        void OrgAD_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Replace) 
            {
                FObjBase.PollModule.Current.Poll_JustOnce(
                    new PollModule.PollHandler(delegate()
                    {
                        byte[] buf;
                        GetValue(null, 0, DYNAREA_OBJ_INTERFACE.GET_SCANINFO, out buf);
                        CurrObjSys.PushObjInfoEx(
                            this, DYNAREA_OBJ_INTERFACE.PUSH_SCANINFO,
                            buf);
                    }), this, MARKNO_PUSH_SCANINFO);
            }
        }

        void mDynArea_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if ((e.PropertyName == "Thick") ||
                (e.PropertyName == "Position") ||
                (e.PropertyName == "BoltIndex") ||
                (e.PropertyName == "BoltNo") ||
                (e.PropertyName == "AD"))
            {
                FObjBase.PollModule.Current.Poll_JustOnce(
                    new PollModule.PollHandler(delegate()
                    {
                        byte[] buf;
                        GetValue(null, 0, DYNAREA_OBJ_INTERFACE.GET_ADPOS, out buf);
                        CurrObjSys.PushObjInfoEx(
                            this, DYNAREA_OBJ_INTERFACE.PUSH_ADPOS,
                            buf);
                    }), this, MARKNO_PUSH_ADPOS);
            }
            else if (
                (e.PropertyName == "TempState") ||
                (e.PropertyName == "Mix") ||
                (e.PropertyName == "DataValid") ||
                (e.PropertyName == "Direction") ||
                (e.PropertyName == "Width") ||
                (e.PropertyName == "FilmVelocity")||
                (e.PropertyName == "FilmPosition") ||
                (e.PropertyName == "Velocity")
                )
            {
                FObjBase.PollModule.Current.Poll_JustOnce(
                    new PollModule.PollHandler(delegate()
                    {
                        byte[] buf;
                        GetValue(null, 0, DYNAREA_OBJ_INTERFACE.GET_SCANINFO, out buf);
                        CurrObjSys.PushObjInfoEx(
                            this, DYNAREA_OBJ_INTERFACE.PUSH_SCANINFO,
                            buf);
                    }), this, MARKNO_PUSH_SCANINFO);
            }
            else if ((e.PropertyName == "IStatus") ||
                (e.PropertyName == "OStatus"))
            {

                FObjBase.PollModule.Current.Poll_JustOnce(
                    new PollModule.PollHandler(delegate()
                    {
                        byte[] buf;
                        GetValue(null, 0, DYNAREA_OBJ_INTERFACE.GET_IO, out buf);
                        CurrObjSys.PushObjInfoEx(
                            this, DYNAREA_OBJ_INTERFACE.PUSH_IO,
                            buf);
                    }), this, MARKNO_PUSH_IO);
            }

            else if ((e.PropertyName == "FLYADIsConnect") ||
                (e.PropertyName == "SecuteLock") ||
                (e.PropertyName == "Version")||
                (e.PropertyName == "Hrs")||
                (e.PropertyName == "GageType"))
            {
                FObjBase.PollModule.Current.Poll_JustOnce(
                    new PollModule.PollHandler(delegate()
                    {
                        byte[] buf;
                        GetValue(null, 0, DYNAREA_OBJ_INTERFACE.GET_SYSTEM, out buf);
                        CurrObjSys.PushObjInfoEx(
                            this, DYNAREA_OBJ_INTERFACE.PUSH_SYSTEM,
                            buf);
                    }), this, MARKNO_PUSH_SYSTEM);
            }
            else if ((e.PropertyName == "ProductName") ||
                (e.PropertyName == "Target") ||
                (e.PropertyName == "Alarm") ||
                (e.PropertyName == "Comp") ||
                (e.PropertyName == "Shift") ||
                (e.PropertyName == "DataBoltNoBegin") ||
                (e.PropertyName == "DataBoltNoEnd") ||
                (e.PropertyName == "ScanBoltNoBegin") ||
                (e.PropertyName == "ScanBoltNoEnd"))
            {
                FObjBase.PollModule.Current.Poll_JustOnce(
                    new PollModule.PollHandler(delegate()
                    {
                        byte[] buf;
                        GetValue(null, 0, DYNAREA_OBJ_INTERFACE.GET_PROFILE, out buf);
                        CurrObjSys.PushObjInfoEx(
                            this, DYNAREA_OBJ_INTERFACE.PUSH_PROFILE,
                            buf);
                    }), this, MARKNO_PUSH_PROFILE);
            }
            else if ((e.PropertyName == "NBolts") ||
                (e.PropertyName == "FirstBoltNo"))
            {

                FObjBase.PollModule.Current.Poll_JustOnce(
                    new PollModule.PollHandler(delegate()
                    {
                        byte[] buf;
                        GetValue(null, 0, DYNAREA_OBJ_INTERFACE.GET_BOLTMAP, out buf);
                        CurrObjSys.PushObjInfoEx(
                            this, DYNAREA_OBJ_INTERFACE.PUSH_BOLTMAP,
                            buf);
                    }), this, MARKNO_PUSH_BOLTMAP);
            }
            else if ((e.PropertyName == "ControllerState")||
                (e.PropertyName == "AutoScanCounter"))
            {

                FObjBase.PollModule.Current.Poll_JustOnce(
                    new PollModule.PollHandler(delegate()
                    {
                        byte[] buf;
                        GetValue(null, 0, DYNAREA_OBJ_INTERFACE.GET_CONTROLLER_STATE, out buf);
                        CurrObjSys.PushObjInfoEx(
                            this, DYNAREA_OBJ_INTERFACE.PUSH_CONTROLLER_STATE,
                            buf);
                    }), this, MARKNO_PUSH_CONTROLLER_STATE);
            }
            else if (e.PropertyName == "Position2")
            {
                FObjBase.PollModule.Current.Poll_JustOnce(
                    new PollModule.PollHandler(delegate()
                    {
                        byte[] buf;
                        GetValue(null, 0, DYNAREA_OBJ_INTERFACE.GET_POSITION2, out buf);
                        CurrObjSys.PushObjInfoEx(
                            this, DYNAREA_OBJ_INTERFACE.PUSH_POSITION2,
                            buf);
                    }), this, MARKNO_PUSH_POSITION2);
            }
        }
        

        #region IFObj 成员

        public override void GetValue(IFConn from, uint srcid, ushort memid, out byte[] infodata)
        {
            switch (memid)
            {
                case DYNAREA_OBJ_INTERFACE.GET_ADPOS:
                    {
                        DYNAREA_OBJ_INTERFACE.Pack_GetADPos p = new DYNAREA_OBJ_INTERFACE.Pack_GetADPos()
                        {
                            AD = mDynArea.AD,
                            Thick = mDynArea.Thick,
                            Position = mDynArea.Position,
                            BoltIndex = mDynArea.BoltIndex
                        };
                        string json = JsonConvert.SerializeObject(p);
                        infodata = Misc.Converter.StringToBytes(json);
                    } break;
                case DYNAREA_OBJ_INTERFACE.GET_SCANINFO:
                    {
                        DYNAREA_OBJ_INTERFACE.Pack_GetScanInfo p = new DYNAREA_OBJ_INTERFACE.Pack_GetScanInfo()
                        {
                            Direction = mDynArea.Direction,
                            DataValid = mDynArea.DataValid,
                            Width = mDynArea.Width,
                            FilmVelocity = mDynArea.FilmVelocity,
                            FilmPosition = mDynArea.FilmPosition,
                            Velocity = mDynArea.Velocity,
                            SampleAD = mDynArea.SampleAD
                        };
                        string json = JsonConvert.SerializeObject(p);
                        infodata = Misc.Converter.StringToBytes(json);
                    } break;
                case DYNAREA_OBJ_INTERFACE.GET_PROFILE:
                    {
                        DYNAREA_OBJ_INTERFACE.Pack_GetProfile p = new DYNAREA_OBJ_INTERFACE.Pack_GetProfile()
                        {
                            ProductName = mDynArea.ProductName,
                            Target = mDynArea.Target,
                            Alarm = mDynArea.Alarm,
                            DataBoltNoBegin = mDynArea.DataBoltNoBegin,
                            DataBoltNoEnd = mDynArea.DataBoltNoEnd,
                            ScanBoltNoBegin = mDynArea.ScanBoltNoBegin,
                            ScanBoltNoEnd = mDynArea.ScanBoltNoEnd,
                            Comp = mDynArea.Comp,
                            Shift = mDynArea.Shift
                        };
                        string json = JsonConvert.SerializeObject(p);
                        infodata = Misc.Converter.StringToBytes(json);
                    } break;
                case DYNAREA_OBJ_INTERFACE.GET_BOLTMAP:
                    {
                        DYNAREA_OBJ_INTERFACE.Pack_GetBoltmap p = new DYNAREA_OBJ_INTERFACE.Pack_GetBoltmap()
                        {
                            NBolts = mDynArea.NBolts,
                            FirstBoltNo = mDynArea.FirstBoltNo
                        };
                        string json = JsonConvert.SerializeObject(p);
                        infodata = Misc.Converter.StringToBytes(json);
                    } break;
                case DYNAREA_OBJ_INTERFACE.GET_IO:
                    {
                        DYNAREA_OBJ_INTERFACE.Pack_GetIO p = new DYNAREA_OBJ_INTERFACE.Pack_GetIO()
                        {
                            IStatus = mDynArea.IStatus,
                            OStatus = mDynArea.OStatus
                        };
                        string json = JsonConvert.SerializeObject(p);
                        infodata = Misc.Converter.StringToBytes(json);
                    } break;

                case DYNAREA_OBJ_INTERFACE.GET_CONTROLLER_STATE:
                    {
                        DYNAREA_OBJ_INTERFACE.Pack_GetControllerState p = new DYNAREA_OBJ_INTERFACE.Pack_GetControllerState()
                        {
                            ControllerState = mDynArea.ControllerState,
                            AutoScanCounter = mDynArea.AutoScanCounter
                        };
                        string json = JsonConvert.SerializeObject(p);
                        infodata = Misc.Converter.StringToBytes(json);
                    } break;
                case DYNAREA_OBJ_INTERFACE.GET_SYSTEM:
                    {
                        DYNAREA_OBJ_INTERFACE.Pack_GetSystem p = new DYNAREA_OBJ_INTERFACE.Pack_GetSystem()
                        {
                            FLYADIsConnect = mDynArea.FLYADIsConnect,
                            SecuteLock = mDynArea.SecuteLock,
                            Hrs = mDynArea.Hrs
                        };
                        string json = JsonConvert.SerializeObject(p);
                        infodata = Misc.Converter.StringToBytes(json);
                    } break;
                case DYNAREA_OBJ_INTERFACE.GET_POSITION2:
                    {
                        DYNAREA_OBJ_INTERFACE.Pack_Position2 p = new DYNAREA_OBJ_INTERFACE.Pack_Position2()
                        {
                            Position2 = mDynArea.Position2
                        };
                        string json = JsonConvert.SerializeObject(p);
                        infodata = Misc.Converter.StringToBytes(json);
                    } break;
                default:
                    infodata = null;
                    break;
            }
        }

        #endregion


    }
}
