package com.tiedup.remake.util;

import com.tiedup.remake.core.TiedUpMod;
import net.minecraft.world.level.GameRules;

/**
 *
 * Manages configurable values for gameplay mechanics.
 * GameRules can be changed via /gamerule command in-game.
 *
 * Based on original mod's configuration system.
 */
public class ModGameRules {

    // RESTRAINT MECHANICS

    /** Time (in seconds) required to tie up a player. Default: 5 */
    public static GameRules.Key<GameRules.IntegerValue> TYING_PLAYER_TIME;
    /** Time (in seconds) required to untie a tied player. Default: 10 */
    public static GameRules.Key<GameRules.IntegerValue> UNTYING_PLAYER_TIME;
    /** Whether gagged players can be heard within a short range. Default: true */
    public static GameRules.Key<GameRules.BooleanValue> GAG_TALK_PROXIMITY;
    /** Resistance added by a padlock when locked on an item. Default: 250 */
    public static GameRules.Key<GameRules.IntegerValue> PADLOCK_RESISTANCE;

    // STRUGGLE SYSTEM

    /** Enable/disable struggle system. Default: true */
    public static GameRules.Key<GameRules.BooleanValue> STRUGGLE;
    /** Success probability for struggling (0-100). Default: 40% */
    public static GameRules.Key<GameRules.IntegerValue> PROBABILITY_STRUGGLE;
    /** Minimum resistance decrease on successful struggle. Default: 1 */
    public static GameRules.Key<GameRules.IntegerValue> STRUGGLE_MIN_DECREASE;
    /** Maximum resistance decrease on successful struggle. Default: 10 */
    public static GameRules.Key<GameRules.IntegerValue> STRUGGLE_MAX_DECREASE;
    /** Cooldown between struggle attempts (ticks, 20 = 1s). Default: 80 (4s) */
    public static GameRules.Key<GameRules.IntegerValue> STRUGGLE_TIMER;
    /** Ticks per 1 resistance point in continuous struggle. Default: 20 (1/s) */
    public static GameRules.Key<
        GameRules.IntegerValue
    > STRUGGLE_CONTINUOUS_RATE;
    /** Probability of random shock during collar struggle (0-100). Default: 20% */
    public static GameRules.Key<
        GameRules.IntegerValue
    > STRUGGLE_COLLAR_RANDOM_SHOCK;

    // BUG-003: RESISTANCE_ROPE, RESISTANCE_GAG, RESISTANCE_BLINDFOLD, RESISTANCE_COLLAR

    // NPC STRUGGLE

    /** Enable/disable NPC struggle to escape restraints. Default: true */
    public static GameRules.Key<GameRules.BooleanValue> NPC_STRUGGLE_ENABLED;
    /** Base interval (ticks) between NPC struggle attempts. Default: 6000 (5 min) */
    public static GameRules.Key<GameRules.IntegerValue> NPC_STRUGGLE_INTERVAL;

    // COLLAR & SHOCKER

    /** Enable/disable enslavement system. Default: true */
    public static GameRules.Key<GameRules.BooleanValue> ENSLAVEMENT_ENABLED;
    /** Base radius for shocker controller. Default: 50 blocks */
    public static GameRules.Key<
        GameRules.IntegerValue
    > SHOCKER_CONTROLLER_BASE_RADIUS;

    // NPC SPAWNING

    /** Enable/disable damsel entity spawning. Default: true */
    public static GameRules.Key<GameRules.BooleanValue> DAMSELS_SPAWN;
    /** Enable/disable kidnapper entity spawning (all variants). Default: true */
    public static GameRules.Key<GameRules.BooleanValue> KIDNAPPERS_SPAWN;
    /** Damsel spawn rate (0-100). Default: 100 */
    public static GameRules.Key<GameRules.IntegerValue> DAMSEL_SPAWN_RATE;
    /** Kidnapper spawn rate (0-100). Default: 100 */
    public static GameRules.Key<GameRules.IntegerValue> KIDNAPPER_SPAWN_RATE;
    /** Kidnapper Archer spawn rate (0-100). Default: 100 */
    public static GameRules.Key<
        GameRules.IntegerValue
    > KIDNAPPER_ARCHER_SPAWN_RATE;
    /** Kidnapper Elite spawn rate (0-100). Default: 100 */
    public static GameRules.Key<
        GameRules.IntegerValue
    > KIDNAPPER_ELITE_SPAWN_RATE;
    /** Kidnapper Merchant spawn rate (0-100). Default: 100 */
    public static GameRules.Key<
        GameRules.IntegerValue
    > KIDNAPPER_MERCHANT_SPAWN_RATE;
    /** Master spawn rate (0-100). Default: 100 */
    public static GameRules.Key<GameRules.IntegerValue> MASTER_SPAWN_RATE;
    /** Spawn gender mode: 0=BOTH, 1=FEMALE_ONLY, 2=MALE_ONLY. Default: 0 */
    public static GameRules.Key<GameRules.IntegerValue> SPAWN_GENDER_MODE;

    // BOUNTY SYSTEM

    /** Maximum bounties per player. Default: 5 */
    public static GameRules.Key<GameRules.IntegerValue> MAX_BOUNTIES;
    /** Duration of bounties in seconds. Default: 14400 (4 hours) */
    public static GameRules.Key<GameRules.IntegerValue> BOUNTY_DURATION;
    /** Radius for bounty delivery detection. Default: 5 blocks */
    public static GameRules.Key<GameRules.IntegerValue> BOUNTY_DELIVERY_RADIUS;

    // MISCELLANEOUS

    /** Kidnap bomb explosion radius. Default: 5 blocks */
    public static GameRules.Key<GameRules.IntegerValue> KIDNAP_BOMB_RADIUS;

    /**
     * Register all custom GameRules.
     * Called during mod initialization.
     */
    public static void register() {
        TiedUpMod.LOGGER.info("Registering TiedUp GameRules...");

        // NOTE: Using hardcoded defaults because ModConfig isn't loaded yet during mod construction
        GAG_TALK_PROXIMITY = GameRules.register(
            "gagTalkProximity",
            GameRules.Category.CHAT,
            GameRules.BooleanValue.create(true)
        );
        TYING_PLAYER_TIME = GameRules.register(
            "tyingPlayerTime",
            GameRules.Category.MISC,
            GameRules.IntegerValue.create(5)
        );

        UNTYING_PLAYER_TIME = GameRules.register(
            "untyingPlayerTime",
            GameRules.Category.MISC,
            GameRules.IntegerValue.create(10)
        );

        STRUGGLE = GameRules.register(
            "struggle",
            GameRules.Category.MISC,
            GameRules.BooleanValue.create(true)
        );

        PROBABILITY_STRUGGLE = GameRules.register(
            "probabilityStruggle",
            GameRules.Category.MISC,
            GameRules.IntegerValue.create(40)
        );

        STRUGGLE_MIN_DECREASE = GameRules.register(
            "struggleMinDecrease",
            GameRules.Category.MISC,
            GameRules.IntegerValue.create(1)
        );

        STRUGGLE_MAX_DECREASE = GameRules.register(
            "struggleMaxDecrease",
            GameRules.Category.MISC,
            GameRules.IntegerValue.create(10)
        );

        STRUGGLE_TIMER = GameRules.register(
            "struggleTimer",
            GameRules.Category.MISC,
            GameRules.IntegerValue.create(80)
        );

        // BUG-003: Removed resistanceRope/Gag/Blindfold/Collar GameRule registrations.
        // Bind resistance is now managed by ModConfig via SettingsAccessor.

        STRUGGLE_COLLAR_RANDOM_SHOCK = GameRules.register(
            "struggleCollarRandomShock",
            GameRules.Category.MISC,
            GameRules.IntegerValue.create(20)
        );

        SHOCKER_CONTROLLER_BASE_RADIUS = GameRules.register(
            "shockerControllerBaseRadius",
            GameRules.Category.MISC,
            GameRules.IntegerValue.create(50)
        );

        ENSLAVEMENT_ENABLED = GameRules.register(
            "enslavementEnabled",
            GameRules.Category.MISC,
            GameRules.BooleanValue.create(true)
        );

        DAMSELS_SPAWN = GameRules.register(
            "damselsSpawn",
            GameRules.Category.SPAWNING,
            GameRules.BooleanValue.create(true)
        );

        // Kidnapper spawning (includes Elite, Archer, Merchant)
        KIDNAPPERS_SPAWN = GameRules.register(
            "kidnappersSpawn",
            GameRules.Category.SPAWNING,
            GameRules.BooleanValue.create(true)
        );

        // NPC spawn rates (0-100 percentage)
        // BUG-001 FIX: Defaults now match ModConfig values instead of all being 100
        DAMSEL_SPAWN_RATE = GameRules.register(
            "damselSpawnRate",
            GameRules.Category.SPAWNING,
            GameRules.IntegerValue.create(60)
        );

        KIDNAPPER_SPAWN_RATE = GameRules.register(
            "kidnapperSpawnRate",
            GameRules.Category.SPAWNING,
            GameRules.IntegerValue.create(70)
        );

        KIDNAPPER_ARCHER_SPAWN_RATE = GameRules.register(
            "kidnapperArcherSpawnRate",
            GameRules.Category.SPAWNING,
            GameRules.IntegerValue.create(70)
        );

        KIDNAPPER_ELITE_SPAWN_RATE = GameRules.register(
            "kidnapperEliteSpawnRate",
            GameRules.Category.SPAWNING,
            GameRules.IntegerValue.create(70)
        );

        KIDNAPPER_MERCHANT_SPAWN_RATE = GameRules.register(
            "kidnapperMerchantSpawnRate",
            GameRules.Category.SPAWNING,
            GameRules.IntegerValue.create(70)
        );

        MASTER_SPAWN_RATE = GameRules.register(
            "masterSpawnRate",
            GameRules.Category.SPAWNING,
            GameRules.IntegerValue.create(100)
        );

        KIDNAP_BOMB_RADIUS = GameRules.register(
            "kidnapBombRadius",
            GameRules.Category.MISC,
            GameRules.IntegerValue.create(5)
        );

        SPAWN_GENDER_MODE = GameRules.register(
            "spawnGenderMode",
            GameRules.Category.SPAWNING,
            GameRules.IntegerValue.create(0)
        );

        MAX_BOUNTIES = GameRules.register(
            "maxBounties",
            GameRules.Category.MISC,
            GameRules.IntegerValue.create(5)
        );

        BOUNTY_DURATION = GameRules.register(
            "bountyDuration",
            GameRules.Category.MISC,
            GameRules.IntegerValue.create(14400)
        );

        BOUNTY_DELIVERY_RADIUS = GameRules.register(
            "bountyDeliveryRadius",
            GameRules.Category.MISC,
            GameRules.IntegerValue.create(5)
        );

        PADLOCK_RESISTANCE = GameRules.register(
            "padlockResistance",
            GameRules.Category.MISC,
            GameRules.IntegerValue.create(250)
        );

        STRUGGLE_CONTINUOUS_RATE = GameRules.register(
            "struggleContinuousRate",
            GameRules.Category.MISC,
            GameRules.IntegerValue.create(20)
        );

        NPC_STRUGGLE_ENABLED = GameRules.register(
            "npcStruggleEnabled",
            GameRules.Category.MISC,
            GameRules.BooleanValue.create(true)
        );

        NPC_STRUGGLE_INTERVAL = GameRules.register(
            "npcStruggleInterval",
            GameRules.Category.MISC,
            GameRules.IntegerValue.create(6000)
        );

        TiedUpMod.LOGGER.info("Registered {} TiedUp GameRules", 28);
    }

    // All getter methods removed in H4-F.
    // Call sites now use SettingsAccessor which reads GameRules fields directly.
    // BUG-003: getResistance() was removed earlier. Use SettingsAccessor.getBindResistance().
    // Chloroform fake GameRule (just read ModConfig) replaced by SettingsAccessor.getChloroformDuration().
}
