package org.frostbite.kktrack;

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

/**
 * Created by owen on 06/02/14.
 */
public class RaceController {
    public static int count = 0;
    public static Scanner input = new Scanner(System.in);
    public static void raceRunnerAlt(RaceParams params, Racer[] racers, int[] extraInfo, int race, KubKarInterface app){
        int[] trackLineup = new int[params.getTrack()];
        String returned = "";
        boolean isGood;
        boolean laneSet;
        boolean reverse = false;
        Arrays.fill(trackLineup, 9999);
        String racersTemp = "";
        for(int i=0; i<trackLineup.length; i++){
            laneSet = false;
            if(!reverse){
                for(int currentCheck=1; !laneSet; currentCheck++){
                    if(checkForTrack(racers[extraInfo[currentCheck]], trackLineup, i, params)){
                        trackLineup[i] = extraInfo[currentCheck];
                        racers[extraInfo[currentCheck]].setTrackRaced(i);
                        laneSet = true;
                    } else if(currentCheck==(extraInfo.length-1)){
                        trackLineup[i] = 9999;
                        laneSet = true;
                    }
                }
                reverse = true;
            } else {
                for(int currentCheck=extraInfo.length-1; !laneSet; currentCheck--){
                    if(checkForTrack(racers[extraInfo[currentCheck]], trackLineup, i, params)){
                        trackLineup[i] = extraInfo[currentCheck];
                        racers[extraInfo[currentCheck]].setTrackRaced(i);
                        laneSet = true;
                    } else if(currentCheck<0){
                        trackLineup[i] = 9999;
                        laneSet = true;
                    }
                }
                reverse = false;
            }
            if(trackLineup[i]!=9999){
                racersTemp += "Lane " + (i+1) + ": " + racers[trackLineup[i]].getRacerName() + "(ID: " + (racers[trackLineup[i]].getID()+1) + ")" + "\n";
            } else {
                racersTemp += "Lane " + (i+1) + ": NOT IN USE (ID: Null)\n";
            }
        }
        app.println("\n\nThe lineup for the catchup race " + (race+1) + " is :\n" + racersTemp);
        app.println("You will now enter the places below");
        for(int i=0; i<params.getTrack(); i++){
            isGood = false;
            do{
                if(trackLineup[i]!=9999){
                    app.print("\nPlease enter the place of the car in lane " + (i+1) + ": ");
                    while(!input.hasNextInt()){
                        app.print("Places must be entered as a number: ");
                        input.next();
                    }
                    returned = racers[trackLineup[i]].setNextPlace(input.nextInt(), params, i);
                    if(returned.length()!=0){
                        app.println(returned);
                    } else {
                        isGood = true;
                    }
                } else {
                    isGood = true;
                }
            }while(!isGood);
        }
    }
    private static boolean checkForTrack(Racer racer, int[] trackLineup, int track, RaceParams params){
        boolean result = true;
        if(!racer.hasRacedOn(track)&&racer.getRuns()<params.getTrack()){
            for(int i=0; i<trackLineup.length; i++){
                if(racer.getID()==trackLineup[i]){
                    result = false;
                }
            }
        } else {
            result = false;
        }
        return result;
    }
    public static int[] checkRacers(RaceParams params, Racer[] racers){
        ArrayList<Integer> incompRacers = new ArrayList<Integer>();
        int numOfExtra = 0;
        int temp = 0;
        for(Racer check : racers){
            if((check.getRuns())<(params.getTrack())){
                incompRacers.add(check.getID());
                temp = (params.getTrack())-(check.getRuns());
                if(numOfExtra<temp)
                    numOfExtra=temp;
            }
        }
        int[] result = new int[incompRacers.size()+1];
        result[0] = numOfExtra;
        for(int i=1; i<result.length; i++){
            result[i] = incompRacers.get(i-1);
            //System.out.println("DEBUG: " + racers[incompRacers.get(i-1)].getRacerName());
        }
        return result;
    }
    public static void raceRunner(RaceParams params, Racer[] racers, int heat, int race, KubKarInterface app){
        boolean isGood;
        String returned = "";
        int[] trackLineup = new int[params.getTrack()];
        String racersTemp = "";
        for(int i=0; i<trackLineup.length; i++){
            trackLineup[i] = count+heat;
            if(trackLineup[i]>params.getRacers()-1){
                trackLineup[i] = trackLineup[i] - (params.getRacers());
            }
            racersTemp += "Lane " + (i+1) + ": " + racers[trackLineup[i]].getRacerName() + "(ID: " + (racers[trackLineup[i]].getID()+1) + ")" + "\n";
            count++;
        }
        app.println("\n\nThe lineup for race " + (race+1) + " of heat " + (heat+1) + " is :\n" + racersTemp);
        app.println("You will now enter the places below");
        for(int i=0; i<params.getTrack(); i++){
            isGood = false;
            do{
                app.print("\nPlease enter the place of the car in lane " + (i+1) + ": ");
                while(!input.hasNextInt()){
                    app.print("Places must be entered as a number: ");
                    input.next();
                }
                returned = racers[trackLineup[i]].setNextPlace(input.nextInt(), params, i);
                if(returned.equalsIgnoreCase("That is not a valid place!")){
                    app.println(returned);
                } else {
                    app.println(returned);
                    isGood = true;
                }
            }while(!isGood);
        }
    }
}
