/**
 * @file JAWFRecordGAM.cpp
 * @brief Source file for class JAWFRecordGAM
 * @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 source file contains the definition of all the methods for
 * the class JAWFRecordGAM (public, protected, and private). Be aware that some
 * methods, such as those inline could be defined on the header file, instead.
 */

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

/*---------------------------------------------------------------------------*/
/*                         Project header includes                           */
/*---------------------------------------------------------------------------*/
#include "JAWFRecordGAM.h"

#include "AdvancedErrorManagement.h"
#include "CLASSMETHODREGISTER.h"
#include "File.h"
#include "MessageI.h"
#include "RegisteredMethodsMessageFilter.h"

/*---------------------------------------------------------------------------*/
/*                           Static definitions                              */
/*---------------------------------------------------------------------------*/

// How often output signals are updated.
const MARTe::uint32 cycleMs = 10u;

/*---------------------------------------------------------------------------*/
/*                           Method definitions                              */
/*---------------------------------------------------------------------------*/

JAWFRecordGAM::JAWFRecordGAM() :
        GAM() {
    filenameSignalIndex = 0u;
    timeSignal = NULL_PTR(MARTe::int32 *);
    loadTriggerSignal = NULL_PTR(MARTe::uint32 *);
    valueSignals = NULL_PTR(MARTe::float32 **);
    maxElements = 0u;
    readOnce = true;
}

JAWFRecordGAM::~JAWFRecordGAM() {
    if (valueSignals != NULL_PTR(MARTe::float32 **)) {
        delete[] valueSignals;
    }
}

bool JAWFRecordGAM::Initialise(MARTe::StructuredDataI & data) {
    using namespace MARTe;
    bool ok = GAM::Initialise(data);
    if (ok) {
        ok = data.Read("Directory", directory);
        if (!ok) {
            REPORT_ERROR(MARTe::ErrorManagement::ParametersError, "The Directory shall be specified");
        }
    }
    return ok;
}

bool JAWFRecordGAM::Setup() {
    using namespace MARTe;
    bool ok = (numberOfInputSignals == 2u);
    if (ok) {
        ok = (numberOfOutputSignals > 1u);
        if (!ok) {
            REPORT_ERROR(MARTe::ErrorManagement::ParametersError, "At least two output signal shall be defined");
        }
    }
    else {
        REPORT_ERROR(MARTe::ErrorManagement::ParametersError, "Two input signal with the filename and file load trigger shall be defined");
    }
    if (ok) {
        StreamString signalName = "Filename";
        ok = GetSignalIndex(InputSignals, filenameSignalIndex, signalName.Buffer());
        if (!ok) {
            REPORT_ERROR(MARTe::ErrorManagement::ParametersError, "Filename input signal shall be defined");
        }
        else {
            TypeDescriptor inputType = GetSignalType(InputSignals, filenameSignalIndex);
            ok = (inputType == CharString);
            if (!ok) {
                ok = (inputType == Character8Bit);
            }
            if (!ok) {
                StreamString signalName;
                (void) GetSignalName(InputSignals, filenameSignalIndex, signalName);
                REPORT_ERROR(MARTe::ErrorManagement::ParametersError, "Signal %s shall be defined as string", signalName.Buffer());
            }
        }
    }
    if (ok) {
        StreamString signalName = "CSV_LOAD";
        uint32 loadSignalIndex;
        ok = GetSignalIndex(InputSignals, loadSignalIndex, signalName.Buffer());
        if (!ok) {
            REPORT_ERROR(MARTe::ErrorManagement::ParametersError, "CSV_LOAD input signal shall be defined");
        }
        else {
            TypeDescriptor inputType = GetSignalType(InputSignals, loadSignalIndex);
            ok = (inputType == UnsignedInteger32Bit);
            if (!ok) {
                StreamString signalName;
                (void) GetSignalName(InputSignals, loadSignalIndex, signalName);
                REPORT_ERROR(MARTe::ErrorManagement::ParametersError, "Signal %s shall be defined as uint32", signalName.Buffer());
            } else {
                loadTriggerSignal = reinterpret_cast<uint32 *>(GetInputSignalMemory(loadSignalIndex));
            }
        }
    }
    if (ok) {
        TypeDescriptor timeType = GetSignalType(OutputSignals, 0);

        ok = (timeType == SignedInteger32Bit);
        if (!ok) {
            StreamString signalName;
            (void) GetSignalName(InputSignals, 0, signalName);
            REPORT_ERROR(MARTe::ErrorManagement::ParametersError, "Signal %s shall be defined as int32", signalName.Buffer());
        }

        uint32 i;
        for (i = 1u; (i < numberOfOutputSignals) && (ok); i++) {
            TypeDescriptor outputType = GetSignalType(OutputSignals, i);
            ok = (outputType == Float32Bit);
            if (!ok) {
                StreamString signalName;
                (void) GetSignalName(OutputSignals, i, signalName);
                REPORT_ERROR(MARTe::ErrorManagement::ParametersError, "Signal %s shall be defined as float32", signalName.Buffer());
            }
            else {
                uint32 dimentionsCount;
                ok = GetSignalNumberOfDimensions(OutputSignals, 0u, dimentionsCount);
                if (!ok) {
                    REPORT_ERROR(MARTe::ErrorManagement::ParametersError, "Output signals shall have 1 dimension.");
                }
                else {
                    uint32 elementsCount;
                    ok = GetSignalNumberOfElements(OutputSignals, 0u, elementsCount);
                    if (maxElements == 0) {
                        maxElements = elementsCount;
                    }
                    else {
                        ok = (maxElements == elementsCount);
                        if (!ok) {
                            REPORT_ERROR(MARTe::ErrorManagement::ParametersError, "Output signals shall have consistent number of elements.");
                        }
                    }
                }
            }
        }
    }
    if (ok) {
        valueSignals = new float32*[numberOfOutputSignals - 1u];
        uint32 i;
        for (i = 1u; i < numberOfOutputSignals; i++) {
            valueSignals[i - 1] = reinterpret_cast<float32 *>(GetOutputSignalMemory(i));
        }
        timeSignal = reinterpret_cast<int32 *>(GetOutputSignalMemory(0));
    }
    return ok;
}

bool JAWFRecordGAM::PrepareNextState(const MARTe::char8 * const currentStateName, const MARTe::char8 * const nextStateName) {
    if (strcmp(nextStateName, "WaitReady") == 0) {
        // Reset read once flag when reentering WaitReady state.
        readOnce = true;
    }
    return true;
}

void JAWFRecordGAM::LoadFile() {
    using namespace MARTe;

    bool ok = true;

    const char8 * const filenameSignal = reinterpret_cast<const char8 * const >(GetInputSignalMemory(filenameSignalIndex));
    StreamString filename = directory;
    filename += DIRECTORY_SEPARATOR;
    filename += filenameSignal;
    REPORT_ERROR(ErrorManagement::Debug, "Opening file %s", filename.Buffer());

    File f;
    ok = f.Open(filename.Buffer(), BasicFile::ACCESS_MODE_R);

    uint32 numberOfPreProgrammedTimeRows = 0u;
    int32 *preProgrammedTime;
    float32 **preProgrammedValues;
    uint32 numberOfPreProgrammedValues = 0u;

    if (ok) {
        StreamString tokenLine;
        while (f.GetLine(tokenLine)) {
            if (tokenLine[0] != '#') {
                numberOfPreProgrammedTimeRows++;
            }
            tokenLine = "";
        }
        f.Seek(0);
        uint32 t = 0u;
        tokenLine = "";
        while ((ok) && (f.GetLine(tokenLine))) {
            if (tokenLine[0] == '#') {
                tokenLine = "";
                continue;
            }
            if (numberOfPreProgrammedValues == 0) {
                StreamString token;
                char8 ignored;
                tokenLine.Seek(0);
                while (tokenLine.GetToken(token, ",", ignored)) {
                    numberOfPreProgrammedValues++;
                    token = "";
                }
                ok = numberOfPreProgrammedValues == numberOfOutputSignals;
                // Remove time from pre-programmed values count.
                numberOfPreProgrammedValues -= 1u;
                if (ok) {
                    preProgrammedTime = new int32[maxElements];
                    memset(preProgrammedTime, 0, maxElements * sizeof(int32));
                    preProgrammedValues = new float32*[numberOfPreProgrammedValues];
                    uint32 j;
                    for (j = 0u; j < numberOfPreProgrammedValues; j++) {
                        preProgrammedValues[j] = new float32[maxElements];
                        memset(preProgrammedValues[j], 0, maxElements * sizeof(float32));
                    }
                }
                else {
                    REPORT_ERROR(MARTe::ErrorManagement::ParametersError, "Number of columns in csv file (%d) is not consistent with the number of output signals (%d)", numberOfPreProgrammedValues,
                                 numberOfOutputSignals - 1u);
                }
            }
            if (ok) {
                StreamString token;
                char8 ignored;
                tokenLine.Seek(0);
                uint32 idx = 0u;
                while (tokenLine.GetToken(token, ",", ignored)) {
                    if (idx == 0u) {
                        preProgrammedTime[t] = atoi(token.Buffer());
                    }
                    else {
                        ok = ((idx - 1) < numberOfPreProgrammedValues);
                        if (ok) {
                            preProgrammedValues[idx - 1][t] = static_cast<float32>(atof(token.Buffer()));
                        }
                        else {
                            REPORT_ERROR(ErrorManagement::FatalError, "Number of columns in csv file is not consistent with the number of output signals in line %d", t);
                        }
                    }
                    token = "";
                    idx++;
                }
                t++;
            }
            tokenLine = "";
        }
        f.Close();
    } else {
        REPORT_ERROR(ErrorManagement::Debug, "Cannot open the file %s", filename.Buffer());
        return;
    }
    if (numberOfPreProgrammedTimeRows > maxElements) {
        REPORT_ERROR(ErrorManagement::Warning, "Only %d (out of %d) of pre-programmed values will be written to waveform record.",
                maxElements, numberOfPreProgrammedTimeRows);
    }
    ok = MemoryOperationsHelper::Copy(timeSignal, preProgrammedTime, maxElements * sizeof(int32));
    uint32 i;
    for (i = 0u; (i < numberOfPreProgrammedValues) && (ok); ++i) {
        ok = MemoryOperationsHelper::Copy(valueSignals[i], preProgrammedValues[i], maxElements * sizeof(float32));
    }
    if (!ok) {
        REPORT_ERROR(ErrorManagement::Warning, "Failed to write data to waveform record.");
    }

    if (preProgrammedValues != NULL_PTR(float32 **)) {
        uint32 i;
        for (i = 0u; i < numberOfPreProgrammedValues; i++) {
            delete preProgrammedValues[i];
        }
        delete[] preProgrammedValues;
    }
    if (preProgrammedTime != NULL_PTR(int32 *)) {
        delete[] preProgrammedTime;
    }
}

bool JAWFRecordGAM::Execute() {
    if (*loadTriggerSignal == 1u) {
        if (readOnce) {
            LoadFile();
            readOnce = false;
        }
    } else {
        // Reset read once flag when loadTriggerSignal is reset.
        readOnce = true;
    }
    return true;
}

CLASS_REGISTER(JAWFRecordGAM, "1.0")
