#ifdef NICE_USELIB_MEX
/** 
* @author Johannes Ruehle
* @date 25-04-2014
* @brief test MEX program for wrapping an OBJ::Examples class around a given feature and label matrix 
*/

// STL includes
#include <math.h>
#include <matrix.h>
#include <mex.h>

// NICE-core includes
#include <core/vector/MatrixT.h>
#include <core/vector/VectorT.h>

#include "vislearning/cbaselib/Example.h"

// Interface for conversion between Matlab and C objects
#include "gp-hik-core/matlab/classHandleMtoC.h"
#include "gp-hik-core/matlab/ConverterMatlabToNICE.h"
#include "gp-hik-core/matlab/ConverterNICEToMatlab.h"

using namespace std; //C basics
using namespace NICE;  // nice-core

// MAIN MATLAB FUNCTION
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{    
    // get the command string specifying what to do
    if (nrhs < 1)
        mexErrMsgTxt("No commands and options passed... Aborting!");        
    
    if( !mxIsChar( prhs[0] ) )
        mexErrMsgTxt("First argument needs to be the command, ie.e, the unit test method to call... Aborting!");
    
    std::string cmd = MatlabConversion::convertMatlabToString( prhs[0] );
    
    // in all other cases, there should be a second input,
    // which the be the class instance handle
    if (nrhs < 2)
    {
        mexErrMsgTxt("Second input should be some kind of matrix variable");
        return;
    }

    if (nlhs < 1)
    {
        mexErrMsgTxt("No return value defined, possible loss of data... Aborting!");
    }

    ////////////////////////////////////////
    //  Check which method to call  //
    ////////////////////////////////////////

    if ( !strcmp("convertDoubleMatrixToExamples", cmd.c_str() ) )
    {
        if (nrhs != 3)
        {
            mexErrMsgTxt("needs 2 matrix inputs, first the training features, second the sample labels");
            return;
        }

        const mxArray *t_pArrTrainData   = prhs[1];
        const mxArray *t_pArrTrainLabels = prhs[2];

        NICE::Vector t_vecLabelsTrain = MatlabConversion::convertDoubleVectorToNice( t_pArrTrainLabels );
        NICE::Matrix t_matDataTrain   = MatlabConversion::convertDoubleMatrixToNice( t_pArrTrainData   );

        OBJREC::Examples t_ExamplesTrain;

        bool bConversionSuccess = OBJREC::Examples::wrapExamplesAroundFeatureMatrix( t_matDataTrain, t_vecLabelsTrain, t_ExamplesTrain );
        //wrapExamplesAroundFeatureMatrix

        std::cerr << "Examples size: " << t_ExamplesTrain.size() << std::endl;
        for(int i=0; i< t_ExamplesTrain.size(); i++)
        {
            int iClass = t_ExamplesTrain[i].first;
            OBJREC::Example &t_Example = t_ExamplesTrain[i].second ;

            std::cerr << "Example["<<i<<"]" << "L:" << iClass << " data: "<< *t_Example.vec << std::endl;

        }

        // clean up
        t_ExamplesTrain.clean();

        // output
        plhs[0] = mxCreateLogicalScalar( bConversionSuccess );

        return;
    }    
    
    
    // Got here, so command not recognized
    
    std::string errorMsg (cmd.c_str() );
    errorMsg += " -- command not recognized.";
    mexErrMsgTxt( errorMsg.c_str() );

}
#endif
