#include <stdio.h>

#define SCPI_ARGS_N_C 1
#define SCPI_ARGS_N_Q 0
#include "app/scpi/scpi_handler.h"
#include "app/led/led.h"

const uint8_t fsqvbl_CommandHandlerLED1   = 1; // IND:LED:LED1
const uint8_t fsqvbl_CommandHandlerLED2   = 2; // IND:LED:LED2
const uint8_t fsqvbl_CommandHandlerWRMP   = 3; // IND:WRMP

// -----
// @argTokens, @argTypes
// Declare argument parser entities
// Supported arguments: 1=CHARACTER
DECLARE_SCPI_ARGS_C( eScpiArg_Character );

// Argument 1 Character Values allowed list / ACM Switch State
DECLARE_ARGUMENT_CHARACTER_ALLOWED_LIST_EXPORT( IND_AllowedValues_SwitchState,
   "OFF", 
   "ON",
   "GRN",
   "RED"
);

#include "app/scpi/commandHandlers/led_switch.h"
#include "app/nfm/nfm_base.h"

// Refer to:
// [1] SCPI Specification, revision 1999.0
//     "Standard Commands for Programmable Instruments (SCPI), VERSION 1999.0, May 1999"
// [2] Gpib Programming Tutorial, (http://g2pc1.bu.edu/~qzpeng/gpib/manual/GpibProgTut.pdf)
//     Electronics Group (http://www.few.vu.nl/~elec), 11 January 2000 Electronics Group
// [3] IEEE 488.2 Standard, revision IEEE Std 488.2-1987 (1992)
//     "IEEE Standard Codes, Formats, Protocols, and Common Commands for Use With IEEE Std 488.1-1987, IEEE

// =================================================================================
// @fsqvbl_CommandHandlerMEASnSWITCH_group
// State's virtual table

static void fsqe_CommandHandlerLED_Switch_Group( const struct fFSeqEntry_t * this, tFSeqCtx_t ctx );
static void fsql_CommandHandlerLED_Switch_Group( const struct fFSeqEntry_t * this, tFSeqCtx_t ctx );
static const struct fFSeqEntry_t * fsqf_CommandHandlerLED_Switch_Group( const struct fFSeqEntry_t * this, tFSeqCtx_t ctx, const struct fFSeqEntry_t * * pDeferredNext );

const fFSeqVTable_t fsqvbl_CommandHandlerLEDSWITCH_group = 
{
     .f = fsqf_CommandHandlerLED_Switch_Group,
     .enter = fsqe_CommandHandlerLED_Switch_Group,  
     .leave = fsql_CommandHandlerLED_Switch_Group
};

static void fsqe_CommandHandlerLED_Switch_Group( const struct fFSeqEntry_t * this, tFSeqCtx_t ctx )
{
    sProcessProgramDataCommonContext_t * common_ctx = ctx;
    
    common_ctx->MeasAndSwitch.idx = 0;
    
    SCPI_PARSE_ARGUMENTS( common_ctx ); (void)common_ctx->argsParserStatus; // status is modified
}

static void fsql_CommandHandlerLED_Switch_Group( const struct fFSeqEntry_t * this, tFSeqCtx_t ctx )
{
}

static const struct fFSeqEntry_t * fsqf_CommandHandlerLED_Switch_Group( const struct fFSeqEntry_t * this, 
                                                                         tFSeqCtx_t ctx, 
                                                                          const struct fFSeqEntry_t * * pDeferredNext )
{
    const fFSeqEntry_t * nextstate = NULL;
    
    sProcessProgramDataCommonContext_t * common_ctx = ctx;
    sScpiParserContext_t * global_ctx = common_ctx->global_ctx;

    switch( common_ctx->event )
    {
        case eProgramData_Event_Write:
        {
            if( eScpiStatus_success != common_ctx->argsParserStatus ) // check argument parser status
            {
               common_ctx->status = eProgramDataArgumentSyntax;  // parameter syntax error, caller should generate error message
            }
            else if( ! common_ctx->isQuery )
            {
               common_ctx->status = eProgramDataIllegalArgument;  // forward set, illegal parameter value, caller should generate error message

               if( common_ctx->handler_ctx == &fsqvbl_CommandHandlerLED1 )
               {
                // process first argument (switch state)
                common_ctx->SwitchState.state = SCPI_PROCESS_ARGUMENT_CHARACTER( common_ctx, IND_AllowedValues_SwitchState, 0 );
               }
               else if( common_ctx->handler_ctx == &fsqvbl_CommandHandlerLED2 )
               {
                common_ctx->SwitchState.state = SCPI_PROCESS_ARGUMENT_CHARACTER( common_ctx, IND_AllowedValues_SwitchState, 0 );
               }
               else if( common_ctx->handler_ctx == &fsqvbl_CommandHandlerWRMP )
               {
                common_ctx->SwitchState.state = SCPI_PROCESS_ARGUMENT_CHARACTER( common_ctx, IND_AllowedValues_SwitchState, 0 );
               }
               
               // check result
               if( SCPI_ARGUMENT_CHARACTER_INVALID_ID == common_ctx->SwitchState.state )
               {
                   (void)common_ctx->status; // eProgramDataIllegalArgument
               }
               else
               {
                   size_t error = 0;
                   
                   if( common_ctx->handler_ctx == &fsqvbl_CommandHandlerLED1 )
                   {
                       if( common_ctx->SwitchState.state == 0 )
                       {
                       }
                       else if( common_ctx->SwitchState.state == 2 )
                       {
                       }
                       else if( common_ctx->SwitchState.state == 3 )
                       {
                       }
                       else
                       {
                           error = 1; // Key State Syntax Error
                       }
                   }
                   else if( common_ctx->handler_ctx == &fsqvbl_CommandHandlerLED2 )
                   {
                       if( common_ctx->SwitchState.state == 0 )
                       {
                           LEDHandle.harmup_stop();
                           LEDHandle.SetMode(eLedMode_Idle);
                           ledRed(false);
                           ledGreen(false);
                       }
                       else if( common_ctx->SwitchState.state == 2 )
                       {
                           LEDHandle.harmup_stop();
                           LEDHandle.SetMode(eLedMode_Idle);
                           ledRed(false);
                           ledGreen(true);
                       }
                       else if( common_ctx->SwitchState.state == 3 )
                       {
                           LEDHandle.harmup_stop();
                           LEDHandle.SetMode(eLedMode_Idle);
                           ledRed(true);
                           ledGreen(false);
                       }
                       else
                       {
                           error = 1; // Key State Syntax Error
                       }                   
                   }
		   else if( common_ctx->handler_ctx == &fsqvbl_CommandHandlerWRMP )
                   {
                       if( common_ctx->SwitchState.state == 0 )
                       {
                           LEDHandle.harmup_stop();
                           LEDHandle.SetMode(eLedMode_Normal);
                           LEDHandle.harmup_stop();
                       }
                       else if( common_ctx->SwitchState.state == 1 )
                       {
                           LEDHandle.harmup_stop();
                           LEDHandle.SetMode(eLedMode_Normal);
                           LEDHandle.harmup_init();
                       }
                       else
                       {
                           error = 1; // Key State Syntax Error
                       }
                   }


                   switch( error )
                   {
                       case 1: // Key State Syntax Error
                       {
                           (void)common_ctx->status; // eProgramDataIllegalArgument
                       }
                       break;

                       case 2: // Key State unavailable in this device
                       {
                           (void)common_ctx->status; // eProgramDataIllegalArgument
                       }
                       break;

                   }

                   if( 0 != error ) break;

                   common_ctx->status = eProgramDataDone;  // request processed, wait for reading...
               }
            }
            else
            {
               common_ctx->status = eProgramDataNeedRead;  // request processed, wait for reading...
            }
        }
        break;

        case eProgramData_Event_Read:
        {
             // @idx - current position of the source data to be outputed
             if( common_ctx->MeasAndSwitch.idx == 0 ) // first reading
             {
                 size_t length = 0;
                 
                 if( common_ctx->handler_ctx == &fsqvbl_CommandHandlerLED1 )
                 {
                     eLed_color_t color = GET_LED_COLOR_STATE(LED1);
                     if(color == eLed_color_off)
                     {
                         const char temp[5]= "OFF\n";
                         length = _snprintf( common_ctx->tempBuffer, sizeof(common_ctx->tempBuffer), "%s", temp);
                     }
                     else if(color == eLed_color_red)
                     {
                         const char temp[5]= "RED\n";
                         length = _snprintf( common_ctx->tempBuffer, sizeof(common_ctx->tempBuffer), "%s", temp);
                     }
                     else if(color == eLed_color_grn)
                     {
                         const char temp[5]= "GRN\n";
                         length = _snprintf( common_ctx->tempBuffer, sizeof(common_ctx->tempBuffer), "%s", temp);
                     }
                 }
                 else if( common_ctx->handler_ctx == &fsqvbl_CommandHandlerLED2 )
                 {
                     eLed_color_t color = GET_LED_COLOR_STATE(LED2);
                     if(color == eLed_color_off)
                     {
                         const char temp[5]= "OFF\n";
                         length = _snprintf( common_ctx->tempBuffer, sizeof(common_ctx->tempBuffer), "%s", temp);
                     }
                     else if(color == eLed_color_red)
                     {
                         const char temp[5]= "RED\n";
                         length = _snprintf( common_ctx->tempBuffer, sizeof(common_ctx->tempBuffer), "%s", temp);
                     }
                     else if(color == eLed_color_grn)
                     {
                         const char temp[5]= "GRN\n";
                         length = _snprintf( common_ctx->tempBuffer, sizeof(common_ctx->tempBuffer), "%s", temp);
                     }
                 }
                 else if( common_ctx->handler_ctx == &fsqvbl_CommandHandlerWRMP )
                 {
                     eLed_color_t color = GET_LED_COLOR_STATE(LED2);
                     if(color == eLed_color_red)
                     {
                         const char temp[5]= "ON\n";
                         length = _snprintf( common_ctx->tempBuffer, sizeof(common_ctx->tempBuffer), "%s", temp);
                     }
                     else if(color == eLed_color_grn)
                     {
                         const char temp[5]= "OFF\n";
                         length = _snprintf( common_ctx->tempBuffer, sizeof(common_ctx->tempBuffer), "%s", temp);
                     }
                 }
				 
                 if( length > 0 )
                 {
                     // place null-terminator in the end of line
                     common_ctx->tempBuffer[length] = '\0';
                 }
                 else
                 {
                     
                     fsq_RaiseError( SCPI_ERROR_INTERNAL_DEVICE,
                                     SCPI_ERROR_INTERNAL_DEVICE_MSG,
                                     global_ctx->sParser.xHandlerToken.shead,
                                     global_ctx->sParser.xHandlerToken.stail );

                     common_ctx->status = eProgramData_SpecificError; // specific error already generated
                     break;
                 }
             }

             // Since @done flag is set, this dispatcher shall not be called anymore.
             // Since this handler is implemented as a single-state automat, there no
             // ... other states to go to:
             (void)nextstate;

             // modify current postion index:
             SCPI_RESPONSE_HELPER( common_ctx, common_ctx->MeasAndSwitch.idx );
        }
        break;
    }

    return nextstate;
}
