/*
 * Original project: RedEnvelope by KaguyaStudio-MC
 * Original project link: https://github.com/KaguyaStudio-MC/RedEnvelope
 * Modified by: LeisureTimeDock
 * This file is part of RedEnvelope
 * Licensed under Camellia License Version 1.0
 */

package top.leisuretimedock.redenvelope.core.config;

import net.minecraftforge.common.ForgeConfigSpec;
import org.intellij.lang.annotations.MagicConstant;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import top.leisuretimedock.redenvelope.core.util.REUtil;

public class ServerConfig {
    private static final ForgeConfigSpec.Builder BUILDER = new ForgeConfigSpec.Builder();

    @SuppressWarnings("UnnecessaryUnicodeEscape")
    public static final ForgeConfigSpec.IntValue TTL = BUILDER.comment("The time to live of the red envelope")
            .comment("\u7ea2\u5305\u7684\u5b58\u6d3b\u65f6\u95f4")
            .defineInRange("ttl", 1200, 1, 24000);

    @SuppressWarnings("UnnecessaryUnicodeEscape")
    public static final ForgeConfigSpec.IntValue SEND_COOLDOWN = BUILDER.comment("Cooldown time between sending red envelopes (in seconds)")
            .comment("\u53d1\u9001\u7ea2\u5305\u7684\u51b7\u5374\u65f6\u95f4（秒）")
            .comment("Set to 0 to disable cooldown")
            .comment("\u8bbe\u7f6e\u4e3a0\u53ef\u4ee5\u53d6\u6d88\u51b7\u5374")
            .defineInRange("send_cooldown", 60, 0, 3600); // 默认60秒，最大1小时

    @SuppressWarnings("UnnecessaryUnicodeEscape")
    public static final ForgeConfigSpec.IntValue COMMON_COOLDOWN = BUILDER.comment("Cooldown for common red envelopes (in seconds)")
            .comment("\u666e\u901a\u7ea2\u5305\u7684\u51b7\u5374\u65f6\u95f4（秒）")
            .comment("0 means use global cooldown")
            .defineInRange("common_cooldown", 0, 0, 3600);

    @SuppressWarnings("UnnecessaryUnicodeEscape")
    public static final ForgeConfigSpec.IntValue LUCK_COOLDOWN = BUILDER.comment("Cooldown for luck red envelopes (in seconds)")
            .comment("\u8fd0\u6c14\u7ea2\u5305\u7684\u51b7\u5374\u65f6\u95f4（秒）")
            .comment("0 means use global cooldown")
            .defineInRange("luck_cooldown", 0, 0, 3600);

    @SuppressWarnings("UnnecessaryUnicodeEscape")
    public static final ForgeConfigSpec.IntValue SPELL_COOLDOWN = BUILDER.comment("Cooldown for spell red envelopes (in seconds)")
            .comment("\u5492\u8bed\u7ea2\u5305\u7684\u51b7\u5374\u65f6\u95f4（秒）")
            .comment("0 means use global cooldown")
            .defineInRange("spell_cooldown", 0, 0, 3600);

    @SuppressWarnings("UnnecessaryUnicodeEscape")
    public static final ForgeConfigSpec.IntValue DAILY_LIMIT = BUILDER.comment("Maximum number of red envelopes a player can send per day")
            .comment("\u6bcf\u4e2a\u73a9\u5bb6\u6bcf\u5929\u53ef\u4ee5\u53d1\u9001\u7684\u7ea2\u5305\u4e0a\u9650")
            .comment("Set to 0 to disable limit")
            .comment("\u8bbe\u7f6e\u4e3a0\u53ef\u4ee5\u53d6\u6d88\u4e0a\u9650")
            .defineInRange("daily_limit", 0, 0, 1000);

    @SuppressWarnings("UnnecessaryUnicodeEscape")
    public static final ForgeConfigSpec.BooleanValue ALLOW_MULTIPLE_CLAIM = BUILDER.comment("Allow players to claim the same red envelope multiple times")
            .comment("\u662f\u5426\u5141\u8bb8\u73a9\u5bb6\u591a\u6b21\u9886\u53d6\u540c\u4e00\u4e2a\u7ea2\u5305")
            .comment("If false, each player can only claim once per red envelope")
            .comment("\u5982\u679c\u4e3afalse\uff0c\u6bcf\u4e2a\u73a9\u5bb6\u53ea\u80fd\u9886\u53d6\u6bcf\u4e2a\u7ea2\u5305\u4e00\u6b21")
            .define("allow_multiple_claim", false);

    @SuppressWarnings("UnnecessaryUnicodeEscape")
    public static final ForgeConfigSpec.BooleanValue ALLOW_CLAIM_OWN = BUILDER.comment("Allow players to claim their own red envelopes")
            .comment("\u662f\u5426\u5141\u8bb8\u73a9\u5bb6\u9886\u53d6\u81ea\u5df1\u53d1\u7684\u7ea2\u5305")
            .comment("If false, players cannot claim their own red envelopes")
            .comment("\u5982\u679c\u4e3afalse\uff0c\u73a9\u5bb6\u4e0d\u80fd\u9886\u53d6\u81ea\u5df1\u53d1\u7684\u7ea2\u5305")
            .define("allow_claim_own", true);



    public static final ForgeConfigSpec SPEC = BUILDER.build();

    // ===== 辅助方法 =====

    /**
     * 获取指定类型红包的冷却时间
     */
    public static int getCooldownForType(@Nullable @MagicConstant( stringValues = { REUtil.LUCK, REUtil.SPELL, REUtil.COMMON }) String type) {
        int cooldown = 0;
        if (type != null) {
            cooldown = switch (type) {
                case REUtil.COMMON -> COMMON_COOLDOWN.get();
                case REUtil.LUCK -> LUCK_COOLDOWN.get();
                case REUtil.SPELL -> SPELL_COOLDOWN.get();
                default -> SEND_COOLDOWN.get();
            };
        }

        // 如果单独配置为0，使用全局冷却
        return cooldown > 0 ? cooldown : SEND_COOLDOWN.get();
    }

    /**
     * 检查是否有冷却时间限制
     */
    public static boolean hasCooldown() {
        return SEND_COOLDOWN.get() > 0;
    }

}
