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

namespace FLY.Thick.Base.Client
{
    public class DynAreaServiceClient:FObj
    {
        public DynArea mDynArea;
        private UInt32 mServerID;
        private IFConn mConn;


        public DynAreaServiceClient(UInt32 id)
        {
            mServerID = id;
            mDynArea = new DynArea();
        }

        #region IFObj 成员
        public override void Dispose()
        {
            CurrObjSys.ObjRemove(
                this, mConn);
        }
        public override void ConnectNotify(IFConn from)
        {
            mDynArea.ServerIsConnected = from.IsConnected;
            mConn = from;

            if (from.IsConnected) 
            {
                CurrObjSys.GetValueEx(mConn, mServerID, ID, DYNAREA_OBJ_INTERFACE.GET_ADPOS);
                CurrObjSys.GetValueEx(mConn, mServerID, ID, DYNAREA_OBJ_INTERFACE.GET_SCANINFO);
                CurrObjSys.GetValueEx(mConn, mServerID, ID, DYNAREA_OBJ_INTERFACE.GET_CONTROLLER_STATE);
                CurrObjSys.GetValueEx(mConn, mServerID, ID, DYNAREA_OBJ_INTERFACE.GET_PROFILE);
                CurrObjSys.GetValueEx(mConn, mServerID, ID, DYNAREA_OBJ_INTERFACE.GET_BOLTMAP);
                CurrObjSys.GetValueEx(mConn, mServerID, ID, DYNAREA_OBJ_INTERFACE.GET_SYSTEM);
                CurrObjSys.GetValueEx(mConn, mServerID, ID, DYNAREA_OBJ_INTERFACE.GET_IO);
                CurrObjSys.GetValueEx(mConn, mServerID, ID, DYNAREA_OBJ_INTERFACE.GET_POSITION2);

                CurrObjSys.SenseConfigEx(mConn, mServerID, ID,0xffffffff, SENSE_CONFIG.ADD);
            }
        }

        public override void PushGetValue(IFConn from, uint srcid, ushort memid, byte[] infodata)
        {
            PushInfo(from,srcid,memid,infodata);
        }
        public override void PushInfo(IFConn from, uint srcid, ushort infoid, byte[] infodata)
        {
            switch (infoid)
            {
                case DYNAREA_OBJ_INTERFACE.PUSH_ADPOS:
                    {
                        string json = Misc.Converter.BytesToString(infodata);
                        DYNAREA_OBJ_INTERFACE.Pack_GetADPos pack = JsonConvert.DeserializeObject<DYNAREA_OBJ_INTERFACE.Pack_GetADPos>(json);
                        
                        mDynArea.AD = pack.ad;
                        mDynArea.Thick = pack.thick;
                        mDynArea.Position = pack.position;
                        mDynArea.BoltIndex = pack.boltIndex;
                    } break;
                case DYNAREA_OBJ_INTERFACE.PUSH_SCANINFO:
                    {
                        string json = Misc.Converter.BytesToString(infodata);
                        DYNAREA_OBJ_INTERFACE.Pack_GetScanInfo pack = JsonConvert.DeserializeObject<DYNAREA_OBJ_INTERFACE.Pack_GetScanInfo>(json);
                        if (pack.sampleAD != null)
                        {
                            for (int i = 0; i < mDynArea.SampleAD.Count() && i < pack.sampleAD.Count(); i++)
                            {
                                mDynArea.SampleAD[i] = pack.sampleAD[i];
                            }
                        }

                        mDynArea.TempState = pack.tempState;
                        mDynArea.DataValid = pack.datavalid;
                        mDynArea.Direction = pack.direction;
                        mDynArea.Width = pack.width;
                        mDynArea.FilmVelocity = pack.filmVelocity;
                        mDynArea.FilmPosition = pack.filmPosition;
                        mDynArea.Velocity = pack.velocity;
                    } break;
                case DYNAREA_OBJ_INTERFACE.PUSH_PROFILE:
                    {
                        string json = Misc.Converter.BytesToString(infodata);
                        DYNAREA_OBJ_INTERFACE.Pack_GetProfile pack = JsonConvert.DeserializeObject<DYNAREA_OBJ_INTERFACE.Pack_GetProfile>(json);

                        mDynArea.ProductName = pack.ProductName;
                        mDynArea.Target = pack.Target;
                        mDynArea.Alarm = pack.Alarm;
                        mDynArea.DataBoltNoBegin = pack.DataBoltNoBegin;
                        mDynArea.DataBoltNoEnd = pack.DataBoltNoEnd;
                        mDynArea.ScanBoltNoBegin = pack.ScanBoltNoBegin;
                        mDynArea.ScanBoltNoEnd = pack.ScanBoltNoEnd;
                        mDynArea.Comp = pack.Comp;
                        mDynArea.Shift = pack.Shift;
                    } break;
                case DYNAREA_OBJ_INTERFACE.PUSH_SYSTEM:
                    {
                        string json = Misc.Converter.BytesToString(infodata);
                        DYNAREA_OBJ_INTERFACE.Pack_GetSystem pack = JsonConvert.DeserializeObject<DYNAREA_OBJ_INTERFACE.Pack_GetSystem>(json);

                        mDynArea.FLYADIsConnect = pack.isConnect;
                        mDynArea.SecuteLock = pack.secuteLock;
                        mDynArea.Hrs = pack.hrs;
                        mDynArea.Version = pack.version;
                        mDynArea.GageType = pack.gagetype;
                    } break;
                case DYNAREA_OBJ_INTERFACE.PUSH_BOLTMAP:
                    {
                        string json = Misc.Converter.BytesToString(infodata);
                        DYNAREA_OBJ_INTERFACE.Pack_GetBoltmap pack = JsonConvert.DeserializeObject<DYNAREA_OBJ_INTERFACE.Pack_GetBoltmap>(json);

                        mDynArea.NBolts = pack.NBolts;
                        mDynArea.FirstBoltNo = pack.FirstBoltNo;
                    } break;
                case DYNAREA_OBJ_INTERFACE.PUSH_IO:
                    {
                        string json = Misc.Converter.BytesToString(infodata);
                        DYNAREA_OBJ_INTERFACE.Pack_GetIO pack = JsonConvert.DeserializeObject<DYNAREA_OBJ_INTERFACE.Pack_GetIO>(json);

                        mDynArea.IStatus = pack.iStatus;
                        mDynArea.OStatus = pack.oStatus;
                    } break;
                case DYNAREA_OBJ_INTERFACE.PUSH_CONTROLLER_STATE:
                    {
                        string json = Misc.Converter.BytesToString(infodata);
                        DYNAREA_OBJ_INTERFACE.Pack_GetControllerState pack = JsonConvert.DeserializeObject<DYNAREA_OBJ_INTERFACE.Pack_GetControllerState>(json);

                        mDynArea.ControllerState = pack.controller_state;
                        mDynArea.AutoScanCounter = pack.autoscan_counter;
                    } break;
                case DYNAREA_OBJ_INTERFACE.PUSH_POSITION2:
                    {
                        string json = Misc.Converter.BytesToString(infodata);
                        DYNAREA_OBJ_INTERFACE.Pack_Position2 pack = JsonConvert.DeserializeObject<DYNAREA_OBJ_INTERFACE.Pack_Position2>(json);

                        mDynArea.Position2 = pack.position;
                    }break;
            }
        }
        #endregion


    }
}
