package com.discordrules;

import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.RegisterCommandsEvent;
import net.minecraftforge.event.entity.player.PlayerEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.ModLoadingContext;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.config.ModConfig;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

@Mod("discordrules")
public class ServerRules {
    private static final Logger LOGGER = LogManager.getLogger(ServerRules.class);

    public ServerRules() {
        ModLoadingContext.get().registerConfig(ModConfig.Type.COMMON, ServerRulesConfig.SPEC);
        FMLJavaModLoadingContext.get().getModEventBus().addListener(this::onConfigLoaded);
        MinecraftForge.EVENT_BUS.register(this);
        LOGGER.info("Discord Rules Mod Initialized.");
    }

    private void onConfigLoaded(ModConfig.Loading event) {
        if (event.getConfig().getSpec() == ServerRulesConfig.SPEC) {
            String token = ServerRulesConfig.BOT_TOKEN.get();
            if (token.equals("YOUR_TOKEN_HERE")) {
                LOGGER.warn("Rules mod is using default config values! Edit config/discordrules-common.toml");
            } else {
                LOGGER.info("Rules mod config loaded successfully. Channel: {}", ServerRulesConfig.CHANNEL_ID.get());
            }
        }
    }

    @SubscribeEvent
    public void onRegisterCommands(RegisterCommandsEvent event) {
        RulesCommand.register(event.getDispatcher());
        LOGGER.info("Registered /rules command.");
    }

    @SubscribeEvent
    public void onPlayerLogout(PlayerEvent.PlayerLoggedOutEvent event) {
        CooldownManager.removePlayer(event.getEntity().getUUID());
    }
}
