/*!\file ModelProcessorx
 * \brief: create datasets using input binary file and a set of requested analyses
 */

#ifdef HAVE_CONFIG_H
	#include <config.h>
#else
#error "Cannot compile with HAVE_CONFIG_H symbol! run configure first!"
#endif

#include "../../classes/classes.h"
#include "../../shared/shared.h"
#include "./ModelProcessorx.h"

void ModelProcessorx(Elements** pelements, Nodes** pnodes, Vertices** pvertices, Materials** pmaterials, Constraints** pconstraints, Loads** ploads, Parameters** pparameters, FILE* IOMODEL,char* rootpath,const int solution_type,const int nummodels,const int* analysis_type_list){

	int   i,analysis_type,verbose;
	bool  isthermal,ismasstransport,isstressbalance,isgroundingline,isenthalpy;

	/*output: */
	Elements    *elements    = NULL;
	Nodes       *nodes       = NULL;
	Vertices    *vertices    = NULL;
	Materials   *materials   = NULL;
	Constraints *constraints = NULL;
	Loads       *loads       = NULL;
	Parameters  *parameters  = NULL;

	/*Initialize IoModel from input file*/
	IoModel* iomodel = new IoModel(IOMODEL);

	/*Fetch parameters: */
	iomodel->Constant(&verbose,VerboseEnum);
	iomodel->Constant(&isthermal,TransientIsthermalEnum);
	iomodel->Constant(&isenthalpy,ThermalIsenthalpyEnum);
	iomodel->Constant(&ismasstransport,TransientIsmasstransportEnum);
	iomodel->Constant(&isstressbalance,TransientIsstressbalanceEnum);
	iomodel->Constant(&isgroundingline,TransientIsgroundinglineEnum);

	SetVerbosityLevel(verbose);

	if(VerboseMProcessor()) _printf0_("   starting model processor \n");

	for(i=0;i<nummodels;i++){

		analysis_type=analysis_type_list[i];

		/*Hack for trasient runs (FIXME: to be improved)*/
		if(solution_type==TransientSolutionEnum && analysis_type==ThermalAnalysisEnum  && iomodel->dim==2) continue;
		if(solution_type==TransientSolutionEnum && analysis_type==MeltingAnalysisEnum  && iomodel->dim==2) continue;
		if(solution_type==TransientSolutionEnum && analysis_type==EnthalpyAnalysisEnum && iomodel->dim==2) continue;
		if(solution_type==TransientSolutionEnum && analysis_type==ThermalAnalysisEnum && isthermal==false) continue;
		if(solution_type==TransientSolutionEnum && analysis_type==MeltingAnalysisEnum && isthermal==false) continue;
		if(solution_type==TransientSolutionEnum && analysis_type==EnthalpyAnalysisEnum && isthermal==false) continue;
		if(solution_type==TransientSolutionEnum && analysis_type==ThermalAnalysisEnum && isenthalpy==true) continue;
		if(solution_type==TransientSolutionEnum && analysis_type==MeltingAnalysisEnum && isenthalpy==true) continue;
		if(solution_type==TransientSolutionEnum && analysis_type==EnthalpyAnalysisEnum && isenthalpy==false) continue;
		if(solution_type==TransientSolutionEnum && analysis_type==MasstransportAnalysisEnum && ismasstransport==false && isgroundingline==false) continue;
		if(solution_type==TransientSolutionEnum && analysis_type==StressbalanceAnalysisEnum && isstressbalance==false) continue;
		if(solution_type==TransientSolutionEnum && analysis_type==StressbalanceVerticalAnalysisEnum && isstressbalance==false) continue;
		if(solution_type==TransientSolutionEnum && analysis_type==StressbalanceSIAAnalysisEnum && isstressbalance==false) continue;
		if(solution_type==SteadystateSolutionEnum && analysis_type==ThermalAnalysisEnum && isenthalpy==true) continue;
		if(solution_type==SteadystateSolutionEnum && analysis_type==MeltingAnalysisEnum && isenthalpy==true) continue;
		if(solution_type==SteadystateSolutionEnum && analysis_type==EnthalpyAnalysisEnum && isenthalpy==false) continue;

		if(VerboseMProcessor()) _printf0_("   creating datasets for analysis " << EnumToStringx(analysis_type) << "\n");
		CreateDataSets(&elements,&nodes,&vertices,&materials,&constraints,&loads,&parameters,iomodel,rootpath,solution_type,analysis_type,nummodels,i);
	}
	if(VerboseMProcessor()) _printf0_("   done with model processor \n");

	/*Free resources:*/
	delete iomodel;

	/*Assign output pointers:*/
	*pelements=elements;
	*pnodes=nodes;
	*pvertices=vertices;
	*pmaterials=materials;
	*pconstraints=constraints;
	*ploads=loads;
	*pparameters=parameters;

}
