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

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

// Argument 1 Character Values allowed list / ACM Path
DECLARE_ARGUMENT_CHARACTER_ALLOWED_LIST_EXPORT( MemTable_AllowedValues_AcmPath, "A","B","C","D","AB","AC","AD","BC","BD","CD","CHECk" ); // Order is set by @ePortComb_t

// Argument 2 Character Values allowed list / S-parameter
DECLARE_ARGUMENT_CHARACTER_ALLOWED_LIST_EXPORT( MemTable_AllowedValues_SParam, "SHORt", "OPEN","LOAD","LOAD2","OPEN2","S11","S21","S12","S22","S13","S14","S23","S24", "S31","S32","S33","S34","S41","S42","S43","S44" ); // Order is set by @ePortStateId_t

// Argument 3 Character Values allowed list / Memory Bank
DECLARE_ARGUMENT_CHARACTER_ALLOWED_LIST_IMPORT( MemTable_AllowedValues_Bank );

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

static size_t printValues( char * pcBuffer, size_t szBuffer, const sNFMChrzPoint_t * pValues, size_t nValues, bool theLastOne );
static void fsqe_CommandHandlerMemoryTableData( const struct fFSeqEntry_t * this, tFSeqCtx_t ctx );
static void fsql_CommandHandlerMemoryTableData( const struct fFSeqEntry_t * this, tFSeqCtx_t ctx );
static const struct fFSeqEntry_t * fsqf_CommandHandlerMemoryTableData( const struct fFSeqEntry_t * this, tFSeqCtx_t ctx, const struct fFSeqEntry_t * * pDeferredNext );

const fFSeqVTable_t fsqvbl_CommandHandlerMemoryTableData = 
{
     .f = fsqf_CommandHandlerMemoryTableData,
     .enter = fsqe_CommandHandlerMemoryTableData,  
     .leave = fsql_CommandHandlerMemoryTableData
};

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

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

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

static const struct fFSeqEntry_t * fsqf_CommandHandlerMemoryTableData( 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->MemTableData.comb = SCPI_PROCESS_ARGUMENT_CHARACTER( common_ctx, MemTable_AllowedValues_AcmPath, 0 );

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

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

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

               // Get memory bank
               if( common_ctx->args > 2 )
               // process first argument (bank)
               common_ctx->MemTableData.bank = SCPI_PROCESS_ARGUMENT_CHARACTER( common_ctx, MemTable_AllowedValues_Bank, 2 );
               else
               // default parameter
               common_ctx->MemTableData.bank = 0; // Factory

               // check result
               if( SCPI_ARGUMENT_CHARACTER_INVALID_ID == common_ctx->MemTableData.bank ) break;  // eProgramDataIllegalArgument
               
               ePortStateId_t portState = (ePortStateId_t)((ePortStateId_MIN) + common_ctx->MemTableData.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->MemTableData.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->MemTableData.idx >= common_ctx->MemTableData.nchs )
             {
                 if( common_ctx->MemTableData.isdone ) break;

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

                     common_ctx->status = eProgramDataIllegalArgument; // forward set

                     if( 0 == NFMClass->methods.xCharacterization.getPoints_Init( tableId, 
                                                                                  portComb,
                                                                                  portState,
                                                                                  common_ctx->MemTableData.dDataPoints,
                                                                                  cellsof(common_ctx->MemTableData.dDataPoints),
                                                                                  &common_ctx->MemTableData.getctx ) )
                     {
                         goto L_TableData_DATAERR;
                     }
                     else
                     {
                         common_ctx->MemTableData.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 @MemTableData.dFreqPoints
                 // are interconnected. Number of cells @MemTableData.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->MemTableData.dDataPoints), "Invalid size" );
                 // Greater (there is unused cells in @dFreqPoints):
                 //STATIC_ASSERT( (sizeof(common_ctx->dDataPoints) / PRINT_VALUE_MIN_SPACE) <= cellsof(common_ctx->MemTableData.dDataPoints), "Invalid size" );

                 size_t nPointsRetrieve = sizeof(common_ctx->tempBuffer) / PRINT_VALUE_MIN_SPACE;
                 switch( NFMClass->methods.xCharacterization.getPoints_Next( &common_ctx->MemTableData.getctx,
                                                                             &nPointsRetrieve ) )
                 {
                     case eNFMGetPointError_Success:       common_ctx->MemTableData.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->MemTableData.dDataPoints, nPointsRetrieve, common_ctx->MemTableData.isdone );                          
                          }

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

                          common_ctx->MemTableData.idx = 0;  // reset prepared characters index
                          common_ctx->MemTableData.nchs = l; // prepared characters in buffer
                     }
                     break;
                     case eNFMGetPointError_DataError:     goto L_TableData_DATAERR;
                     case eNFMGetPointError_InvalidValue:  
                     default:                              goto L_TableData_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->MemTableData.idx, common_ctx->MemTableData.isdone );

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

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

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

    return nextstate;

L_TableData_DATAERR:

    // Formal call: in case the parameter is optional, the token is unfilled.
    // So it is required to fill the token with correct values from allowed list.
    SCPI_ARGUMENT_CHARACTER_VALUE_TOKEN( MemTable_AllowedValues_Bank,
                                         common_ctx->MemTableData.bank,
                                        &common_ctx->argTokens[2] );

    fsq_RaiseErrorEx( SCPI_ERROR_DATA_CORRUPTED, SCPI_ERROR_DATA_CORRUPTED_MSG,
                      common_ctx->argTokens[2].shead,
                      common_ctx->argTokens[2].stail,
                      global_ctx->sParser.xHandlerToken.shead,
                      global_ctx->sParser.xHandlerToken.stail );

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

L_TableData_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;
// @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, size_t nValues, bool theLastOne )
{
    // One point has the following format:
    // <Magn>,<SPACE>,<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*2=38 characters required to print one point.
    // To prevent overflowing by _snprintf() with printing null-term,
    // it is required at lease 40 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 )
        {
            if( theLastOne && (nValues==1) )
            l = _snprintf( &pcBuffer[idx], (szBuffer-idx), "%+1.10e%c%c%+1.10e", pValues->magn, ',' , ' ', pValues->phase );
            else
            l = _snprintf( &pcBuffer[idx], (szBuffer-idx), "%+1.10e%c%c%+1.10e%c%c", pValues->magn, ',' , ' ', pValues->phase, ',' , ' ' );

            pValues++;
        }

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

        idx += l;
        nValues--;
    }

    return idx;
}
