#include <stdio.h>
#define PRINT_VALUE_MIN_SPACE (20) // DO NOT TOUCH! =20, see @printValues
#define SCPI_ARGS_N 2
#include "app/scpi/scpi_handler.h"

const uint8_t fsqvbl_CommandHandlerMemoryTableThermoCorrectionMagnitude_context = 1;        // MEMory:TABLe:THERmo:CORRection:MAGNitude
const uint8_t fsqvbl_CommandHandlerMemoryTableThermoCorrectionPhase_context = 2;            // MEMory:TABLe:THERmo:CORRection:PHASe

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

// Argument 1 Character Values allowed list / ACM Path
DECLARE_ARGUMENT_CHARACTER_ALLOWED_LIST_IMPORT( MemTable_AllowedValues_AcmPath ); 

// Argument 2 Character Values allowed list / S-parameter
DECLARE_ARGUMENT_CHARACTER_ALLOWED_LIST_IMPORT( MemTable_AllowedValues_SParam );

#include "app/scpi/commandHandlers/memory_table_ther_corr_data.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_CommandHandlerMemoryTableThermoCorrectionData
// State's virtual table

static size_t printValues( char * pcBuffer, size_t szBuffer, const sNFMChrzPoint_t * pValues, bool mode, size_t nValues, bool theLastOne );
static void fsqe_CommandHandlerMemoryTableThermoCorrectionData( const struct fFSeqEntry_t * this, tFSeqCtx_t ctx );
static void fsql_CommandHandlerMemoryTableThermoCorrectionData( const struct fFSeqEntry_t * this, tFSeqCtx_t ctx );
static const struct fFSeqEntry_t * fsqf_CommandHandlerMemoryTableThermoCorrectionData( const struct fFSeqEntry_t * this, tFSeqCtx_t ctx, const struct fFSeqEntry_t * * pDeferredNext );

const fFSeqVTable_t fsqvbl_CommandHandlerMemoryTableThermoCorrectionData = 
{
     .f = fsqf_CommandHandlerMemoryTableThermoCorrectionData,
     .enter = fsqe_CommandHandlerMemoryTableThermoCorrectionData,  
     .leave = fsql_CommandHandlerMemoryTableThermoCorrectionData
};

static void fsqe_CommandHandlerMemoryTableThermoCorrectionData( 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->MemTableThermoCorrectionData.idx = 0;
    common_ctx->MemTableThermoCorrectionData.nchs = 0;
    common_ctx->MemTableThermoCorrectionData.comb = 0;
    common_ctx->MemTableThermoCorrectionData.state = 0;
    common_ctx->MemTableThermoCorrectionData.init = false;
    common_ctx->MemTableThermoCorrectionData.isdone = false;

    memset( &common_ctx->MemTableThermoCorrectionData.getctx, 0, sizeof(common_ctx->MemTableThermoCorrectionData.getctx) );
}

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

static const struct fFSeqEntry_t * fsqf_CommandHandlerMemoryTableThermoCorrectionData( 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
            {
               common_ctx->status = eProgramDataIllegalArgument;  // forward set, illegal parameter value, caller should generate error message

               // Get acm path (port combination)
               common_ctx->MemTableThermoCorrectionData.comb = SCPI_PROCESS_ARGUMENT_CHARACTER( common_ctx, MemTable_AllowedValues_AcmPath, 0 );

               if( SCPI_ARGUMENT_CHARACTER_INVALID_ID == common_ctx->MemTableThermoCorrectionData.comb ) break;  // eProgramDataIllegalArgument

               // Get S-Param (port state)
               common_ctx->MemTableThermoCorrectionData.state = SCPI_PROCESS_ARGUMENT_CHARACTER( common_ctx, MemTable_AllowedValues_SParam, 1 );

               if( SCPI_ARGUMENT_CHARACTER_INVALID_ID == common_ctx->MemTableThermoCorrectionData.state ) break;  // eProgramDataIllegalArgument

               ePortStateId_t portState = (ePortStateId_t)((ePortStateId_MIN) + common_ctx->MemTableThermoCorrectionData.state);

               if( ! NFMClass->methods.checkPortStateAvailable( portState ) )
               {
                   common_ctx->status = eProgramDataIllegalArgument;
                   common_ctx->argErrIdx = 1; // specify erroneous argument index: S-param
                   break;
               }

               ePortComb_t portComb = (ePortComb_t)((ePortComb_MIN) + common_ctx->MemTableThermoCorrectionData.comb);

               if( ! NFMClass->methods.checkPortCombinationAvailable( portComb ) )
               {
                   common_ctx->status = eProgramDataIllegalArgument;
                   common_ctx->argErrIdx = 0; // specify erroneous argument index: ACM-Path
                   break;
               }

               // Check if the combination of ACM-path and port state is legal:
               if( NFMClass->methods.checkTableParams( portComb, portState ) )
               {
                   common_ctx->status = eProgramDataNeedRead;  // request processed, wait for reading...
               }
               else
               {
                   common_ctx->status = eProgramDataIllegalArgument; // illegal parameter for this device
                   common_ctx->argErrIdx = 1; // specify erroneous argument index: S-param
               }
            }
        }
        break;

        case eProgramData_Event_Read:
        {
             do
             {

             // @idx - current position of the source data to be outputed
             if( common_ctx->MemTableThermoCorrectionData.idx >= common_ctx->MemTableThermoCorrectionData.nchs )
             {
                 if( common_ctx->MemTableThermoCorrectionData.isdone ) break;

                 // There no already prepared data in @tempBuffer
                 if( ! common_ctx->MemTableThermoCorrectionData.init ) // first reading
                 {
                     ePortComb_t portComb = (ePortComb_t)((ePortComb_MIN) + common_ctx->MemTableThermoCorrectionData.comb);
                     ePortStateId_t portState = (ePortStateId_t)((ePortStateId_MIN) + common_ctx->MemTableThermoCorrectionData.state);

                     common_ctx->status = eProgramDataIllegalArgument; // forward set

                     if( common_ctx->handler_ctx == &fsqvbl_CommandHandlerMemoryTableThermoCorrectionMagnitude_context )
                     {
                         if( 0 == NFMClass->methods.xThermo.getPointsThermoMagn_Init( portComb,
                                                                                      portState,
                                                                                      common_ctx->MemTableThermoCorrectionData.dDataPoints,
                                                                                      cellsof(common_ctx->MemTableThermoCorrectionData.dDataPoints),
                                                                                      &common_ctx->MemTableThermoCorrectionData.getctx ) )
                         {
                             goto L_TableTherCorrData_DATAERR;
                         }
                     }
                     else
                     if( common_ctx->handler_ctx == &fsqvbl_CommandHandlerMemoryTableThermoCorrectionPhase_context )
                     {
                         if( 0 == NFMClass->methods.xThermo.getPointsThermoPhase_Init( portComb,
                                                                                       portState,
                                                                                       common_ctx->MemTableThermoCorrectionData.dDataPoints,
                                                                                       cellsof(common_ctx->MemTableThermoCorrectionData.dDataPoints),
                                                                                       &common_ctx->MemTableThermoCorrectionData.getctx ) )
                         {
                             goto L_TableTherCorrData_DATAERR;
                         }
                     }
                     else
                     {
                         my_assert(false);
                         goto L_TableTherCorrData_PARAMERR; // impossible
                     }

                     common_ctx->MemTableThermoCorrectionData.init = true;
                 }
                 #if SCPI_MAX_CMD_TEMP_BUFFER < PRINT_VALUE_MIN_SPACE
                 #error Invalid value 'SCPI_MAX_CMD_TEMP_BUFFER'
                 #endif
     
                 // The length of temporary string buffer, length of one printable number and number of cells in @MemTableThermoCorrectionData.dFreqPoints
                 // are interconnected. Number of cells @MemTableThermoCorrectionData.dFreqPoints should be equal or be greater than maximum number of
                 // printed numbers into the @tempBuffer. But it is recommend that these numbers be equal.
                 // Equal (no unused cells in @dDataPoints):
                 STATIC_ASSERT( (sizeof(common_ctx->tempBuffer) / PRINT_VALUE_MIN_SPACE) == cellsof(common_ctx->MemTableThermoCorrectionData.dDataPoints), "Invalid size" );
                 // Greater (there is unused cells in @dFreqPoints):
                 //STATIC_ASSERT( (sizeof(common_ctx->dDataPoints) / PRINT_VALUE_MIN_SPACE) <= cellsof(common_ctx->MemTableThermoCorrectionData.dDataPoints), "Invalid size" );

                 size_t nPointsRetrieve = sizeof(common_ctx->tempBuffer) / PRINT_VALUE_MIN_SPACE;

                 int32_t status = eNFMGetPointError_InvalidValue;
                 bool mode = false;
                 if( common_ctx->handler_ctx == &fsqvbl_CommandHandlerMemoryTableThermoCorrectionMagnitude_context )
                 {
                     mode = true; // magnitude
                     status = NFMClass->methods.xThermo.getPointsThermoMagn_Next( &common_ctx->MemTableThermoCorrectionData.getctx,
                                                                                  &nPointsRetrieve );
                 }
                 else if( common_ctx->handler_ctx == &fsqvbl_CommandHandlerMemoryTableThermoCorrectionPhase_context )
                 {
                     status = NFMClass->methods.xThermo.getPointsThermoPhase_Next( &common_ctx->MemTableThermoCorrectionData.getctx,
                                                                                   &nPointsRetrieve );
                 }

                 switch( status )
                 {
                     case eNFMGetPointError_Success:       common_ctx->MemTableThermoCorrectionData.isdone = true;
                     case eNFMGetPointError_OutOfBuffer:
                     case eNFMGetPointError_Limit:
                     {
                          size_t l = 0;

                          if( nPointsRetrieve > 0 )
                          {
                              l = printValues( common_ctx->tempBuffer, sizeof( common_ctx->tempBuffer ), 
                                               common_ctx->MemTableThermoCorrectionData.dDataPoints, mode, nPointsRetrieve, common_ctx->MemTableThermoCorrectionData.isdone );
                          }

                          common_ctx->tempBuffer[ l ] = '\0';

                          common_ctx->MemTableThermoCorrectionData.idx = 0;  // reset prepared characters index
                          common_ctx->MemTableThermoCorrectionData.nchs = l; // prepared characters in buffer
                     }
                     break;
                     case eNFMGetPointError_DataError:     goto L_TableTherCorrData_DATAERR;
                     case eNFMGetPointError_InvalidValue:  
                     default:                              goto L_TableTherCorrData_PARAMERR;
                 }
             }

             // 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_EXTDONE_IDXINC( common_ctx, common_ctx->MemTableThermoCorrectionData.idx, common_ctx->MemTableThermoCorrectionData.isdone );

             }
             // While output buffer swallows all the prepared data from @tempBuffer
             while( (common_ctx->MemTableThermoCorrectionData.idx >= common_ctx->MemTableThermoCorrectionData.nchs) );

             bool bufferEmpty = (common_ctx->MemTableThermoCorrectionData.idx >= common_ctx->MemTableThermoCorrectionData.nchs);

             SCPI_RESPONSE_HELPER_EXTDONE_SET( common_ctx, bufferEmpty && common_ctx->MemTableThermoCorrectionData.isdone );
        }
        break;
    }

    return nextstate;

L_TableTherCorrData_DATAERR:

    fsq_RaiseError( SCPI_ERROR_THERM_DATA_NOTFOUND, SCPI_ERROR_THERM_DATA_NOTFOUND_MSG,
                    global_ctx->sParser.xHandlerToken.shead,
                    global_ctx->sParser.xHandlerToken.stail );

    common_ctx->status = eProgramData_SpecificError; // specific error already generated
    return NULL;

L_TableTherCorrData_PARAMERR:
   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
   return NULL;
}

// @printValues
// Prints an array of double points into the buffer.
// If there no free space in the output buffer while not all the numbers are printed, function returns 0;
// @pcBuffer - output buffer;
// @szBuffer - size of output buffer;
// @pValues - an array of numbers to print;
// @mode - true for magnitude, false for phase;
// @nValues - amount of numbers to print;
// @theLastOne - the last call indicator
// Returns:
// - In case all of numbers are printed: number of bytes printed into the output buffer;
// - 0 otherwise
static size_t printValues( char * pcBuffer, size_t szBuffer, const sNFMChrzPoint_t * pValues, bool mode, size_t nValues, bool theLastOne )
{
    // One point has the following format:
    // <Magn>,<SPACE>
    // or
    // <Phase>,<SPACE>
    // where <magn> and <phase> is a floating point numbers: Mantissa+Exponent
    // Mantissa: sign, integer digit, dot, 10 floating point digits, exp-character, exp-sign, exponent value (2 digits)
    // So one number has 17 symbols length plus 2 (comma and space)
    // So, 19 characters required to print one point.
    // To prevent overflowing by _snprintf() with printing null-term,
    // it is required at lease 20 characters free, see @PRINT_VALUE_MIN_SPACE

    size_t idx = 0;
    while( nValues > 0 )
    {
        size_t l = 0;

        if( (szBuffer-idx) >= PRINT_VALUE_MIN_SPACE )
        {
            double value = (mode)?pValues->magn:pValues->phase;
            if( theLastOne && (nValues==1) )
            l = _snprintf( &pcBuffer[idx], (szBuffer-idx), "%+1.10e", value );
            else
            l = _snprintf( &pcBuffer[idx], (szBuffer-idx), "%+1.10e%c%c", value, ',' , ' ' );
            
            pValues++;
        }

        if( l == 0 || l >= (szBuffer-idx) )
        {
            return 0; // insuffecient buffer
        }

        idx += l;
        nValues--;
    }

    return idx;
}
