package moe.oko.opennaw;

import moe.oko.opennaw.command.CityCommand;
import moe.oko.opennaw.command.NationCommand;
import moe.oko.opennaw.listener.ActionListener;
import moe.oko.opennaw.storage.NAWDatabase;
import moe.oko.opennaw.util.*;
import net.luckperms.api.LuckPerms;
import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin;

import java.io.File;

import static moe.oko.opennaw.util.CommandHelper.info;

public final class OpenNAW extends JavaPlugin {

    public static OpenNAW instance;
    public NationHandler nationHandler;

    public ActionLogger actionLogger;

    public CityHandler cityHandler;

    public NAWDatabase database;

    public ConfigHelper configHelper;

    public GroupHandler groupHandler;
    private LuckPerms luckPerms;

    private DynmapHandler dynmapHandler;

    private boolean dynmapEnabled;

    public static OpenNAW getInstance() {
        return instance;
    }

    public NationHandler getNationHandler() {
        return nationHandler;
    }
    public CityHandler getCityHandler(){ return cityHandler; }
    public GroupHandler getGroupHandler() { return groupHandler; }

    @Override
    public void onEnable() {
        // Initialize internals
        instance = this;
        if(!new File(getDataFolder().getAbsolutePath() + "/config.yml").exists()) {
            saveDefaultConfig(); //Default if non existent.
        } else {
            saveConfig(); //Save cfg on start
        }
        configHelper = new ConfigHelper(this.getConfig());
        this.luckPerms = getServer().getServicesManager().load(LuckPerms.class);
        actionLogger = new ActionLogger();
        nationHandler = new NationHandler();
        cityHandler = new CityHandler();
        groupHandler = new GroupHandler(this.luckPerms);

        info("OpenNAW is initialized.");
        info("Starting database load.");
        database = configHelper.fetchDatabase();
        info("Database initialized. Starting to fetch all values");
        nationHandler.loadNationList(database.loadNations());
        database.loadPlayers();
        cityHandler.loadCityList(database.loadCities());
        //Do not change above load order.

        // Register Commands & Events
        this.getCommand("nation").setExecutor(new NationCommand());
        this.getCommand("city").setExecutor(new CityCommand());
        getServer().getPluginManager().registerEvents(new ChatHandler(), this);
        getServer().getPluginManager().registerEvents(new ActionListener(), this);

        if(Bukkit.getPluginManager().isPluginEnabled("dynmap")) {
            this.dynmapEnabled = true;
            //Only initialize if dyn is loaded.
            dynmapHandler = new DynmapHandler();
        }
    }

    @Override
    public void onDisable() {
        info("Starting persistence save.");
        cityHandler.saveCities();
        nationHandler.saveNations();
        info("OpenNAW is disabled!");
    }

    public boolean isDynmapEnabled() {
        return dynmapEnabled;
    }

    public DynmapHandler getDynmapHandler() {
        return dynmapHandler;
    }

    public NAWDatabase getDatabase() { return database; }

    public ActionLogger getActionLogger() {
        return actionLogger;
    }

    public ConfigHelper getConfigHelper() {
        return configHelper;
    }
}
