#include "proc_mapa.h"
#include "jgame.h"
#include "aux_textfile.h"
#include "aux_font.h"
namespace mapa
{    
	namespace botons
	{
		const int caminar     	= 0;
		const int parar			= 1;
		const int cavar			= 2;
		const int escalar		= 3;
		const int perforar		= 4;
		const int escalera		= 5;
		const int pasarela		= 6;
		const int corda			= 7;
		const int suicidi		= 8;
		const int fastforward	= 9;
	}
	namespace arounders
	{
		int orientacio_inicial {0};
		int totals {0};
		int necessaris {0};

		int arrivats {0};
		int eixits {0};
		int morts {0};
	}

	namespace accions
	{
		int parar {0};
		int cavar {0};
		int escalar {0};
		int perforar {0};
		int escalera {0};
		int pasarela {0};
		int corda {0};
	}

	int ini_x, ini_y;
	int fin_x, fin_y;
	uint32_t velocitat;

	draw::surface *mapa = nullptr;
	draw::surface *porta = nullptr;
	draw::surface *boto = nullptr;

	int contador;

    void carregar()
	{
		mapa::velocitat = 70;
		mapa::contador = game::getTicks();

		textfile::open("mapes.txt");

		do {
			textfile::searchToken("LEVEL");
		} while (textfile::toInt(textfile::getNextToken()) != game::getConfig("fase"));

		const int tileset = textfile::getIntValue("tileset");
		arounders::orientacio_inicial = textfile::getIntValue("orientacio");
		arounders::totals = textfile::getIntValue("arounders");
		arounders::necessaris = textfile::getIntValue("necessaris");
		
		accions::parar = textfile::getIntValue("parar");
		accions::cavar = textfile::getIntValue("cavar");
		accions::escalar = textfile::getIntValue("escalar");
		accions::perforar = textfile::getIntValue("perforar");
		accions::escalera = textfile::getIntValue("escalera");
		accions::pasarela = textfile::getIntValue("pasarela");
		accions::corda = textfile::getIntValue("corda");

		ini_x = textfile::getIntValue("inici");
		ini_y = textfile::toInt(textfile::getNextToken());

		fin_x = textfile::getIntValue("final");
		fin_y = textfile::toInt(textfile::getNextToken());

		arounders::arrivats = arounders::eixits = arounders::morts = 0;

		//int tilemap[200];
		//for (int y=0; y<10; ++y) for (int x=0; x<10; ++x) fscanf(f, "%i", &tilemap[x+y*20]);

		draw::surface *tiles = draw::loadSurface("tiles.gif");
		draw::setSource(tiles);

		if (mapa) draw::freeSurface(mapa);
		mapa = draw::createSurface(320, 200);
		draw::setDestination(mapa);
		draw::cls(0);

		for (int y=0; y<10; ++y)
			for (int x=0; x<20; ++x)
			{
				int tile = textfile::toInt(textfile::getNextToken());
				if (tile > 0) draw::draw(x*16, y*16, 16, 16, (tile-1)*16, tileset*16);
			}

		draw::freeSurface(tiles);

		textfile::close();

		draw::surface *marcador = draw::loadSurface("marcador.gif");
		draw::setSource(marcador);
		draw::draw(0, 165, marcador->w, marcador->h, 0, 0);
		draw::freeSurface(marcador);

		font::selectFont(font::type::normal);
		font::print(188,188, game::getConfig("fase")+1);
		font::print(7,188, "XX");
		font::print(135,188, "XX");
		font::print(151,188, "XX");
		font::print(224,171, "ACTIUS");
		font::print(224,177, "TOTAL");
		font::print(224,183, "NECESSARIS");
		font::print(224,189, "ARRIVATS");

		draw::setDestination(nullptr);

		if (mapa::porta == nullptr) mapa::porta = draw::loadSurface("puerta.gif");
		if (mapa::boto == nullptr) mapa::boto = draw::loadSurface("boto.gif");
	}

    void pintar(int accio, int prevista)
	{
		draw::draw(mapa);
		draw::setSource(porta);
		draw::draw(ini_x*16, ini_y*16);
		draw::draw(fin_x*16, fin_y*16);

        font::selectFont(font::type::normal);
		font::print(23,188, mapa::accions::parar);
		font::print(39,188, mapa::accions::cavar);
		font::print(55,188, mapa::accions::escalar);
		font::print(71,188, mapa::accions::perforar);
		font::print(87,188, mapa::accions::escalera);
		font::print(103,188, mapa::accions::pasarela);
		font::print(119,188, mapa::accions::corda);

		font::print(301,171, mapa::arounders::eixits-mapa::arounders::arrivats-mapa::arounders::morts);
		font::print(301,177, mapa::arounders::totals);
		font::print(301,183, mapa::arounders::necessaris);
		font::print(301,189, mapa::arounders::arrivats);

		if (accio >= 10) accio = 0;
		if (prevista >= 10) prevista = 0;
		draw::setSource(boto);
		draw::draw((accio*16)+5, 171);

		if (prevista != accio && prevista != 0) {
			if ((game::getTicks()-contador) <= 200) {
				draw::draw((prevista*16)+5, 171);
			} else {
				if ((game::getTicks()-contador) >= 400) contador = game::getTicks();
			}
		}

		if (velocitat < 70) {
			draw::draw((9*16)+5, 171); // Pintar el botó de accelerat com pulsat
		}
	}

	const int procesar()
	{
		int boto = -1;

		if (input::mouseY() >= 171 && input::mouseY() <= 187 && input::mouseX() >= 5 && input::mouseX() <= 165) {
			boto = (input::mouseX()-5) / 16;

			switch(boto) {
				case mapa::botons::parar:
					if (mapa::accions::parar == 0) boto = -1;
					break;
				case mapa::botons::cavar:
					if (mapa::accions::cavar == 0) boto = -1;
					break;
				case mapa::botons::escalar:
					if (mapa::accions::escalar == 0) boto = -1;
					break;
				case mapa::botons::perforar:
					if (mapa::accions::perforar == 0) boto = -1;
					break;
				case mapa::botons::escalera:
					if (mapa::accions::escalera == 0) boto = -1;
					break;
				case mapa::botons::pasarela:
					if (mapa::accions::pasarela == 0) boto = -1;
					break;
				case mapa::botons::corda:
					if (mapa::accions::corda == 0) boto = -1;
					break;
				case botons::fastforward:
					if (mapa::velocitat == 70) {
						mapa::velocitat = 10;
					} else {
						mapa::velocitat = 70;
					}
					boto = -1;
					break;
			}
		}
		return boto;
	}

}