#include "core/config.h"
#include <stdio.h>

#define SCPI_ARGS_N 0
#include "app/scpi/scpi_handler.h"

#include "app/scpi/commandHandlers/interface_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

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

static void fsqe_CommandHandlerInterfaceSwitch( const struct fFSeqEntry_t * this, tFSeqCtx_t ctx );
static void fsql_CommandHandlerInterfaceSwitch( const struct fFSeqEntry_t * this, tFSeqCtx_t ctx );
static const struct fFSeqEntry_t * fsqf_CommandHandlerInterfaceSwitch( const struct fFSeqEntry_t * this, tFSeqCtx_t ctx, const struct fFSeqEntry_t * * pDeferredNext );

const fFSeqVTable_t fsqvbl_CommandHandlerInterfaceSwitch = 
{
     .f = fsqf_CommandHandlerInterfaceSwitch,
     .enter = fsqe_CommandHandlerInterfaceSwitch,  
     .leave = fsql_CommandHandlerInterfaceSwitch
};

static void fsqe_CommandHandlerInterfaceSwitch( const struct fFSeqEntry_t * this, tFSeqCtx_t ctx )
{
    sProcessProgramDataCommonContext_t * common_ctx = ctx;

    SCPI_PARSE_ARGUMENTS( common_ctx ); (void)common_ctx->argsParserStatus; // status is modified

    common_ctx->IntSwitch.idx = 0;
}

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

static const struct fFSeqEntry_t * fsqf_CommandHandlerInterfaceSwitch( 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( ! common_ctx->isQuery )
            {
               common_ctx->status = eProgramDataSyntaxError; // invalid command header type: COMMAND not supported
            }
            else if( eScpiStatus_success != common_ctx->argsParserStatus ) // check argument parser status
            {
               common_ctx->status = eProgramDataArgumentSyntax;  // parameter syntax error, caller should generate error message
            }
            else
            {
               #if CONFIG_USB_OVERRIDE_IFACE_USBTMC
               if( NFMClass->methods.usbInterface.setInterface( eNFM_IfaceUSBTMC ) )
               #else
               if( NFMClass->methods.usbInterface.setInterface( eNFM_IfaceUSBVendor ) )
               #endif
               {
                   #if CONFIG_REBOOT_FEATURE
                   RebootRequest();
                   #endif

                   common_ctx->status = eProgramDataNeedRead;  // request processed
               }    
               else
               {
                   fsq_RaiseError( SCPI_ERROR_INTERNAL_DEVICE,
                                   SCPI_ERROR_INTERNAL_INT_SWITCH_MSG,
                                   global_ctx->sParser.xHandlerToken.shead,
                                   global_ctx->sParser.xHandlerToken.stail );

                   common_ctx->status = eProgramData_SpecificError;  // request processed
               }
               // 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;
            }
        }
        break;

        case eProgramData_Event_Read:
        {
            if( 0 == common_ctx->IntSwitch.idx )
            {
               size_t l = _snprintf( common_ctx->tempBuffer, sizeof(common_ctx->tempBuffer), "%s", SCPI_MESSAGE_INTERNAL_INT_SWITCH_MSG );

               if( l > 0 )
               {
                   // place null-terminator in the end of line
                   common_ctx->tempBuffer[l] = '\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;
               }
            }

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

    return nextstate;
}

