package Bubulle;

import java.io.IOException;
import java.util.ArrayList;

public class Tracker {

	//régularité
	//plus les angles identiques = score bas
	//plus distance identiques = score bas
	//ATTENTION !!! b3 et b4 --> 2 delta t
	
	private static ArrayList<Trajectoire> solution = new ArrayList<Trajectoire>();
	private static ArrayList<Bulle> population;
	private static final int maxBulle = 5;
	private static final double ANGLE = 20; //20
	private static final double DISTANCE = 0.6;//0.4
        
        private static Trajectoire tracer;
	

	public static void main(String[] args) throws IOException {
		
		final String norma = /*"bubu";*/"norma_N5_tau4_dt2_delai820_00000";
		final String ext = ".txt";
		int max = 1; 
		
		String name = norma+max+ext;
		
		//Lecture et stockage en mémoire de toute les bulles
		Bubulles.creerBulle(name);
		
		//demarrage(Recherche.file);
		demarrage2(Bubulles.population);
	}
	
	private static void score(Trajectoire t) {
		
		ArrayList<Bulle> list = t.getList();
		double score = 0.0;
		
		//Angle
		for(int i = 0; i < 2; i++) {
			score+= Math.abs( Angle.degre(list.get(i), list.get(i+1), list.get(i+2)) - Angle.degre(list.get(i+1), list.get(i+2), list.get(i+3)) );
		}
		
		//Distance
		for(int i = 0; i < 3; i++) {
			score+= Math.abs( list.get(i).distanceXYZ(list.get(i+1)) - list.get(i+1).distanceXYZ(list.get(i+2)) );
		}
		
		t.setScore(score);
	}
	
	
	private static void demarrage(ArrayList<Bulle> popInit) {
		
		population = popInit;
		tracer = new Trajectoire();
		backtracking2(popInit.get(0).getNo());
		
			
	}
	
	/*
	 * act : id de la bulle (1 a 10)
	 * population : liste des bulles (0 a 9) taille 10
	 * tracer : liste de bulle
	 */
	//Arret trajectoire pleine 5
	

		private static boolean backtracking2(int act) {
                        /*------------------*/
                        //DEBUG
                       // tracer.getList().stream().forEach((b) -> {
                        //    System.out.print(b.getNo() + " ");
                        //});
			//System.out.println();
			/*------------------*/
			/*------------------*/
			
			
			if(act < population.size()) {
				if(estok(act)) {//TODO
					tracer.setList(population.get(act-1));
					
					
					if(tracer.getList().size() == 5) {
						solution.add(tracer);
						
                                            /*------------------*/
                                            //DEBUG
                                            tracer.getList().stream().forEach((b) -> {
                                                System.out.print(b.getNo() + " ");
                                            });
                                            
                                            System.out.println("--add--");
                                            /*------------------*/
                                            /*------------------*/


                                            Bulle b = tracer.removeLast();
                                            
                                            //test doublon
                                            int actTest = b.getNo()+1;
                                            while(!pasdoublon(actTest) || !estok(actTest)){
                                                actTest++;
                                            }
                                            
                                            return backtracking2(actTest);
					}
					else {
                                            //test doublon
                                            int actTest = 1;
                                            while(!pasdoublon(actTest) || !estok(actTest)){
                                                actTest++;
                                            }
                                            
                                            return backtracking2(actTest);
					}
				}else {
                                    //test doublon
                                    int actTest = act+1;
                                    while(!pasdoublon(actTest) || !estok(actTest)){
                                        actTest++;
                                    }
                                    return backtracking2(actTest);
				}
			}else {
                                boolean stop = false;
                                
				//on recupere le no +1 du dernier element
                                if(tracer.getList().isEmpty()){
                                    stop = true;
                                }
				while(!stop && tracer.getLast().getNo() + 1 > population.size()) {
					//Bulle b1 = tracer.removeLast(); //b1 pas utiliser
                                        
                                        System.out.println("-- PING --"); //DEBUG
                                        
                                        tracer.removeLast();
					if(tracer.getList().isEmpty()){
                                            stop = true;
                                        }
				}
                                
                                
                                if(stop){
                                    System.out.println("-- END --"); //DEBUG
                                    return false;
                                }
                                else{
                                    Bulle b3 = tracer.removeLast();
                                    
                                    //test doublon
                                    int actTest = b3.getNo()+1;
                                    while(!pasdoublon(actTest) || !estok(actTest)){
                                        actTest++;
                                    }
                                    return backtracking2(actTest);
                                }
			}
		}
                
		/******************************************************************/
		/******************************************************************/
		/******************************************************************/
		
		private static ArrayList<Bulle> popTravail;
		
		private static void demarrage2(ArrayList<Bulle> popInit) {
			population = popInit;
			tracer = new Trajectoire();
			
			
			//Parcours pour chaque bulle comme point de départ
			for(int i = 0; i < population.size()/*1*/; i++) {
				/*ArrayList<Bulle> popTravail = new ArrayList<Bulle>();
				Bulle maBulle = population.get(i);
				
				
				//Parcours pour récupérer les bulles à portée
				for(Bulle b : population) {
					//a portée
					if(maBulle.distanceXYZ(b) <= DISTANCE * 5 && maBulle.getNo() != b.getNo()) {
						popTravail.add(b);
					}
				}
				
				//Ajout de la première au tracer
				tracer.setList(maBulle);
				backtracking3(i);*/
				System.out.println(population.get(i).getNo());
			}
		}
		
		private static boolean backtracking3(int act) {
			
			for(Bulle debug : popTravail)
				System.out.print( debug.getNo() + " ");
			
			return true;
		}
		/******************************************************************/
		/******************************************************************/
		/******************************************************************/
		
		public static boolean pasdoublon(int act) {//TODO 2 delta t
                    if(tracer.getList().isEmpty()) {
				return true;
                    }
                    else{
                        if(act >= population.size()){
                            return true;
                        }
                        if(tracer.contains(population.get(act-1))) {
                            //System.out.println("contain");
                            return false;
                        }
                    }
                    return true;
                }
		
		public static boolean estok(int act) {//TODO 2 delta t
			/*
			 * distance si |distance| > DISTANCE -> false
			 * angle si |angle| > ANGLE -> false
			 * doublon si l'elment est deja dans la liste -> false
			 */
                        if(act >= population.size()){
                            return true;
                        }
                        
			if(tracer.getList().isEmpty()) {
				return true;
			}
			else if(tracer.getList().size() == 1) {
				
				//System.out.println("---> " + Math.abs(tracer.getLast().distanceXYZ(population.get(act-1))));
				//distance
                                
                                double dist = Math.abs(tracer.getLast().distanceXYZ(population.get(act-1)));
                                
				if(dist > DISTANCE) {
					//System.out.println("dist 1  : " + dist);
					return false;
				}
			}
			else {
				
				//angle
				/*if(Math.abs(Angle.degre(tracer.getElementPosition(2), tracer.getElementPosition(1), population.get(act-1))) > ANGLE) {
					//System.out.println("angle");
					return false;
				}*/
				
				//distance
				if(Math.abs(tracer.getLast().distanceXYZ(population.get(act-1))) > DISTANCE) {
					//System.out.println("dist 2   : " + Math.abs(tracer.getLast().distanceXYZ(population.get(act-1))));
					return false;
				}
				
			}
			return true;
			
		}
	
	
} 
