/**
 * @file JAPreProgrammedGAM.h
 * @brief Header file for class JAPreProgrammedGAM
 * @date Jan, 2019
 * @author rhari
 *
 * @copyright Copyright 2015 F4E | European Joint Undertaking for ITER and
 * the Development of Fusion Energy ('Fusion for Energy').
 * Licensed under the EUPL, Version 1.1 or - as soon they will be approved
 * by the European Commission - subsequent versions of the EUPL (the "Licence")
 * You may not use this work except in compliance with the Licence.
 * You may obtain a copy of the Licence at: http://ec.europa.eu/idabc/eupl
 *
 * @warning Unless required by applicable law or agreed to in writing,
 * software distributed under the Licence is distributed on an "AS IS"
 * basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the Licence permissions and limitations under the Licence.

 * @details This header file contains the declaration of the class JAPreProgrammedGAM
 * with all of its public, protected and private members. It may also include
 * definitions for inline methods which need to be visible to the compiler.
 */

#ifndef GAMS_JAPREPROGRAMMEDGAM_H_
#define GAMS_JAPREPROGRAMMEDGAM_H_

/*---------------------------------------------------------------------------*/
/*                        Standard header includes                           */
/*---------------------------------------------------------------------------*/

/*---------------------------------------------------------------------------*/
/*                        Project header includes                            */
/*---------------------------------------------------------------------------*/
#include "GAM.h"
#include "Message.h"

/*---------------------------------------------------------------------------*/
/*                           Class declaration                               */
/*---------------------------------------------------------------------------*/

/**
 * @brief GAM that update PS output value for pre-programmed operation.
 *
 * The configuration syntax is (names and signal quantity are only given as an example):
 * <pre>
 *         +PreProgrammedGAM = {
 *           Class = JAPreProgrammedGAM
 *           Directory = "../Configurations" // Directory which has pre-pro configuration file.
 *           PreProgrammedPeriodMs = 1       // RFON state is executed every millisecond.
 *           InputSignals = {
 *               CSV_LOAD = {
 *                   DataSource = EPICSCAInput
 *                   Type = uint32
 *               }
 *               Filename = {
 *                   Alias = CSV_NAME
 *                   DataSource = EPICSCAInput
 *               }
 *               FHPS_REF = {
 *                   DataSource = DDB1
 *                   Type = float32
 *               }
 *               RFON = {
 *                   DataSource = RealTimeThreadAsyncBridge
 *                   Type = uint32
 *               }
 *           }
 *           OutputSignals = {
 *               GYA_PREHEAT_TIME = {
 *                   DataSource = DDB1
 *                   Type = int32
 *               }
 *               MHVPS_REF = {
 *                   DataSource = DDB1
 *                   Type = float32
 *               }
 *               BPS_REF = {
 *                   DataSource = DDB1
 *                   Type = float32
 *               }
 *               APS_REF = {
 *                   DataSource = DDB1
 *                   Type = float32
 *               }
 *               MCPS_TRG_CURR_SET = {
 *                   DataSource = DDB1
 *                   Type = float32
 *               }
 *               GCPS_TRG_CURR_SET = {
 *                   DataSource = DDB1
 *                   Type = float32
 *               }
 *               FHPS_REF = {
 *                   DataSource = DDB1
 *                   Type = float32
 *               }
 *               CSV_LOADED = {
 *                   DataSource = DDB1
 *                   Type = uint32
 *               }
 *               CSV_ERR = {
 *                   DataSource = DDB1
 *                   Type = uint32
 *               }
 *           }
 *       }
 * </pre>
 * 
 */

class JAPreProgrammedGAM : public MARTe::GAM, public MARTe::StatefulI {
public:
    CLASS_REGISTER_DECLARATION()

    JAPreProgrammedGAM();

    virtual ~JAPreProgrammedGAM();

    virtual bool Initialise(MARTe::StructuredDataI & data);

    virtual bool Setup();

    virtual bool Execute();

    virtual bool PrepareNextState(const MARTe::char8 * const currentStateName,
                                  const MARTe::char8 * const nextStateName);

private:
    bool LoadFile();

    void DeleteArrays();

    //Parameters
    MARTe::StreamString directory; //Location for prepro configuration file.
    MARTe::uint32 preProgrammedExecutaionPeriodMs; // Time between to execution cycles in milliseconds in pre-programmed mode.

    //Input Signals
    MARTe::uint32 *loadTriggerSignal; //index:0
    MARTe::uint32 filenameSignalIndex;//index:1
    MARTe::float32 *fhpsrefSignal;    //index:2
    MARTe::uint32 *rfonStateSignal;   //index:3

    //Output Signals
    MARTe::int32 *timeSignal;          //index:0
    MARTe::float32 **valueSignals;     //index:1-5 = BPS,APS,MC,GC,FHPS
    MARTe::uint32 *fileLoadedSignal;   //index:6
    MARTe::uint32 *fileLoadErrorOutput;//index:7

    //Internal variables
    MARTe::int32 *preProgrammedTime;
    MARTe::float32 **preProgrammedValues;

    MARTe::uint32 startTime;

    //Number of columns in csv, EXCLUDING the time
    MARTe::uint32 numberOfPreProgrammedValues; //is 5.(BPS, APS, MC, GC, FHPS)
    MARTe::uint32 numberOfPreProgrammedTimeRows; //This start from t=0 row.
    MARTe::uint32 currentRow;

    // Number of milliseconds since the last time output signals were updated.
    MARTe::uint32 msCounter;

    // Row number where the pre-programmed data begins (time == 0).
    MARTe::uint32 preProgrammedRow;

    enum OperationMode {
        LoadFileMode, PreProgrammedMode, None
    };
    enum PreProTableDefinition {
        Time, MHVPS, BPS, APS, MCPS, GCPS, FHPS
    };

    OperationMode mode;

    bool resetOutputSignals;

    // Flag determining whether file should be read (so it isn't read multiple time)
    bool readOnce;
};



/*---------------------------------------------------------------------------*/
/*                        Inline method definitions                          */
/*---------------------------------------------------------------------------*/

#endif /* GAMS_JAPREPROGRAMMEDGAM_H_ */
