public class Prologin extends Interface {
	// Fonction appelée au début de la partie.
	public void partie_init() {
		// Interface.moi();
		System.out.println("Player " + API.moi() + ": Begin");
		API.init();
	}

	public void printMap() {
		Position p = new Position();
		System.out.println("Printing");
		for (int y = 0; y < 39; y++) {
			for (int x = 0; x < 39; x++) {
				p.x = x;
				p.y = y;
				System.err.print(" " + (API.est_libre(p) ? 1 : 0));
			}
			System.err.println();
		}
	}

	public Position position(int x, int y) {
		Position p = new Position();
		p.x = x;
		p.y = y;
		return p;
	}

	public void aspirationThing() {
		int max = -1;
		Position maxP = null;
		for (Position p : API.ma_base()) {
			if (p.y == 18 || p.y == 19 || p.y == 20)
				continue;
			if (API.puissance_aspiration(p) >= max) {
				max = API.puissance_aspiration(p);
				maxP = p;
			}
		}
		Position dest = position(0, 19);
		if (API.puissance_aspiration(dest) == API.LIMITE_ASPIRATION)
			dest.y += 1;
		if (API.puissance_aspiration(dest) == API.LIMITE_ASPIRATION) {
			dest.y -= 1;
			dest.x = 38;
		}
		if (API.puissance_aspiration(dest) == API.LIMITE_ASPIRATION) {
			dest.y += 1;
		}
		if (API.puissance_aspiration(dest) != API.LIMITE_ASPIRATION) {
			API.deplacer_aspiration(maxP, dest);
		}
	}

	public int mainPipeBuild() {
		int action = 5;
		Position p = new Position();
		loop1: for (int x = 1; x < 40; x++) {
			for (int y = 19; y < 21; y++) {
				p.x = x;
				p.y = y;
				if (API.est_debris(p)) {
					API.deblayer(p);
					API.construire(p);
					action -= 3;
					break loop1;
				}
			}
		}
		loop: for (int x = 1; x < 40; x++) {
			for (int y = 19; y < 21; y++) {
				p.x = x;
				p.y = y;
				if (API.est_libre(p)) {
					API.construire(p);
					action -= 1;
				}
				if (action == 0)
					break loop;
			}
		}
		return action;
	}

	public int distanceToBase(Position p) {
		Position symmetrid = new Position();
		if (p.x > 20)
			symmetrid.x = 38 - p.x;
		else
			symmetrid.x = p.x;
		if (p.y > 20)
			symmetrid.y = 38 - p.y;
		else
			symmetrid.y = p.y;
		int distance = 0;
		distance += symmetrid.x;
		if (symmetrid.y < 13)
			distance += 13 - distance;
		return distance;
	}

	public boolean isEntoure(Position pulsar) {
		boolean toReturn = true;
		Position toChange = new Position();
		toChange.x = pulsar.x - 1;
		toChange.y = pulsar.y - 1;
		toReturn &= !Interface.est_libre(toChange);

		toChange.x = pulsar.x;
		toChange.y = pulsar.y - 1;
		toReturn &= !Interface.est_libre(toChange);

		toChange.x = pulsar.x + 1;
		toChange.y = pulsar.y - 1;
		toReturn &= !Interface.est_libre(toChange);

		toChange.x = pulsar.x - 1;
		toChange.y = pulsar.y;
		toReturn &= !Interface.est_libre(toChange);

		toChange.x = pulsar.x - 1;
		toChange.y = pulsar.y + 1;
		toReturn &= !Interface.est_libre(toChange);

		toChange.x = pulsar.x;
		toChange.y = pulsar.y + 1;
		toReturn &= !Interface.est_libre(toChange);

		toChange.x = pulsar.x + 1;
		toChange.y = pulsar.y + 1;
		toReturn &= !Interface.est_libre(toChange);

		toChange.x = pulsar.x + 1;
		toChange.y = pulsar.y;
		toReturn &= !Interface.est_libre(toChange);

		return toReturn;
	}

	public Position getBestPulsar() {
		int min = -1;
		Position nearestPulsar = null;
		for (Position p : Interface.liste_pulsars()) {
			if (isEntoure(p))
				continue;
			int distance = distanceToBase(p);
			double score = Interface.info_pulsar(p).puissance * Interface.info_pulsar(p).pulsations_restantes;
			score /= distance;
			if (min == -1 || distance < min) {
				min = distance;
				nearestPulsar = p;
			}
		}
		return nearestPulsar;
	}

	public void buildPipeTo(Position pulsar) {
		int[][] map = new int[39][39];

		for (int y = 0; y < map.length; y++) {
			for (int x = 0; x < map.length; x++) {
				if (Interface.est_libre(position(x, y)) || Interface.est_tuyau(position(x, y)) || Interface.est_debris(position(x, y)))
					map[y][x] = -1;
				else
					map[y][x] =  -2;
			}
		}
		
		for(Position p : API.ma_base())
		{
			map[p.y][p.x] = -3;
		}
		
		map[pulsar.y][pulsar.x] = 0;

		int[][] ways = {{-1, 0}, {0, -1}, {1, 0}, {0, 1}};
		for(int k = 0 ; k < 100 ; k++)
		{
			for (int y = 0; y < map.length; y++) {
				for (int x = 0; x < map.length; x++) {
					if(map[y][x] == k)
					{
						for(int[] direction : ways)
						{
							int ycor = y+direction[0];
							int xcor = x+direction[1];
							if(ycor >= 0 && ycor < 39 &&
							   x+direction[x] >= 0 && xcor < 39)
							{
								if(map[ycor][xcor] == -1)
								{
									map[ycor][xcor] = k+1;
								}
							}	
						}
					}
				}
			}
			
		}
	}

	// Fonction appelée à chaque tour.
	public void jouer_tour() {
		if (API.tour_actuel() == 100) {
			printMap();
		}
		aspirationThing();
		int actionLeft = mainPipeBuild();
		Position pulsar2go2 = getBestPulsar();

	}

	// Fonction appelée à la fin de la partie.
	public void partie_fin() {
		// Place ton code ici
	}
}
