package com.playerblocklife;

import org.bukkit.plugin.java.JavaPlugin;
import java.util.logging.Level;

public class PlayerBlockLife extends JavaPlugin {
    private static PlayerBlockLife instance;
    private PlayerBlockManager blockManager;
    private SkinManager skinManager;
    private LifeSystem lifeSystem;
    private ConfigManager configManager;

    // 在 PlayerBlockLife.java 中添加：

    @Override
    public void reloadConfig() {
        super.reloadConfig();
        configManager.reloadConfig();
        getLogger().info("配置已重新加载");
    }
    @Override
    public void onEnable() {
        instance = this;
        saveDefaultConfig();

        this.configManager = new ConfigManager(this);
        this.skinManager = new SkinManager(this);
        this.blockManager = new PlayerBlockManager(this, skinManager);
        this.lifeSystem = new LifeSystem(this);

        getServer().getPluginManager().registerEvents(new BlockBreakListener(this), this);
        getServer().getPluginManager().registerEvents(new PlayerJoinListener(this), this);
        getServer().getPluginManager().registerEvents(new PlayerQuitListener(this), this);

        getCommand("setlifeblocks").setExecutor(new SetLifeBlocksCommand(this));
        getCommand("checklifeblocks").setExecutor(new CheckLifeBlocksCommand(this));
        getCommand("pblreload").setExecutor(new AdminCommands(this));
        getCommand("pbldelete").setExecutor(new AdminCommands(this));

        blockManager.loadData();
        skinManager.loadAllSkins();

        startScheduler();

        getLogger().info("§a========================================");
        getLogger().info("§ePlayerBlockLife v" + getDescription().getVersion() + " 已启用");
        getLogger().info("§e作者: " + getDescription().getAuthors());
        getLogger().info("§a========================================");
    }

    @Override
    public void onDisable() {
        if (blockManager != null) {
            blockManager.saveData();
        }
        if (skinManager != null) {
            skinManager.saveSkinData();
        }
        getLogger().info("§cPlayerBlockLife 插件已禁用");
    }

    private void startScheduler() {
        getServer().getScheduler().runTaskTimerAsynchronously(this, () -> {
            blockManager.saveData();
            skinManager.saveSkinData();
        }, 6000L, 6000L);

        getServer().getScheduler().runTaskTimer(this, () -> {
            lifeSystem.checkAllPlayers();
        }, 200L, 200L);
    }

    public static PlayerBlockLife getInstance() {
        return instance;
    }

    public PlayerBlockManager getBlockManager() {
        return blockManager;
    }

    public SkinManager getSkinManager() {
        return skinManager;
    }

    public LifeSystem getLifeSystem() {
        return lifeSystem;
    }

    public ConfigManager getConfigManager() {
        return configManager;
    }

    public void logInfo(String message) {
        getLogger().info(message);
    }

    public void logWarning(String message) {
        getLogger().warning(message);
    }

    public void logError(String message, Throwable throwable) {
        getLogger().log(Level.SEVERE, message, throwable);
    }
}