package fr.luc.gamingcore.hook;

import fr.luc.gamingcore.Main;
import fr.luc.gamingcore.manager.GuildManager;
import fr.luc.gamingcore.model.Guild;
import fr.luc.gamingcore.model.GuildMember;
import fr.luc.gamingcore.model.GuildRank;
import me.clip.placeholderapi.expansion.PlaceholderExpansion;
import org.bukkit.OfflinePlayer;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
 * Expansion PlaceholderAPI pour GamingCore.
 * Expose les données des guildes (tag, couleur, nom, rang, banque, claims)
 * aux plugins tiers comme TAB, EssentialsChat, DecentHolograms, etc.
 */
public class GamingCorePlaceholderExpansion extends PlaceholderExpansion {

    private final Main plugin;
    private final GuildManager guildManager;

    /**
     * Constructeur de l'expansion PlaceholderAPI.
     *
     * @param plugin Instance principale du plugin.
     */
    public GamingCorePlaceholderExpansion(Main plugin) {
        this.plugin = plugin;
        this.guildManager = plugin.getGuildManager();
    }

    @Override
    public @NotNull String getIdentifier() {
        return "gamingcore";
    }

    @Override
    public @NotNull String getAuthor() {
        return "Luc";
    }

    @Override
    public @NotNull String getVersion() {
        return plugin.getDescription().getVersion();
    }

    @Override
    public boolean persist() {
        return true;
    }

    @Override
    public @Nullable String onRequest(OfflinePlayer player, @NotNull String params) {
        if (player == null) {
            return "";
        }

        Guild guild = guildManager.getGuildByPlayer(player.getUniqueId());

        // Placeholders de vérification
        if (params.equalsIgnoreCase("has_guild")) {
            return guild != null ? "true" : "false";
        }

        if (guild == null) {
            if (params.equalsIgnoreCase("guild_rank_priority") || params.equalsIgnoreCase("rank_priority")) {
                return "0";
            }
            if (params.equalsIgnoreCase("guild_tag") || params.equalsIgnoreCase("tag") ||
                    params.equalsIgnoreCase("guild_name") || params.equalsIgnoreCase("guild") ||
                    params.equalsIgnoreCase("guild_formatted_tag") || params.equalsIgnoreCase("guild_chat_tag") ||
                    params.equalsIgnoreCase("guild_rank") || params.equalsIgnoreCase("rank")) {
                return "";
            }
            if (params.equalsIgnoreCase("guild_color") || params.equalsIgnoreCase("color")) {
                return "#FFFFFF";
            }
            return "";
        }

        GuildMember member = guild.getMember(player.getUniqueId());
        GuildRank rank = member != null ? guild.getRank(member.getRankId()) : null;
        String rankName = rank != null ? rank.getName() : "Membre";
        int rankPriority = rank != null ? rank.getPriority() : 1;

        switch (params.toLowerCase()) {
            case "guild":
            case "guild_name":
            case "name":
                return guild.getName();

            case "guild_tag":
            case "tag":
                return guild.getTag();

            case "guild_tag_brackets":
                return "[" + guild.getTag() + "]";

            case "guild_color":
            case "color":
                return guild.getColor();

            case "guild_formatted_tag":
            case "formatted_tag":
                // Format HEX compatible TAB et EssentialsChat (ex: &#00C9FF[VAL]&r )
                return "&" + guild.getColor() + "[" + guild.getTag() + "]&r ";

            case "guild_chat_tag":
            case "chat_tag":
                return "&" + guild.getColor() + "[" + guild.getTag() + "]&r ";

            case "guild_rank":
            case "rank":
                return rankName;

            case "guild_rank_priority":
                return String.valueOf(rankPriority);

            case "guild_balance":
            case "balance":
                return guildManager.getEconomyManager().format(guild.getBalance());

            case "guild_claims":
            case "claims":
                return String.valueOf(guild.getClaimCount());

            case "guild_members":
            case "members":
                return String.valueOf(guild.getMemberCount());

            default:
                return null;
        }
    }
}
