package com.spaceshipproject;

import org.bukkit.Material;
import org.bukkit.configuration.file.FileConfiguration;

/**
 * Acces type-safe a la config (config.yml).
 */
public class ConfigManager {

    private final SpaceShipProject plugin;

    public ConfigManager(SpaceShipProject plugin) {
        this.plugin = plugin;
    }

    public void reload() {
        plugin.reloadConfig();
    }

    private FileConfiguration cfg() {
        return plugin.getConfig();
    }

    public int maxShipsPerPlayer() { return cfg().getInt("max-ships-per-player", 5); }

    public int defaultSizeX() { return cfg().getInt("default-size.x", 4); }
    public int defaultSizeY() { return cfg().getInt("default-size.y", 4); }
    public int defaultSizeZ() { return cfg().getInt("default-size.z", 6); }

    public int maxSizeX() { return cfg().getInt("max-size.x", 16); }
    public int maxSizeY() { return cfg().getInt("max-size.y", 8); }
    public int maxSizeZ() { return cfg().getInt("max-size.z", 16); }

    public double costSummon()           { return cfg().getDouble("costs.summon", 100); }
    public double costUnload()           { return cfg().getDouble("costs.unload", 0); }
    public double costUpgradeBase()      { return cfg().getDouble("costs.upgrade-base", 1000); }
    public double costSpaceJumpPerBlock(){ return cfg().getDouble("costs.space-jump-per-block", 1); }

    public int spaceJumpBlocksPerLevel() { return cfg().getInt("space-jump.blocks-per-level", 100); }
    public int spaceJumpMin()            { return cfg().getInt("space-jump.min-blocks", 1); }
    public int spaceJumpHardCap()        { return cfg().getInt("space-jump.max-blocks-hard-cap", 5000); }

    public int summonOffsetY() { return cfg().getInt("summon-offset-y", 30); }

    public Material defaultPlatformMaterial() { return parseMat("default-platform-material", Material.SMOOTH_QUARTZ); }
    public Material defaultEdgeMaterial()     { return parseMat("default-edge-material",     Material.QUARTZ_PILLAR); }
    public Material defaultGlassMaterial()    { return parseMat("default-glass-material",    Material.WHITE_STAINED_GLASS); }

    public String defaultShipName() { return cfg().getString("default-ship-name", "Spaceship #%n%"); }

    public boolean checkCollision() { return cfg().getBoolean("safety.check-collision", true); }
    public int minYAbovePlayer() { return cfg().getInt("safety.min-y-above-player", 10); }

    private Material parseMat(String key, Material def) {
        String s = cfg().getString(key);
        if (s == null) return def;
        Material m = Material.matchMaterial(s);
        return m != null ? m : def;
    }

    /* ============ MESSAGES ============ */

    public String prefix()              { return color(cfg().getString("messages.prefix", "&8[&bSpaceShip&8] &7")); }
    public String msgNoPerm()           { return prefix() + color(cfg().getString("messages.no-permission",       "&cNo perm")); }
    public String msgNotOwner()         { return prefix() + color(cfg().getString("messages.not-owner",           "&cNot owner")); }
    public String msgGiven(String n)    { return prefix() + color(cfg().getString("messages.ship-given",          "&aGiven")).replace("%name%", n); }
    public String msgSummoned(String n) { return prefix() + color(cfg().getString("messages.ship-summoned",       "&aSummoned")).replace("%name%", n); }
    public String msgUnloaded(String n) { return prefix() + color(cfg().getString("messages.ship-unloaded",       "&aUnloaded")).replace("%name%", n); }
    public String msgTpUp(String n)     { return prefix() + color(cfg().getString("messages.ship-teleport-up",    "&aTP up")).replace("%name%", n); }
    public String msgTpDown()           { return prefix() + color(cfg().getString("messages.ship-teleport-down",  "&aTP down")); }
    public String msgAlreadyLoaded()    { return prefix() + color(cfg().getString("messages.ship-already-loaded", "&eAlready loaded")); }
    public String msgNotLoaded()        { return prefix() + color(cfg().getString("messages.ship-not-loaded",     "&eNot loaded")); }
    public String msgCollision()        { return prefix() + color(cfg().getString("messages.ship-collision",      "&cCollision")); }
    public String msgMaxShips(int max)  { return prefix() + color(cfg().getString("messages.max-ships-reached",   "&cMax")).replace("%max%", String.valueOf(max)); }
    public String msgDeleted()          { return prefix() + color(cfg().getString("messages.ship-deleted",        "&cDeleted")); }
    public String msgNotEnoughMoney(String need, String have) {
        return prefix() + color(cfg().getString("messages.not-enough-money", "&cFonds insuffisants : %need%/%have%"))
                .replace("%need%", need).replace("%have%", have);
    }
    public String msgVaultUnavailable() { return prefix() + color(cfg().getString("messages.vault-unavailable",  "&cVault indisponible")); }
    public String msgSpaceJumpSuccess(int dist, String dir, String cost) {
        return prefix() + color(cfg().getString("messages.space-jump-success", "&aJump %dist% %dir% (-%cost%)"))
                .replace("%dist%", String.valueOf(dist)).replace("%dir%", dir).replace("%cost%", cost);
    }
    public String msgSpaceJumpNotOnShip() { return prefix() + color(cfg().getString("messages.space-jump-not-on-ship", "&cPas sur le ship")); }
    public String msgSpaceJumpTooFar(int max, int lvl) {
        return prefix() + color(cfg().getString("messages.space-jump-too-far", "&cTrop loin (max %max% lvl %lvl%)"))
                .replace("%max%", String.valueOf(max)).replace("%lvl%", String.valueOf(lvl));
    }

    public static String color(String s) {
        if (s == null) return "";
        return s.replace('&', '\u00a7');
    }
}
