#include "test.h"

int main(int argc, char* argv[])
{
	FILE* input;
	matrix Dm, Dp, D, Dc, Dcm, Dcp, Da, Dra;
	int XCount, update, i;
	int *X, *InFilled;
	int OutFilled[8];
	actn_r result;
	MemoryManager memory;
	char InputDesc[] = "matrix D, matrix Dm, matrix Dp, matrix Dc, matrix Dcm, matrix Dcp, arrayi X, int update";
	char OutputDesc[] = "matrix Da, matrix Dma, matrix Dpa, matrix Dra, matrix Dmra, matrix Dpra, arrayi TA, int Unique";
	
	/*Get the input file.*/
	if (! (input = ParseCmdLine(argc,argv))) return 0;	
	
	memory = CreateMemoryManager(10, 10, 0, 0);
	while (ParseStructure(input, InputDesc, &InFilled, &memory, &D, &Dm, &Dp, &Dc, &Dcm, &Dcp, &X, &XCount, &update))
	{
		/*Display the problem*/
		printf("Problem:\n");
		DisplayStructure(InputDesc, InFilled, &D, &Dm, &Dp, &Dc, &Dcm, &Dcp, X, XCount, update);
		
		/*See whether incidence or separate i/o matrices were provided and if the 
		former, then fill the i/o matrices.*/
		FillDmDp(InFilled, &D, &Dm, &Dp, &memory);
		FillDmDp(InFilled + 3, &Dc, &Dcm, &Dcp, &memory);
				
		/*Solve the problem*/
		//actn_r actn(matrix* Dm, matrix* Dp, int* X, int XCount, matrix *Dcm, matrix *Dcp, int update)
		result = actn((InFilled[0] || InFilled[1]) ? &Dm : 0, (InFilled[0] || InFilled[2]) ? &Dp : 0, InFilled[6] ? X : 0, XCount, (InFilled[3] || InFilled[4]) ? &Dcm : 0, (InFilled[3] || InFilled[5]) ? &Dcp : 0, InFilled[7] ? update : 0, 1);
		
		for (i = 0; i < 8; i++)
		{
			OutFilled[i] = 1;
		}
		if (HasSelfLoops(&result.Dma, &result.Dpa))
		{
			OutFilled[0] = 0;
		}
		else
		{
			Da = SubtractMatrix(&result.Dpa, &result.Dma, 0);
			ManageMatrix(&memory, &Da);
			OutFilled[1] = 0;
			OutFilled[2] = 0;
		}
		if (HasSelfLoops(&result.Dmra, &result.Dpra))
		{
			OutFilled[3] = 0;
		}
		else
		{
			Dra = SubtractMatrix(&result.Dpra, &result.Dmra, 0);
			ManageMatrix(&memory, &Dra);
			OutFilled[4] = 0;
			OutFilled[5] = 0;
		}
		
		/*Display the solution. If there are no self loops, display incidence 
		matrices instead of whole matrices.*/

		printf("Result:\n");
		DisplayStructure(OutputDesc, OutFilled, &Da, &result.Dma, &result.Dpa, &Dra, &result.Dmra, &result.Dpra, result.TA, result.TACount, result.unique);
		FreeMemory(&memory);
		DeallocateActn(&result);
		memory = CreateMemoryManager(10, 10, 0, 0);
		printf("-----------------------------------------------------------------\n");
	}
	FreeMemory(&memory);
	return 0;
}
