using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FObjBase;
namespace FLY.Simulation.Flyad7.OBJProxy
{
    class DRIVE_MAN:FObj
    {
        FLYAD7 Data;
        public DRIVE_MAN(int objsys_idx,FLYAD7 data):base(objsys_idx)
        {
            ID = FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.ID;
            Data = data;
        }
        public override void GetValue(IFConn from, uint srcid, ushort memid, out byte[] infodata)
        {
            infodata = null;
            switch (memid) 
            {
                case FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.GET_DRIVEPARAM:
                    {
                        FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.Pack_DriveParam pack = new FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.Pack_DriveParam();
                        pack.Velocity = Data.Velocity;
                        pack.SV = Data.SVelocity;
                        pack.ATime = Data.ATime;
                        pack.DTime = Data.DTime;
                        pack.hspeed1 = Data.HVelocity1;
                        pack.hspeed2 = Data.HVelocity2;
                        infodata = pack.ToBytes();
                    }break;
                case FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.GET_STATE:
                    {
                        FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.Pack_DriveState pack = new FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.Pack_DriveState();
                        pack.order = Data.DriveOrder;
                        pack.status = Data.DriveStatus;
                        pack.marker = Data.Marker;
                        pack.mode = 1;
                        infodata = pack.ToBytes();
                    }break;
                case FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.GET_SYNC_STATUS:
                    {
                        FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.Pack_SyncStatus pack = new FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.Pack_SyncStatus();

                        pack.status = Data.GetSyncStatus();
                        infodata = pack.ToBytes();
                    }
                    break;
                case FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.GET_SYNC_ORDER_CNT:
                    {
                        FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.Pack_SyncOrderCnt pack = new FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.Pack_SyncOrderCnt();
                        pack.cnt = Data.GetSyncOrderCnt();
                        infodata = pack.ToBytes();
                    }
                    break;
            }
        }

        public override void SetValue(IFConn from, uint srcid, ushort memid, byte[] infodata)
        {
            switch (memid)
            {
                case FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.SET_DRIVEPARAM:
                    {
                        FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.Pack_DriveParam pack = new FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.Pack_DriveParam();
                        if (!pack.TryParse(infodata))
                            break;
                        if(pack.Velocity!=UInt32.MaxValue)
                            Data.Velocity = pack.Velocity;
                        if (pack.SV != UInt32.MaxValue)
                            Data.SVelocity = pack.SV;
                        if (pack.ATime != UInt32.MaxValue)
                            Data.ATime = pack.ATime;
                        if (pack.DTime != UInt32.MaxValue)
                            Data.DTime = pack.DTime;
                        if (pack.hspeed1 != UInt32.MaxValue)
                            Data.HVelocity1 = pack.hspeed1;
                        if (pack.hspeed2 != UInt32.MaxValue)
                            Data.HVelocity2 = pack.hspeed2;
                    } break;
            }
        }
        public override void CallFunction(IFConn from, uint srcid, uint magic, ushort funcid, byte[] infodata)
        {
            switch (funcid) 
            {
                case FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.CALL_PXX:
                    {
                        FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.Pack_OrderBase pack;
                        if(!FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.Pack_OrderBase.TryParse(
                            infodata, out pack))
                            return;//失败

                        Type t = pack.GetType();
                        if(t == typeof(FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.Pack_CallRunto))
                        {
                            FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.Pack_CallRunto p = 
                                pack as FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.Pack_CallRunto;
                            Data.Runto(p.to);
                        }
                        else if(t == typeof(FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.Pack_CallPXX))
                        {
                            FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.Pack_CallPXX p =
                                pack as FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.Pack_CallPXX;
                            switch (p.order) 
                            {
                                case FlyADBase.DRIVE_MAN_ORDER.ORIGIN:
                                    Data.Origin();
                                    break;
                                case FlyADBase.DRIVE_MAN_ORDER.STOP:
                                    Data.Stop();
                                    break;
                                case FlyADBase.DRIVE_MAN_ORDER.FORWORD:
                                    Data.Forward();
                                    break;
                                case FlyADBase.DRIVE_MAN_ORDER.BACKWORD:
                                    Data.Backward();
                                    break;
                            }
                        }
                        else if (t == typeof(FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.Pack_Pos1LCShift))
                        {
                            FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.Pack_Pos1LCShift p =
                                pack as FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.Pack_Pos1LCShift;
                            Data.Pos1LCShift = p.pos1_LCShift;
                        }
                        else if (t == typeof(FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.Pack_Pos2Shift)) 
                        {
                            FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.Pack_Pos2Shift p =
                                pack as FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.Pack_Pos2Shift;
                            Data.SetPos2Offset(p.pos2_shift);
                        }
                        else if (t == typeof(FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.Pack_Pos2)) 
                        {
                            FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.Pack_Pos2 p =
                                pack as FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.Pack_Pos2;
                            Data.SetPos2(p.pos2);
                        }
                        else if (t == typeof(FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.Pack_Pos2Comp))
                        {
                            FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.Pack_Pos2Comp p =
                                pack as FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.Pack_Pos2Comp;
                            //不实现，功能作废
                        }
                        else if (t == typeof(FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.Pack_Pos2_01))
                        {
                            FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.Pack_Pos2_01 p =
                                pack as FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.Pack_Pos2_01;
                            Data.SetPos2At01(p.pos2, p.immediately);
                        }
                        else if (t == typeof(FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.Pack_SyncBeginWithPos2))
                        {
                            FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.Pack_SyncBeginWithPos2 p =
                                pack as FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.Pack_SyncBeginWithPos2;
                            Data.SyncBegin(p.pos2);
                        }
                        else if (t == typeof(FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.Pack_SyncBegin))
                        {
                            Data.SyncBegin();
                        }
                        else if (t == typeof(FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.Pack_SyncEnd))
                        {
                            Data.SyncEnd();
                        }
                        else if (t == typeof(FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.Pack_SyncClear))
                        {
                            Data.SyncClear();
                        }
                        else if (t == typeof(FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.Pack_SyncPos2Clear))
                        {
                            Data.SyncPos2Clear();
                        }
                        else if (t == typeof(FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.Pack_SyncRunAtLC))
                        {
                            FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.Pack_SyncRunAtLC p =
                                pack as FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.Pack_SyncRunAtLC;
                            Data.SyncRunAtLC(p.pos2_begin, p.pos2_end, p.pos1lc, p.hasDataGrid, p.marker);
                        }
                        else if (t == typeof(FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.Pack_SyncOrigin))
                        {
                            FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.Pack_SyncOrigin p =
                                pack as FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.Pack_SyncOrigin;
                            Data.SyncOrigin(p.marker);
                        }
                        else if (t == typeof(FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.Pack_SyncRuntoLC))
                        {
                            FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.Pack_SyncRuntoLC p =
                                pack as FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.Pack_SyncRuntoLC;
                            Data.SyncRunToLC(p.pos1, p.velocity, p.hasDataGrid, p.marker);
                        }
                        else if (t == typeof(FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.Pack_SyncRunto))
                        {
                            FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.Pack_SyncRunto p =
                                pack as FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.Pack_SyncRunto;
                            Data.SyncRunTo(p.pos1, p.velocity, p.hasDataGrid, p.marker);
                        }
                        else if (t == typeof(FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.Pack_SyncWait))
                        {
                            FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.Pack_SyncWait p =
                                pack as FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.Pack_SyncWait;
                            Data.SyncWait(p.ms,p.marker);
                        }
                    }break;
            }
        }
    }
}
