package moe.oko.opennaw.util;

import moe.oko.opennaw.OpenNAW;
import moe.oko.opennaw.model.City;
import moe.oko.opennaw.model.Nation;
import org.bukkit.Location;
import org.bukkit.Material;

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

public class CityHandler {
    private HashMap<String, City> cities = new HashMap<>();

    public void addCity(String name, String resource, Location location) {
        cities.put(name, new City(name, resource, location));
        location.getBlock().setType(Material.SPONGE);
    }

    public void removeCity(String name) { cities.remove(name);}

    public void setCityOwner(City city, Nation nation) { cities.get(city.getName()).setOwner(nation); }

    public boolean isLocationAPoint(Location loc) {
        for(City cty : cities.values()) {
            if(cty.getPoint().equals(loc)) {
                return true;
            }
        }
        return false;
    }

    public City fetchCityViaPoint(Location location) {
        for(City cty : cities.values()) {
            if(cty.getPoint().equals(location)) {
                return cty;
            }
        }
        return null;
    }

    public City fetchCityViaName(String name) {
        for(City cty : cities.values()) {
            if(cty.getName().equalsIgnoreCase(name)) {
                return cty;
            }
        }
        return null;
    }

    public void loadCityList(List<City> cities) {
        for(City city : cities) {
            this.cities.put(city.getName(), city);
        }
    }

    public List<String> getCityList() {
        List<String> cityList = new ArrayList<String>();
        for (City city : cities.values()) {
            cityList.add(city.getName());
        }
        return cityList;
    }

    public void saveCities() {
        OpenNAW.getInstance().getDatabase().saveCities(cities.values());
    }
}
