using MTC;
using MTConnect;
using System;

namespace MTC_Adapter
{
    public class AdapterDemo : AdapterGeneric
    {
        public Condition mTemp = new Condition("temp");
        public Condition mFillLevel = new Condition("cool_low", true);

        /// <summary>
        /// estende l'init della classe base...
        /// </summary>
        /// <param name="caller"></param>
        /// <param name="adpConf"></param>
        public AdapterDemo(MainForm caller, AdapterConf adpConf) : base(caller, adpConf)
        {
            if (utils.CRB("verbose")) lg.Info("Start init Adapter DEMO");
            // IMPLICITO: carica da metodo base tutte le specifiche del conf...


            // aggiungo specifiche del demo: allarmi gestiti a buttons!!!
            mAdapter.AddDataItem(mTemp);
            mAdapter.AddDataItem(mFillLevel);
 
        }

        public override void startAdapter(int port)
        {
            base.startAdapter(port);

            // aggiungo altre condizioni specifiche...
            
            mTemp.Normal();
            mFillLevel.Normal();
        }

        public override void getSlowChangingData()
        {
            // serve?!?
            base.getSlowChangingData();

            mOperator.Value = parentForm.datiProd.Operator;

            // recupero SEMPRE dati ulteriori: status ON/OFF, clock, ...
            mStatus.Value = parentForm.datiProd.Status;
            mAccTime.Value = parentForm.datiProd.AccTime;
            mClock.Value = DateTime.Now.Date.ToFileTimeUtc();
            mPower.Value = parentForm.datiProd.Power;
        }

        public override void getStrobeAndAckStatus()
        {
            base.getStrobeAndAckStatus();

            // controllo bool allarmi...
            if (parentForm.datiProd.EmrStop)
            {
                mEStop.Value = "TRIGGERED";
            }
            else
            {
                mEStop.Value = "ARMED";
            }
            
            // imposto func mode...
            mFunctionalMode.Value = parentForm.datiProd.FuncMode;

            // se ho un messaggio...
            if(parentForm.datiProd.MessageText.Length>0)
            {
                mMessage.Code = parentForm.datiProd.MessageCode;
                mMessage.Value = parentForm.datiProd.MessageText;
            }
        }

        public override void getConfigParam()
        {
            base.getConfigParam();
        }
        

        public override void processStrobe()
        {
            base.processStrobe();
            // qui forzo la gestione di update dei path & co...
            getPath();
            getUnOp();
            getAxis();
        }
        /// <summary>
        /// non fa nulla perché recupera allarmi in vettore al singolo cambio...
        /// </summary>
        /// <param name="Alarm2Refresh"></param>
        /// <param name="giveAck"></param>
        public override void refreshAlarmState(StFlag32 Alarm2Refresh, bool giveAck)
        {
            base.refreshAlarmState(Alarm2Refresh, giveAck);
        }

        public override void getPath()
        {
            base.getPath();

            PathData PtData = parentForm.CurrPath;
            if(PtData.PathSel>=0)
            {
                vettPath[PtData.PathSel].mPathFeed.Value = PtData.PathFeedrate;
                vettPath[PtData.PathSel].mPathFeedOver.Value = PtData.PathFeedrateOver;
                vettPath[PtData.PathSel].mPathRapidOver.Value = PtData.PathRapidOver;
                vettPath[PtData.PathSel].mPathPosActX.Value = PtData.PathPosAct.x;
                vettPath[PtData.PathSel].mPathPosActY.Value = PtData.PathPosAct.y;
                vettPath[PtData.PathSel].mPathPosActZ.Value = PtData.PathPosAct.z;
                vettPath[PtData.PathSel].mPathPosActI.Value = PtData.PathPosAct.i;
                vettPath[PtData.PathSel].mPathPosActJ.Value = PtData.PathPosAct.j;
                vettPath[PtData.PathSel].mPathPosActK.Value = PtData.PathPosAct.k;



                // aggiunta x revisione 2016.05.05
                vettPath[PtData.PathSel].mPathType.Value = PtData.PathType;
                vettPath[PtData.PathSel].mPathPartId.Value = PtData.PartId;
                vettPath[PtData.PathSel].mPathPartCount.Value = PtData.pzTot;
                vettPath[PtData.PathSel].mPathRunMode.Value = PtData.RunMode;
                vettPath[PtData.PathSel].mPathExeMode.Value = PtData.ExeMode;
                vettPath[PtData.PathSel].mPathCurrProg.Value = PtData.ProgramName;
                vettPath[PtData.PathSel].mPathCurrProgRowNum.Value = PtData.ProgrRow;
                vettPath[PtData.PathSel].mPathActiveAxes.Value = PtData.ActiveAxes;
            }
        }

        public override void getUnOp()
        {
            base.getUnOp();

            UnOpData uoData = parentForm.CurrUnOp;
            if (uoData.UnOpSel >= 0)
            {
                vettUnOp[uoData.UnOpSel].mUnOpToolId.Value = uoData.UnOpToolId;
                vettUnOp[uoData.UnOpSel].mUnOpNumCU.Value = uoData.UnOpNumCU;
                vettUnOp[uoData.UnOpSel].mUnOpStatus.Value = uoData.UnOpStatus;
                vettUnOp[uoData.UnOpSel].mUnOpVitaRes.Value = uoData.UnOpVitaRes;
                vettUnOp[uoData.UnOpSel].mUnOpSpeed.Value = uoData.UnOpSpeed;
                vettUnOp[uoData.UnOpSel].mUnOpLoad.Value = uoData.UnOpLoad;
                vettUnOp[uoData.UnOpSel].mUnOpAccTime.Value = uoData.UnOpAccTime;
            }
        }

        public override void getAxis()
        {
            base.getAxis();
            AxisData AxData = parentForm.CurrAxis;
            if (AxData.AxisSel >= 0)
            {
                // USO asse selezionato + dati visualizzati e di conseguenza aggiorno...
                vettAxis[AxData.AxisSel].mAxMainProc.Value = AxData.AxisMainProc;
                vettAxis[AxData.AxisSel].mAxIsMaster.Value = AxData.AxisIsMaster;
                vettAxis[AxData.AxisSel].mAxMastId.Value = AxData.AxisMastId;
                //vettAxis[AxData.AxisSel].mAxType.Value = AxData.AxisType;
                vettAxis[AxData.AxisSel].mAxDir.Value = AxData.AxisDir;
                vettAxis[AxData.AxisSel].mAxLoad.Value = AxData.AxisLoad;
                vettAxis[AxData.AxisSel].mAxPosAct.Value = AxData.AxisPosAct;
                vettAxis[AxData.AxisSel].mAxPosTgt.Value = AxData.AxisPosTgt;
                vettAxis[AxData.AxisSel].mAxFeedAct.Value = AxData.AxisFeedAct;
                vettAxis[AxData.AxisSel].mAxFeedOver.Value = AxData.AxisFeedOver;
                vettAxis[AxData.AxisSel].mAxAccelAct.Value = AxData.AxisAccel;
                vettAxis[AxData.AxisSel].mAxAccTime.Value = AxData.AxisAccTime;
                vettAxis[AxData.AxisSel].mAxBattery.Value = AxData.AxisBattery;
            }
        }
        
    }
}
