
#include "Partition.h"
#include "Interpolation.h"

#include <Vector>
#include <string>

#include <iostream>
#include <fstream>
#include <sstream>



int main(int argc, char *argv[]) {

	//if (argc != 5){
	//	cout << "Not valid arguments , enter dataset, start mesh, end mesh and number of parts" << endl;
	//	return 0;
	//}

	//string dataset = argv[1];
	//istringstream ss1(argv[2]);
	//istringstream ss2(argv[3]);
	//int mesh1id, mesh2id;
	//ss1 >> mesh1id;
	//ss2 >> mesh2id;
	//string noofparts = argv[4];

	cout << "Enter dataset(scape, hand, cat, horse, dancer), startmeshno, endmeshno, numberofparts" << endl;

	string dataset;
	int mesh1id, mesh2id;
	string noofparts;
	cin >> dataset;
	cin >> mesh1id;
	cin >> mesh2id;
	cin >> noofparts;

	string foldername = "..//Meshes//";

	string format = "";
	int noofmeshes = 0;
	string distfile = "";

	foldername += dataset + "//";
	format = dataset;

	if (dataset == "scape" || dataset == "Scape"){
		if (mesh1id >= 70 || mesh2id >= 70){
			cout << "not valid mesh id" << endl;
			return 0;
		}
	//	foldername += "Scape//";
	//	format = "scape";
		noofmeshes = 71;

	}
	else if (dataset == "hand" || dataset == "Hand"){
		if (mesh1id >= 30 || mesh2id >= 30){
			cout << "not valid mesh id" << endl;
			return 0;
		}
	//	foldername += "Hand//";
	//	format = "handDb";
		noofmeshes = 31;
	}
	else if (dataset == "cat" || dataset == "Cat"){
		if (mesh1id >= 9 || mesh2id >= 9){
			cout << "not valid mesh id" << endl;
			return 0;
		}
	//	foldername += "Cat//";
	//	format = "catdb";
		noofmeshes = 10;
	}
	else if (dataset == "horse" || dataset == "Horse"){
		if (mesh1id >= 10 || mesh2id >= 10){
			cout << "not valid mesh id" << endl;
			return 0;
		}
	//	foldername += "Horse//";
	//	format = "horsedb";
		noofmeshes = 11;
	}
	else if (dataset == "dancer" || dataset == "Dancer"){
		if (mesh1id >= 26 || mesh2id >= 26){
			cout << "not valid mesh id" << endl;
			return 0;
		}
	//	foldername += "Dancer//";
	//	format = "dancerdb";
		noofmeshes = 27;
	}
	else{
		cout << "Not a valid dataset, try scape, hand, cat, horse or dancer" << endl;
		return 0;
	}


	cout << "Using " << dataset << " Dataset" << " Start mesh: " << mesh1id << " End mesh: " << mesh2id << " Using " << noofparts << " Parts" << endl;

	distfile = distfile + format + noofparts;
	int noofpartsint = stoi(noofparts);
	
	cout << "distfile for " << format << " and noofparts: " << noofpartsint << " is: " << distfile << endl;


	vector<vector<int>> parts_vertlists;

	if (noofparts != "1") {//divide into parts
		parts_vertlists = divide(foldername, format, noofpartsint);
		if (parts_vertlists.empty()) return 0;
	}
	

	Graph* mygraph = new Graph();
	mygraph->readMeshes(foldername, noofmeshes, format); // read all meshes

	if (ifstream(&distfile[0u])){//if distfile exists, continue
		cout << "distfile exist continue" << endl;
	}
	else{//if no distfile, write it
		cout << "writing the file" << endl;
		if (noofparts == "1") {
			mygraph->writeDists(distfile);
		}
		else {
			mygraph->WritePartDists(distfile, parts_vertlists);
		}
	}

	int numberofintermediatemeshes = 1;
	//number of interpolated meshes = 1, -> interpol amount becomes 0.5
	//if the number is changed, creates that many interpolated meshes are outputted
	if (noofparts == "1") {
		notpartitioned(mygraph, format, foldername, mesh1id, mesh2id,
			distfile, numberofintermediatemeshes,  noofmeshes);
	}
	else {

		partinter(mygraph, format, foldername, mesh1id, mesh2id, distfile,
			numberofintermediatemeshes, parts_vertlists, noofmeshes);

	}







	return 0;

}
