#include "Interpolation.h"




vector<Mesh*> readPath(vector<Node*> mypath, string foldername, Graph* mygraph, string format) {
	//cout << "reading path:" << endl;
	if (!mygraph) { // if graph is not provided with all meshes read
		vector<Mesh*> meshlist;
		for (unsigned int i = 0; i < mypath.size(); i++) {

			//cout << "mypath[" << i << "]= " << mypath[i]->meshId << endl;

			Mesh* current = new Mesh();
			current->readID(mypath[i]->meshId, foldername, format);
			meshlist.push_back(current);
		}
		return meshlist;
	}
	else { //I have all the meshes already read
		vector<Mesh*> meshlist;
		for (unsigned int i = 0; i < mypath.size(); i++) {

			//	cout << "mypath[" << i << "]= " << mypath[i]->meshId << endl;

			Mesh* current = new Mesh();
			//current->readID(mypath[i]->meshId, foldername, "off");
			meshlist.push_back(mygraph->allmeshes[mypath[i]->meshId]);
		}
		//cout << "done reading path" << endl;
		//cout << "meshlist size: " << meshlist.size() << endl;
		return meshlist;
	}
}



void partinter(Graph* mygraph, string format, string foldername, int mesh1id, int mesh2id,
	string distfilename, int noofintermeshes, const vector<vector<int>> &parts_vertlists,  int noofmeshes) {

		vector<vector<Mesh*>> interpolatedlist_list;


	for (int t = 0; t < noofintermeshes; t++) {
		vector<Mesh*> interpolatedlist;
		interpolatedlist_list.push_back(interpolatedlist);

	}

	for (unsigned int i = 0; i < parts_vertlists.size(); i++) {
		//for each part, create a path
		vector<Node*> mypath;

		mypath = mygraph->createPath(distfilename, noofmeshes, mesh1id, mesh2id, i);

		vector<Mesh*> meshlist = readPath(mypath, foldername, mygraph, format);
		cout << "meshlist size: " << meshlist.size() << endl;

		for (int t = 0; t < noofintermeshes; t++) {
			//for each step fill the list for given part and continue to the next part

			Mesh* intermesh = new Mesh();
			intermesh->readID(0, foldername, format);

			float interpolamount = (t + 1) / float(noofintermeshes + 1); // if 1, then, 0.5 |if 3, 1/4, 2/4, 3/4

			cout << "t:" << t << " interpol amount: " << interpolamount << endl;

			bezierNmesh(meshlist, interpolamount, intermesh, false);

			interpolatedlist_list[t].push_back(intermesh);
		}
	}


	cout << "size of list_list: " << interpolatedlist_list.size() << endl;

	for (int t = 0; t < noofintermeshes; t++) {
		cout << "inner list sizes: " << interpolatedlist_list[t].size() << endl;
	}

	for (int t = 0; t < noofintermeshes; t++) {

		//t is the intermediate mesh no

		Mesh* resultmesh = new Mesh();
		resultmesh->readID(0, foldername, format);
		for (unsigned int i = 0; i < parts_vertlists.size(); i++) {
			cout << "i:" << i << endl;
			for (unsigned j = 0; j < parts_vertlists[i].size(); j++) {
				resultmesh->verts[parts_vertlists[i][j]]->coords[0] = interpolatedlist_list[t].at(i)->verts[parts_vertlists[i][j]]->coords[0];
				resultmesh->verts[parts_vertlists[i][j]]->coords[1] = interpolatedlist_list[t].at(i)->verts[parts_vertlists[i][j]]->coords[1];
				resultmesh->verts[parts_vertlists[i][j]]->coords[2] = interpolatedlist_list[t].at(i)->verts[parts_vertlists[i][j]]->coords[2];
			}
		}

		resultmesh->AlignParts(parts_vertlists, parts_vertlists.size(), format);//local fairing inside
		//resultmesh->fairing_N(20);//global fairing



		//if (format == "centaur") {
		//	cout << "placing on ground" << endl;
		//	place_on_ground(resultmesh);
		//}

		string filename =   format + to_string(mesh1id) + "_" + to_string(mesh2id) + "_"
				+to_string(parts_vertlists.size()) + "parts"+ to_string(float(t+1) / (noofintermeshes+1)).substr(0, 4) +".off";
		
		resultmesh->WriteOff(filename);
	}

	return;
}



void notpartitioned(Graph* mygraph, string format, string foldername, int mesh1id, int mesh2id,
	string distfilename, int noofintermeshes,int noofmeshes) {

	cout << "entering notpartitioned" << endl;

	for (int k = 0; k < noofintermeshes; k++) {
		Mesh* intermesh = new Mesh();
		intermesh->readID(0, foldername, format);
		vector<Node*> mypath;

		mypath = mygraph->createPath(distfilename, noofmeshes, mesh1id, mesh2id, 1);

		vector<Mesh*> meshlist = readPath(mypath, foldername, mygraph, format);

		float interpolamount = (k + 1) / float(noofintermeshes + 1); // if 1, then, 0.5 |if 3, 1/4, 2/4, 3/4
		cout << "interpolamount" << endl;
		bezierNmesh(meshlist, interpolamount, intermesh, false);
		

		string filename = format + to_string(mesh1id) + "_" + to_string(mesh2id) + "_"
			"1part" + to_string(float(k + 1) / (noofintermeshes + 1)).substr(0, 4) + ".off";
		cout << "writing " << filename << endl;
		intermesh->WriteOff(filename);
	}


}