#include "Util.h"
#include <windows.h>
#include <sys/stat.h>
#include <sstream>


Util* Util::instance;


bool Util::fileExists(string filename)
{
	struct stat stFileInfo;
	bool fileExists;
	int intStat;

	intStat = stat(filename.c_str(),&stFileInfo);
	if(intStat == 0)
	{
		fileExists = true;
	}
	else
	{
		fileExists = false;
	}

	return(fileExists);
}

wstring Util::stringToUnicode(string s)
{
	int len;
	int slength = (int)s.length() + 1;
	len = MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, 0, 0);
	wchar_t* buf = new wchar_t[len];
	MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, buf, len);
	wstring r(buf);
	delete[] buf;
	return r;
}

string Util::intToString(int integer, int minDigits)
{
	stringstream ss;
	int maxNumberWithDigits = (minDigits > 0 ? (int)pow(10.0f,minDigits-1) : 0)-1;

	while(maxNumberWithDigits > integer)
	{
		ss << "0";
		maxNumberWithDigits /= 10;
	}
	ss << integer;
	ss.flush();

	return ss.str();
}

string Util::floatToString(float value) {
	std::ostringstream out;
	out << value; 
	std::string str(out.str());
	return str; 
}

void Util::writeToFile(vector<float> record_time, vector<vector<D3DXVECTOR3>> record_data) {
	
	file.open("cameraCoordinates.txt", fstream::out); 
	for(int i=0; i < (int)record_time.size(); i++) {
		vector<D3DXVECTOR3> temp_data = record_data.at(i); 
		D3DXVECTOR3 pos = temp_data.at(0); 
		D3DXVECTOR3 lookAt = temp_data.at(1); 
		D3DXVECTOR3 up = temp_data.at(2); 

		file << record_time.at(i) << ";" << pos.x << ";" << pos.y << ";" << pos.z << ";"; 
		file << lookAt.x << ";" << lookAt.y << ";" << lookAt.z << ";"; 
		file << up.x << ";" << up.y << ";" << up.z << ";";  
		
		if(i < (int)record_time.size()-1)
			file << endl;
		
	}

	file.close();
	

}

void Util::getDataFromFile(vector<vector<D3DXVECTOR3>>* data, vector<float>* time) {
		
	file.open("cameraCoordinates.txt", fstream::in);  
	
	D3DXVECTOR3 pos, lookAt, up;
	vector<D3DXVECTOR3> temp_vec; 
	vector<string> splitBuffer; 
	string line; 
	int lineCount = 0; 
	while(file.good()){
		getline(file, line);
		splitString(line, ';', &splitBuffer);

		up.z = Util::stringToFloat(splitBuffer.back()); 
		splitBuffer.pop_back();
		up.y = Util::stringToFloat(splitBuffer.back()); 
		splitBuffer.pop_back();
		up.x = Util::stringToFloat(splitBuffer.back()); 
		splitBuffer.pop_back();
		lookAt.z = Util::stringToFloat(splitBuffer.back()); 
		splitBuffer.pop_back();
		lookAt.y = Util::stringToFloat(splitBuffer.back()); 
		splitBuffer.pop_back();
		lookAt.x = Util::stringToFloat(splitBuffer.back()); 
		splitBuffer.pop_back();
		pos.z = Util::stringToFloat(splitBuffer.back()); 
		splitBuffer.pop_back();
		pos.y = Util::stringToFloat(splitBuffer.back()); 
		splitBuffer.pop_back();
		pos.x = Util::stringToFloat(splitBuffer.back()); 
		splitBuffer.pop_back();
		time->push_back(Util::stringToFloat(splitBuffer.back()));
		splitBuffer.pop_back();
		temp_vec.push_back(pos); 
		temp_vec.push_back(lookAt); 
		temp_vec.push_back(up); 

		data->push_back(temp_vec); 

		temp_vec.clear(); 

		lineCount++;
	}

	file.close();
}



void Util::splitString(string const& line, const char delimiter, vector<string>* buffer) {
	
	std::size_t last_position(0);
	std::size_t position(0);
         
    for (std::string::const_iterator it(line.begin()); it != line.end(); ++it, ++position)
    {
        if (*it == delimiter)
        {
            buffer->push_back(line.substr(last_position, position - last_position));
            last_position = position + 1;
        }
    }

}

float Util::stringToFloat(string input) {
    istringstream is(input);
    float converted; 
    is >> converted;
    return converted; 
}
