/*
 * Copyright (C) 2024  Minecon724
 * Tweaks724 is licensed under the GNU General Public License. See the LICENSE.md file
 * in the project root for the full license text.
 */

package eu.m724.tweaks;

import eu.m724.tweaks.chat.ChatCommands;
import eu.m724.tweaks.chat.ChatManager;
import eu.m724.tweaks.door.DoorManager;
import eu.m724.tweaks.hardcore.HardcoreManager;
import eu.m724.tweaks.motd.MotdManager;
import eu.m724.tweaks.ping.F3NameListener;
import eu.m724.tweaks.ping.PingChecker;
import eu.m724.tweaks.ping.PingCommands;
import eu.m724.tweaks.pomodoro.PomodoroCommands;
import eu.m724.tweaks.pomodoro.PomodoroManager;
import eu.m724.tweaks.updater.UpdaterManager;
import eu.m724.tweaks.worldborder.WorldBorderManager;
import org.bukkit.plugin.java.JavaPlugin;

import java.io.IOException;
import java.util.Locale;
import java.util.Objects;

public class TweaksPlugin extends JavaPlugin {
    @Override
    public void onEnable() {
        long start = System.nanoTime();

        TweaksConfig config = TweaksConfig.load(this);
        new Language(Locale.US); // TODO

        if (config.chatEnabled()) {
            ChatManager chatManager = new ChatManager(this);
            chatManager.init();

            ChatCommands chatCommands = new ChatCommands(chatManager);
            Objects.requireNonNull(getCommand("chat")).setExecutor(chatCommands);
            Objects.requireNonNull(getCommand("chatmanage")).setExecutor(chatCommands);
        }

        if (config.doorEnabled()) {
            new DoorManager().init(this);
        }

        if (config.brandEnabled()) {
            new F3NameListener(this).init();
        }

        new PingChecker(this).init();
        Objects.requireNonNull(getCommand("ping")).setExecutor(new PingCommands());
        Objects.requireNonNull(getCommand("dkick")).setExecutor(new PingCommands());

        /*if (getServer().getPluginManager().getPlugin("voicechat") != null) {
            new MusicPlayer(this).init();
        } else {
            getLogger().warning("To use voice extensions, install \"Simple Voice Chat\"");
        }*/

        if (config.motdEnabled()) {
            try {
                new MotdManager(this).init();
            } catch (IOException e) {
                getLogger().severe("Failed to initialize MOTD extension");
                throw new RuntimeException(e);
            }
        }

        if (config.worldborderHide()) {
            new WorldBorderManager().init(this);
        }

        if (config.pomodoroEnabled()) {
            new PomodoroManager(this).init();
            getCommand("pomodoro").setExecutor(new PomodoroCommands());
        }

        if (config.updaterEnabled()) {
            try {
                new UpdaterManager(this).init();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }

        if (config.hardcoreEnabled()) {
            new HardcoreManager().init(this);
        }


        getLogger().info("Took %.3f milliseconds".formatted((System.nanoTime() - start) / 1000000.0));
    }
}