
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class Champion2 extends Api
{
	private Map map = new Map();
	private Goal goal = new Goal();
	private int collectDuckId; 
	
	
	
    /**
     * Fonction appelée au début de la partie.
     */
    public void partie_init()
    {
    	
    	collectDuckId = troupes_joueur(Api.moi())[0].id;
    	map.init();
    	
    	
    	
    	
    }

    /**
     * Fonction appelée à chaque tour.
     */
    public void jouer_tour()
    {
    	Position pos = troupes_joueur(Api.moi())[0].maman;
    	System.out.println("---- >  " + pos.colonne + "/" + pos.ligne);
    	
    	if (!goal.has) { //objectif en vue ?
    		if (pos.colonne == 0)
    			avancer(collectDuckId, Direction.EST);
    		else if (pos.ligne == 0)
    			avancer(collectDuckId, Direction.NORD);
    		else if (pos.colonne == Api.LARGEUR-1)
    			avancer(collectDuckId, Direction.OUEST);
    		else if (pos.ligne == Api.HAUTEUR-1)
    			avancer(collectDuckId, Direction.SUD);
    		
    		else {
    			System.err.println(2);
    			goal.search(map); //recherche d'un objectif
    		}
    		

    	}
    	if (goal.has)
	    	for (int i = 0; i < 5; i++) {
	    		avancer(collectDuckId, goal.path[goal.progress]);
	    		goal.progress++;
	    		if (goal.progress >= goal.path.length) {
	    			goal.has = false;
	    			break;
	    		}
	    	}
    	
    	//accomplissement objectif:
    	
    /*	Position pos = new Position();
    	for (int x = 0; x < Api.LARGEUR; x++) {
			pos.colonne = x;
			System.out.println();
			for (int y = 0; y < Api.HAUTEUR; y++) {
				pos.ligne=y;
				EtatCase et = Api.info_case(pos);
				System.out.print("  "+et.contenu+" |");
			}
		}*/
    	
    }

    /**
     * Fonction appelée à la fin de la partie.
     */
    public void partie_fin()
    {
        // TODO
    }
}

class Map {

	public List<Position> papis = new ArrayList<>();
	
	public void init() {
		
		Position pos = new Position();
		for (int x = 0; x < Api.LARGEUR; x++) {
			pos.colonne = x;
			for (int y = 0; y < Api.HAUTEUR; y++) {
				pos.ligne=y;
				EtatCase et = Api.info_case(pos);
				if (et.contenu == TypeCase.PAPY) {
					papis.add(et.pos);
				}
			}
		}
		
		
		
		for (Position p : papis) {
			Api.debug_poser_pigeon(p, PigeonDebug.PIGEON_BLEU);
		}
		
		
	}
	
	
	
}

class Goal{
	
	public boolean has;
	public Direction[] path;
	public int progress = 0;
	
	public void search(Map map){
		
		
		Position currentPos = Api.troupes_joueur(Api.moi())[0].maman;

		java.util.Map<Position, BreadScore> breadScore = new HashMap<>();
		Position[] breads = Api.pains();
	
		for (Position pos : map.papis) {
			
			System.out.println(currentPos.colonne + "/" + currentPos.ligne + "/" + currentPos.niveau);
			System.out.println(pos.colonne + "/" + pos.ligne + "/" + pos.niveau);
			
			int dist = Api.trouver_chemin(currentPos, pos).length;
			if (dist/5 != dist/5f)
				dist=dist/5 + 1;
			else
				dist/=5;
			
			
			System.out.println("dist " + dist);
		
			breadScore.put(pos, new BreadScore(Api.papy_tours_restants(pos), dist));
			
			
		}
		
		for (Position pos : breads) {
			BreadScore sc = breadScore.get(pos);
			if (sc != null) {
				sc.nb=Api.info_case(pos).nb_pains;
			} else {
				System.out.println("cccc ----> "+ Api.moi());
				System.out.println(Api.troupes_joueur(Api.moi()));
				int dist = Api.trouver_chemin(Api.troupes_joueur(Api.moi())[0].maman, pos).length;
				if (dist/5 != dist/5f)
					dist=dist/5 + 1;
				else
					dist/=5;
				
				if (dist > 0) {
					breadScore.put(pos, new BreadScore(dist, dist, Api.info_case(pos).nb_pains));
				}
			}
		}
		int min = -1;
		Position pMin = null;
		for (Position p : breadScore.keySet()) {
			BreadScore sc = breadScore.get(p);
			
			int a1 = sc.time-sc.distance;//argument a1 <== temps a attendre a l"arrivee sur la case avant pain
			int a2 = sc.distance;//argument a2 <== distance pour atteindre 
			
			if (a1 > 0) {
				a2+=a1;
			}
			
			if (pMin == null || min > a2) {
				min = a2;
				pMin = p;
			}
			
		}
		System.out.println(currentPos);
		System.out.println(pMin);
		
		path = Api.trouver_chemin(currentPos, pMin);
		
		
		has = true;
		
	}

	class BreadScore {
		int time; //temps avant apparition
		int distance;
		int nb = 0;
		
		public BreadScore(int time, int dist) {
			this(time, dist, 0);
		}
		
		public BreadScore(int time, int dist, int nb) {
			this.time = time;
			this.distance = dist;
			this.nb = nb;
		}
	}
}


