/*!
\file
\brief wrapper function for MixEF.init() + MixEF.bayes_batch()


 */

#include <estim/mixtures.h>
#include <estim/arx.h>

using namespace bdm;

#ifdef MEX
#include <mex/mex_parser.h>

void mexFunction ( int n_output, mxArray *output[], int n_input, const mxArray *input[] ) {
	// Check the number of inputs and output arguments
	if ( n_input<1 ) mexErrMsgTxt ( "Usage:\n"
		                                "Mix=mixef_init(Data, Com1)\n"
		                                "  Data = [];                                 % matrix of data \n"
		                                "  Com1 = struct('class','BMEF_offspring');   % A typical component \n"
		                                "output:\n"
		                                "  structure with description of mixture." );

	//input check
	if (!mxIsNumeric(input[0])) mexErrMsgTxt("Given input #1 is not a data matrix.");
	if (n_input<2) mexErrMsgTxt("No initial component, dont what type of mixture to fit.");
	if (!mxIsStruct(input[1])) mexErrMsgTxt("Given input #2 is not a struct.");
	//output check
	if ( n_output<1 ) mexErrMsgTxt ( "No output - nothing to do!" );

	// act
	RV::clear_all();

	mat Data=mxArray2mat(input[0]);
	
	UImxArray Cfg(input[1]);
	Cfg.writeFile("mixef_init.cfg");

#else
int main() {
	UIFile Cfg ( "mixef_init.cfg" );
	mat Data=randn(2,100); // <<<<<<<<< ============== beware!!!
#endif
	
	shared_ptr<BMEF> com1=UI::build<BMEF>(Cfg.getRoot());
	MixEF mix;
	mix.init(&(*com1),Data,10);
	mix.bayes_batch(Data,zeros(0,Data.cols()),ones(Data.cols()));
	
#ifdef MEX	
	mxArray* tmp= mxCreateStructMatrix(1,1,0, NULL);
	UImxArray out(tmp);
	mix.to_setting(out);
	output[0]=tmp;
#else
	UIFile out;
	UI::save ( &mix, out, "test" );
	out.save ( "mixef_init.out" );
#endif
}
