package tk.fusselrulezz.ets2editor.profile.source;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import tk.fusselrulezz.ets2editor.format.sii.SIIConfig;
import tk.fusselrulezz.ets2editor.format.sii.SIIEntry;
import tk.fusselrulezz.ets2editor.format.sii.SIISection;
import tk.fusselrulezz.ets2editor.profile.ingame.IngameCompany;
import tk.fusselrulezz.ets2editor.profile.ingame.Player;

public class GameConfiguration extends SIIConfig {
	
	private SIISection economy;
	private SIIEntry visitedCities, visitedCitiesCount;
	
	private Player player;
	
	private List<IngameCompany> companies = new ArrayList<IngameCompany>();
	
	public GameConfiguration(File file) {
		super(file);
		
		if(isValid()) {
			economy = getSectionByType("economy");
			visitedCities = economy.getEntry("visited_cities");
			visitedCitiesCount = economy.getEntry("visited_cities_count");
			
			player = new Player(economy, getSectionFromEconomySection("player"), getSectionFromEconomySection("bank"));
			
			loadCompanies();
		}
	}
	
	protected void loadCompanies() {
		String[] values = economy.getEntry("companies").getArrayValues();
		
		for(int i = 0; i < values.length; i++) {
			companies.add(new IngameCompany(this, getSectionByIdentifier(values[i])));
		}
	}
	
	protected SIISection getSectionFromEconomySection(String entry) {
		return getSectionByIdentifier(economy.getEntry(entry).getValue());
	}
	
	/* Companies */
	
	public IngameCompany getIngameCompanyByIdentifier(String identifier) {
		for(IngameCompany company : companies) {
			if(company.getDefinition().equals(identifier)) {
				return company;
			}
		}
		
		return null;
	}
	
	public List<IngameCompany> getIngameCompaniesByCity(String city) {
		List<IngameCompany> result = new ArrayList<IngameCompany>();
		
		for(IngameCompany company : companies) {
			if(company.getDefinition().endsWith("." + city)) {
				result.add(company);
			}
		}
		
		return result;
	}
	
	public IngameCompany getIngameCompany(String city, String company) {
		for(IngameCompany current : companies) {
			if(current.getDefinition().equals("company.volatile." + company + "." + city)) {
				return current;
			}
		}
		
		return null;
	}
	
	public List<IngameCompany> getIngameCompanies() {
		return companies;
	}
	
	/* Visited Cities */
	
	public String[] getVisitedCities() {
		return visitedCities.getArrayValues();
	}
	
	public int[] getVisitedCitiesCount() {
		return visitedCitiesCount.getArrayValuesAsInteger();
	}
	
	public int getVisitCount(String city) {
		String[] cities = getVisitedCities();
		int[] counts = getVisitedCitiesCount();
		
		for(int i = 0; i < cities.length; i++) {
			if(cities[i].equalsIgnoreCase(city)) {
				return counts[i];
			}
		}
		
		return 0;
	}
	
	public boolean hasVisitedCity(String city) {
		for(String current : getVisitedCities()) {
			if(current.equalsIgnoreCase(city)) {
				return true;
			}
		}
		
		return false;
	}
	
	public void addVisitedCity(String city, int count) {
		if(!hasVisitedCity(city)) {
			visitedCities.addArrayValue(city, false);
			visitedCitiesCount.addArrayValue(String.valueOf(count), false);
		}
	}
	
	public String getLastVisitedCity() {
		return economy.getEntry("last_visited_city").getValue();
	}
	
	public void setLastVisitedCity(String value) {
		economy.getEntry("last_visited_city").setValue(value);
	}
	
	/* Other */
	
	public SIISection getEconomySection() {
		return economy;
	}
	
	public Player getPlayer() {
		return player;
	}
}
