#include "Header.h"
#include <iomanip>


void htmlspecialchars(std::string& line)
{
	std::string buffer;
	buffer.reserve(line.size());
	for (int i = 0; i < line.size(); ++i)
	{
		if (line[i] == '<') buffer.append("&#60;");
		else if (line[i] == '>') buffer.append("&#62;");
		else if (line[i] == '&') buffer.append("&#38;");
		else buffer.append(&line[i], 1);
	}
	line.swap(buffer);
}

void log(std::vector<Primate>& monkeys, Text& book, Population& troop, int selector)
{
	switch (selector)
	{
		case 0:
		{	
			std::ofstream log("log.txt", std::ios::trunc);
			log << std::to_string(book.editions);
			log.close();
		}
		break;
		
		case 1:
		{
			htmlspecialchars(monkeys[troop.best].line);

			std::string file_progression = "stats/snapshot" + std::to_string(book.current_line + 1) + ".txt";
			std::ofstream progression(file_progression, std::ios::app);
			progression << "<span id=\"line\">(" << std::setprecision(8) << std::fixed;
			progression << monkeys[troop.best].intelligence << ")  " << monkeys[troop.best].line << "</span>" << std::endl;
			progression.close();

			std::ofstream line("progress_line.txt", std::ios::trunc);
			line << "\t\t" << monkeys[troop.best].line;
			line.close();
		}
		break;

		case 2:
		{
			std::vector<std::string> fingers_images
			{
				"lp", "lr", "lm", "li", "lt"
				, "rt", "ri", "rm", "rr", "rp"
				, "null"
			};

			std::vector<std::string> fingers_names
			{
				"left pinky", "left ring", "left middle", "left index", "left thumb"
				, "right thumb", "right index", "right middle", "right ring", "right pinky"
				, "null"
			};

			std::vector<std::string> lettercase{ "<img src='blank.png'>", "<img src='shift.png' title='shift key pressed'>" };

			std::ofstream progress("progress.txt", std::ios::app);

			if (book.current_line != 0) progress << std::endl;
			progress << troop.generations << "\t<a href = 'stats.php?id=" << std::to_string(book.current_line + 1);
			progress << "' onclick = \"window.open(this.href,'targetWindow', 'toolbar = no, location = no, scrollbars = yes, width = 600, height = 800');";
			progress << "return false; \"><img src='info.png' style='border: 0px'></a>\t";
			progress << monkeys[troop.best].line << "\t\t" << book.current_line + 1;

			progress.close();

			std::ofstream progress_light_tmp("progress_tmp.txt", std::ios::trunc);
			std::string line;
			std::ifstream progress_read("progress.txt");

			int count = 0;
			while (getline(progress_read, line))
			{
				if (count >= book.current_line - 50)
					progress_light_tmp << line << std::endl;
				++count;
			}

			progress_light_tmp.close();
			progress_read.close();

			std::ifstream tmp("progress_tmp.txt");
			std::ofstream progress_light("progress_light.txt", std::ios::trunc);

			progress_light << tmp.rdbuf();

			progress_light.close();
			tmp.close();

			std::string file_basics = "stats/basics" + std::to_string(book.current_line + 1) + ".txt";
			std::ofstream basics(file_basics, std::ios::trunc);
			basics << monkeys[troop.best].line << std::endl << troop.generations;
			basics.close();

			std::string file_DNA = "stats/DNA" + std::to_string(book.current_line + 1) + ".txt";
			std::ofstream DNA(file_DNA, std::ios::app);

			for (int i = 0; i < LENGTH; ++i)
			{
				auto expression = decode(monkeys[troop.best].dna, i);

				DNA << "<span id=\"line\">Gene " << i+1 << "  \t";
				for (int j = i * 12; j < i * 12 + 12; j++) DNA << monkeys[troop.best].dna[j];
				DNA << std::setw(25) << "  <img src='fingers/" << fingers_images[std::get<0>(expression)];
				DNA << ".png' title=\'" << fingers_names[std::get<0>(expression)] << "\'>";
				DNA << std::setw(5);

				// Fix for the display of the conditions of the thumbs when > 1
				if (std::get<1>(expression) <= 1)
					DNA << std::get<1>(expression);
				else
					DNA << std::get<1>(expression) - 1;

				DNA << "  " << std::setw(10);
				DNA << lettercase[std::get<2>(expression)] << std::setw(14) << monkeys[troop.best].line[i] << "</span>" << std::endl;

			}

			DNA.close();	
		}
		break;
	}
}