#include "apdefap.h"

int gscAction( void )
{    
    int dbg = 1;
    int trayNo;
    int QcNo;
    int SeqCounter;
    int MagzineCounter;
    int MagzineCounterClean;
    char TrayNoTag[20];
    char TrayNoTagQC[20];
    char QcNoTag[22];
    char QcNoTrigger[20];
//    char QcStartReg[20];
    char Augment[20];
    char MagzineCounterLiveTag[20];
    char MagzineCounterRunTag[20];
    char MagzineCounterCompleteTag[20];

    if(dbg==1){printf("\r\nStarting TrayQaqcTriggers.\r\n");}

    // Clear QC triggers as the HAG has run a QC
    if(GetTagBit("HAG_StandardTriggerReset"))
    {
        SetTagFloat("CrmTrigger",0);	//Return-Type: BOOL 
        SetTagBit("HAG_StandardTriggerReset",0);	//Return-Type: BOOL CrmTrigger
    }

    // Run checks for all three trays
    for (trayNo=1; trayNo<=3;trayNo++)
    {
        sprintf(TrayNoTag,"Tray%d_Cnt",trayNo); // Get tag for tray ID (Active).
        sprintf(TrayNoTagQC,"Buffer%dTrayId",trayNo); // Get tag for tray ID (Stored).
        sprintf(MagzineCounterLiveTag,"Mag16%dT_5",trayNo); // Get tag for number of live run samples in tray.
        sprintf(MagzineCounterRunTag,"Mag16%dT_6",trayNo); // Get tag for number of live run samples in tray.
        sprintf(MagzineCounterCompleteTag,"Mag16%dT_7",trayNo); // Get tag for number of live complete samples in tray.

        // No active samples in this tray, don't bother processing.
        if(((int)GetTagWord(MagzineCounterRunTag)+(int)GetTagWord(MagzineCounterCompleteTag)+(int)GetTagWord(MagzineCounterLiveTag)) == 0)
        {
            if(dbg==1){printf("No active samples in this tray. %d\r\n",((int)GetTagWord(MagzineCounterRunTag)+(int)GetTagWord(MagzineCounterCompleteTag)+(int)GetTagWord(MagzineCounterLiveTag)));}
            continue;
        }

        // If the buffer tray ID has changed then.....
        if(GetTagWord(TrayNoTag) == GetTagWord(TrayNoTagQC))
        {
            if(dbg==1){printf(" - Old tray in buffer %d.\r\n",trayNo);}
            // For each of the QAQC sample in the QAQC tray.
            for (QcNo=1; QcNo<=10;QcNo++)
            {
                sprintf(QcNoTag,"Tray%dQaqcTriggered%d",trayNo,QcNo); // Get tag for if this QC has been triggered for this tray.
                sprintf(QcNoTrigger,"TrayQaqcTrigger%d",QcNo); // Get tag for what is required to trigger this QC sample.
//                sprintf(QcStartReg,"QCStartReg_%d",QcNo); // Get tag to set the trigger bit for this QC sample in the tray.
                sprintf(Augment,"Augment%d",QcNo); // Get tag for what augment matches this QC sample.
                
                // Run this if the Trigger has not been triggered already for this tray
                if(GetTagBit(QcNoTag)==0)
                {
                    
                    if(dbg==1){printf("    - trigger is %d for QC %d.\r\n",GetTagWord(QcNoTrigger),QcNo);}
                    switch(GetTagWord(QcNoTrigger))
                    {
                        case 0: // Do not run.
                        case 1: // Do not run.
                            //if(dbg==1){printf("       - No Run.\r\n");}
                        break;
                        case 2: // Run at start of tray.
                            // Augment
                            if(GetTagFloat(Augment)==2)// Random CRM
                            {
                                if(dbg==1){printf("=============triggered Random QC=============\r\nReason;- Start of tray.",QcNo);}
                                SetTagFloat("CrmTrigger",11);
                                return 0;
                            }else if(GetTagFloat(Augment)==0){// Sequential CRM
                                if(dbg==1){printf("=============triggered Sequential QC=============\r\nReason;- Start of tray.",QcNo);}
                                SetTagFloat("CrmTrigger",12);
                                return 0;
                            }
//                            SetTagBit(QcStartReg,1);
                            if(dbg==1){printf("=============triggered QC %d=============\r\nReason;- Start of tray.",QcNo);}
                            SetTagFloat("CrmTrigger",QcNo);	//Return-Type: BOOL 
                            SetTagBit(QcNoTag,1);
                            if(dbg==1){printf("       - Run at start of tray.\r\n");}
                        break;
                        default: // Run at position.
                            MagzineCounter = GetTagWord(MagzineCounterRunTag) + GetTagWord(MagzineCounterCompleteTag);
                            if((MagzineCounter >= (int)GetTagWord(QcNoTrigger)-2))
                            {
                                if(dbg==1){printf("=============triggered QC %d=============\r\nReason;- %d is greater then %d.",QcNo,MagzineCounter,((int)GetTagWord(QcNoTrigger)-2));}
                                // Augment
                                if(Augment==2)// Random CRM
                                {
                                    SetTagFloat("CrmTrigger",11);
                                    return 0;
                                }else if(Augment==0){// Sequential CRM
                                    SetTagFloat("CrmTrigger",12);
                                    return 0;
                                }
                                SetTagBit(QcNoTag,1);
//                                SetTagBit(QcStartReg,1);
                                SetTagFloat("CrmTrigger",QcNo);	//Return-Type: BOOL 
                            }
                            if(dbg==1){printf("       - run at position %d. MagCounter = %d.\r\n",GetTagWord(QcNoTrigger)-2, MagzineCounter);}
                        break;
                    }
                }
            }
        }else{
            if(dbg==1){printf(" - New tray detected.\r\n");}
            SetTagWord(TrayNoTagQC,GetTagWord(TrayNoTag));	//Return-Type: BOOL 

            for (QcNo=1; QcNo<=10;QcNo++)
            {
                sprintf(QcNoTag,"Tray%dQaqcTriggered%d",trayNo,QcNo);
                SetTagBit(QcNoTag,0);	//Return-Type: BOOL 
            }
        }

    }
    return 0; 
}