// Golf System Configuration
// Contains all constants, IDs, and configuration settings

// Entity and Item IDs
export const GOLFBALL_ID = "dss_nocturne:dss_normal_golfball";
export const PUTTER_ID = "block_jesu:dss_gep_golf_putter";
export const IRON_ID = "block_jesu:dss_gep_iron";
export const FAIRWAY_ID = "block_jesu:dss_gep_fairway";
export const DRIVER_ID = "block_jesu:dss_gep_driver";
export const WEDGE_ID = "block_jesu:dss_gep_wedge";
export const HOLE_BLOCK_ID = "block_jesu:dss_gep_hole";
export const LEDGER_ID = "block_jesu:dss_gep_ledger";
export const WATER_ID = "minecraft:water";
export const WATER_FLOW_ID = "minecraft:flowing_water";

export const GOLFBALL_SPAWN_EGG_ID = "dss_nocturne:dss_normal_golfball_placer";

// Added golf clubs variants
export const PUTTERS = [
    "block_jesu:dss_gep_golf_putter",
    "block_jesu:dss_gep_golf_putter_2",
    "block_jesu:dss_gep_golf_putter_3",
    "block_jesu:dss_gep_golf_putter_4",
    "block_jesu:dss_gep_golf_putter_5"
]

export const IRONS = [
    "block_jesu:dss_gep_iron",
    "block_jesu:dss_gep_iron_2",
    "block_jesu:dss_gep_iron_3",
    "block_jesu:dss_gep_iron_4",
    "block_jesu:dss_gep_iron_5"
]

export const FAIRWAYS = [
    "block_jesu:dss_gep_fairway",
    "block_jesu:dss_gep_fairway_2",
    "block_jesu:dss_gep_fairway_3",
    "block_jesu:dss_gep_fairway_4",
    "block_jesu:dss_gep_fairway_5"
]

export const DRIVERS = [
    "block_jesu:dss_gep_driver",
    "block_jesu:dss_gep_driver_2",
    "block_jesu:dss_gep_driver_3",
    "block_jesu:dss_gep_driver_4",
    "block_jesu:dss_gep_driver_5"
]

export const WEDGES = [
    "block_jesu:dss_gep_wedge",
    "block_jesu:dss_gep_wedge_2",
    "block_jesu:dss_gep_wedge_3",
    "block_jesu:dss_gep_wedge_4",
    "block_jesu:dss_gep_wedge_5"
]

export const CLUB_VARIANT_NAMES = {
    1: "Oug",
    2: "Mazuna",
    3: "Halon",
    4: "Temon",
    5: "Pitalis"
}

// Club Configuration [Pro]
export const CLUB_CONFIGS = {
    [PUTTER_ID]: {
        name: "putter",
        strengthMultiplier: 0.5,
        strengthRange: [1, 15],
        arcRange: [0, 0], // Fixed at 0 for flat shots
        maxDistance: 5.0,
        hitSound: "gep.club.hit.putter",
        baseStrengthMultiplier: 1
    },
    [IRON_ID]: {
        name: "iron",
        strengthMultiplier: 1.2,
        strengthRange: [3, 6],
        arcRange: [2, 15],
        maxDistance: 40.0,
        baseStrengthMultiplier: 1
    },
    [FAIRWAY_ID]: {
        name: "fairway",
        strengthMultiplier: 2.3,
        strengthRange: [3, 5],
        arcRange: [6, 8],
        maxDistance: 50.0,
        hitSound: "gep.club.hit.fairway",
        baseStrengthMultiplier: 1
    },
    [DRIVER_ID]: {
        name: "driver",
        strengthMultiplier: 14.0,
        strengthRange: [1, 1],
        arcRange: [2, 10],
        maxDistance: 50.0,
        hitSound: "gep.club.hit.driver",
        baseStrengthMultiplier: 1
    },
    [WEDGE_ID]: {
        name: "wedge",
        strengthMultiplier: 0.8,
        strengthRange: [3, 5],
        arcRange: [30, 50],
        maxDistance: 30.0,
        baseStrengthMultiplier: 1
    },
    
    // Putter variants (2-5) - same config as base putter
    "block_jesu:dss_gep_golf_putter_2": {
        name: "putter",
        strengthMultiplier: 0.5,
        strengthRange: [1, 15],
        arcRange: [0, 0],
        maxDistance: 5.0,
        hitSound: "gep.club.hit.putter",
        baseStrengthMultiplier: 1
    },
    "block_jesu:dss_gep_golf_putter_3": {
        name: "putter",
        strengthMultiplier: 0.5,
        strengthRange: [1, 15],
        arcRange: [0, 0],
        maxDistance: 5.0,
        hitSound: "gep.club.hit.putter",
        baseStrengthMultiplier: 1
    },
    "block_jesu:dss_gep_golf_putter_4": {
        name: "putter",
        strengthMultiplier: 0.5,
        strengthRange: [1, 15],
        arcRange: [0, 0],
        maxDistance: 5.0,
        hitSound: "gep.club.hit.putter",
        baseStrengthMultiplier: 1
    },
    "block_jesu:dss_gep_golf_putter_5": {
        name: "putter",
        strengthMultiplier: 0.5,
        strengthRange: [1, 15],
        arcRange: [0, 0],
        maxDistance: 5.0,
        hitSound: "gep.club.hit.putter",
        baseStrengthMultiplier: 1
    },
    
    // Iron variants (2-5) - same config as base iron
    "block_jesu:dss_gep_iron_2": {
        name: "iron",
        strengthMultiplier: 1.2,
        strengthRange: [3, 6],
        arcRange: [2, 15],
        maxDistance: 40.0,
        baseStrengthMultiplier: 1
    },
    "block_jesu:dss_gep_iron_3": {
        name: "iron",
        strengthMultiplier: 1.2,
        strengthRange: [3, 6],
        arcRange: [2, 15],
        maxDistance: 40.0,
        baseStrengthMultiplier: 1
    },
    "block_jesu:dss_gep_iron_4": {
        name: "iron",
        strengthMultiplier: 1.2,
        strengthRange: [3, 6],
        arcRange: [2, 15],
        maxDistance: 40.0,
        baseStrengthMultiplier: 1
    },
    "block_jesu:dss_gep_iron_5": {
        name: "iron",
        strengthMultiplier: 1.2,
        strengthRange: [3, 6],
        arcRange: [2, 15],
        maxDistance: 40.0,
        baseStrengthMultiplier: 1
    },
    
    // Fairway variants (2-5) - same config as base fairway
    "block_jesu:dss_gep_fairway_2": {
        name: "fairway",
        strengthMultiplier: 2.3,
        strengthRange: [3, 5],
        arcRange: [6, 8],
        maxDistance: 50.0,
        hitSound: "gep.club.hit.fairway",
        baseStrengthMultiplier: 1
    },
    "block_jesu:dss_gep_fairway_3": {
        name: "fairway",
        strengthMultiplier: 2.3,
        strengthRange: [3, 5],
        arcRange: [6, 8],
        maxDistance: 50.0,
        hitSound: "gep.club.hit.fairway",
        baseStrengthMultiplier: 1
    },
    "block_jesu:dss_gep_fairway_4": {
        name: "fairway",
        strengthMultiplier: 2.3,
        strengthRange: [3, 5],
        arcRange: [6, 8],
        maxDistance: 50.0,
        hitSound: "gep.club.hit.fairway",
        baseStrengthMultiplier: 1
    },
    "block_jesu:dss_gep_fairway_5": {
        name: "fairway",
        strengthMultiplier: 2.3,
        strengthRange: [3, 5],
        arcRange: [6, 8],
        maxDistance: 50.0,
        hitSound: "gep.club.hit.fairway",
        baseStrengthMultiplier: 1
    },
    
    // Driver variants (2-5) - same config as base driver
    "block_jesu:dss_gep_driver_2": {
        name: "driver",
        strengthMultiplier: 14.0,
        strengthRange: [1, 1],
        arcRange: [2, 10],
        maxDistance: 50.0,
        hitSound: "gep.club.hit.driver",
        baseStrengthMultiplier: 1
    },
    "block_jesu:dss_gep_driver_3": {
        name: "driver",
        strengthMultiplier: 14.0,
        strengthRange: [1, 1],
        arcRange: [2, 10],
        maxDistance: 50.0,
        hitSound: "gep.club.hit.driver",
        baseStrengthMultiplier: 1
    },
    "block_jesu:dss_gep_driver_4": {
        name: "driver",
        strengthMultiplier: 14.0,
        strengthRange: [1, 1],
        arcRange: [2, 10],
        maxDistance: 50.0,
        hitSound: "gep.club.hit.driver",
        baseStrengthMultiplier: 1
    },
    "block_jesu:dss_gep_driver_5": {
        name: "driver",
        strengthMultiplier: 14.0,
        strengthRange: [1, 1],
        arcRange: [2, 10],
        maxDistance: 50.0,
        hitSound: "gep.club.hit.driver",
        baseStrengthMultiplier: 1
    },
    
    // Wedge variants (2-5) - same config as base wedge
    "block_jesu:dss_gep_wedge_2": {
        name: "wedge",
        strengthMultiplier: 0.8,
        strengthRange: [3, 5],
        arcRange: [30, 50],
        maxDistance: 30.0,
        baseStrengthMultiplier: 1
    },
    "block_jesu:dss_gep_wedge_3": {
        name: "wedge",
        strengthMultiplier: 0.8,
        strengthRange: [3, 5],
        arcRange: [30, 50],
        maxDistance: 30.0,
        baseStrengthMultiplier: 1
    },
    "block_jesu:dss_gep_wedge_4": {
        name: "wedge",
        strengthMultiplier: 0.8,
        strengthRange: [3, 5],
        arcRange: [30, 50],
        maxDistance: 30.0,
        baseStrengthMultiplier: 1
    },
    "block_jesu:dss_gep_wedge_5": {
        name: "wedge",
        strengthMultiplier: 0.8,
        strengthRange: [3, 5],
        arcRange: [30, 50],
        maxDistance: 30.0,
        baseStrengthMultiplier: 1
    }
};

export const GOLFTRAILS = {
    green: "textures/dss/gep/icons/strlg_icon.png",
    blue: "textures/dss/gep/icons/strlb_icon.png",
    red: "textures/dss/gep/icons/strlr_icon.png",
    yellow: "textures/dss/gep/icons/strly_icon.png",
    gray: "textures/dss/gep/icons/strle_icon.png"
}

// Sound mappings for each golf club type
export const CLUB_SOUNDS = {
    // Base clubs
    [PUTTER_ID]: "gep.club.swing.putter",
    [IRON_ID]: "gep.club.swing.iron",
    [FAIRWAY_ID]: "gep.club.swing.fairway",
    [DRIVER_ID]: "gep.club.swing.driver",
    [WEDGE_ID]: "gep.club.swing.wedge",
    
    // Putter variants
    "block_jesu:dss_gep_golf_putter_2": "gep.club.swing.putter",
    "block_jesu:dss_gep_golf_putter_3": "gep.club.swing.putter",
    "block_jesu:dss_gep_golf_putter_4": "gep.club.swing.putter",
    "block_jesu:dss_gep_golf_putter_5": "gep.club.swing.putter",
    
    // Iron variants
    "block_jesu:dss_gep_iron_2": "gep.club.swing.iron",
    "block_jesu:dss_gep_iron_3": "gep.club.swing.iron",
    "block_jesu:dss_gep_iron_4": "gep.club.swing.iron",
    "block_jesu:dss_gep_iron_5": "gep.club.swing.iron",
    
    // Fairway variants
    "block_jesu:dss_gep_fairway_2": "gep.club.swing.fairway",
    "block_jesu:dss_gep_fairway_3": "gep.club.swing.fairway",
    "block_jesu:dss_gep_fairway_4": "gep.club.swing.fairway",
    "block_jesu:dss_gep_fairway_5": "gep.club.swing.fairway",
    
    // Driver variants
    "block_jesu:dss_gep_driver_2": "gep.club.swing.driver",
    "block_jesu:dss_gep_driver_3": "gep.club.swing.driver",
    "block_jesu:dss_gep_driver_4": "gep.club.swing.driver",
    "block_jesu:dss_gep_driver_5": "gep.club.swing.driver",
    
    // Wedge variants
    "block_jesu:dss_gep_wedge_2": "gep.club.swing.wedge",
    "block_jesu:dss_gep_wedge_3": "gep.club.swing.wedge",
    "block_jesu:dss_gep_wedge_4": "gep.club.swing.wedge",
    "block_jesu:dss_gep_wedge_5": "gep.club.swing.wedge"
};

// Physics Constants
export const PHYSICS = {
    MAX_VELOCITY: 30,
    WORLD_MIN: -30000000,
    WORLD_MAX: 30000000,
    MIN_Y: -64,
    MAX_Y: 320,
    SAFE_VELOCITY_FALLBACK: 10,
    SAFE_Y_VELOCITY_FALLBACK: 5
};

// Trail Effect Settings
export const TRAIL_SETTINGS = {
    STATIONARY_CLEANUP_TICKS: 100,
    MINIMUM_MOVEMENT_THRESHOLD: 0.1,
    PARTICLES_PER_TRAIL: 2,
    TRAIL_OFFSET_RANGE: 0.2,
    LINE_PARTICLES_MULTIPLIER: 8,
    NEARBY_PLAYER_DISABLE_DISTANCE: 3
};

// Ball Location Tracking
export const BALL_TRACKING = {
    UPDATE_INTERVAL_TICKS: 1,
    MAX_SEARCH_DISTANCE: 200,
    DIRECTION_ANGLE_SEGMENTS: 8
};

// Scoring System
export const SCORING = {
    HOLE_SPEED_THRESHOLD: 0.5,
    MAX_LEDGER_ENTRIES: 20,
    MAX_HISTORY_ENTRIES: 5,
    CLICK_TIMEOUT_MS: 1000
};

// Particle Effects
export const PARTICLES = {
    PRIMARY: "dss_gep:dss_gep_blue_trail",
    ALT1: "dss_gep:dss_gep_gray_trail",
    ALT2: "dss_gep:dss_gep_green_trail",
    ALT3: "dss_gep:dss_gep_red_trail",
    ALT4: "dss_gep:dss_gep_yellow_trail",
    FALLBACK_1: "minecraft:basic_flame_particle", 
    FALLBACK_2: "minecraft:basic_bubble_particle",
    HIT_EFFECT: "minecraft:critical_hit_emitter"
};

// Sound Effects
export const SOUNDS = {
    BALL_HIT: "gep.golf.ball.hit",
    FALLBACK_SWING: "random.click",
    FALLBACK_HIT: "random.orb",
    PUTTING: "gep.club.hit.putter",
    FAIRWAY: "gep.club.hit.fairway",
    DRIVER: "gep.club.hit.driver",
    HOLE: "gep.club.hole"
};

// System Intervals
export const INTERVALS = {
    MAIN_LOOP_TICKS: 2,
    BALL_LOCATION_UPDATE_TICKS: 10
};

// Render Distance Configuration
export const RENDER_DISTANCE = {
    // Render distance ranges and corresponding power multipliers
    LOW: {
        minChunks: 8,
        maxChunks: 10,
        powerMultiplier: 0.5
    },
    MID: {
        minChunks: 12,
        maxChunks: 16,
        powerMultiplier: 0.75
    },
    REAL: {
        minChunks: 18,
        maxChunks: 24,
        powerMultiplier: 1.0
    }
};

// Utility function to get render distance power multiplier
export function getRenderDistancePowerMultiplier(renderDistance) {
    if (renderDistance >= RENDER_DISTANCE.REAL.minChunks && 
        renderDistance <= RENDER_DISTANCE.REAL.maxChunks) {
        return RENDER_DISTANCE.REAL.powerMultiplier;
    } else if (renderDistance >= RENDER_DISTANCE.MID.minChunks && 
               renderDistance <= RENDER_DISTANCE.MID.maxChunks) {
        return RENDER_DISTANCE.MID.powerMultiplier;
    } else if (renderDistance >= RENDER_DISTANCE.LOW.minChunks && 
               renderDistance <= RENDER_DISTANCE.LOW.maxChunks) {
        return RENDER_DISTANCE.LOW.powerMultiplier;
    }
    // Default fallback
    return 0.75;
}

// Function to detect render distance from simulation distance
export function detectRenderDistanceFromSimulation(simulationDistance) {
    // Map simulation distance to render distance ranges
    if (simulationDistance <= 4) return 8;  // Low: 8-10 chunks
    if (simulationDistance <= 6) return 12; // Mid: 12-16 chunks  
    if (simulationDistance <= 8) return 18; // Real: 18-24 chunks
    return 12; // Default fallback
}

// Function to get current render distance from player settings
export function getCurrentRenderDistance(player) {
    // Try to get simulation distance as proxy for render distance
    try {
        // In Minecraft Bedrock, we can use simulation distance as a proxy
        // since direct render distance access isn't available via API
        const simulationDistance = 12; // Default simulation distance
        return detectRenderDistanceFromSimulation(simulationDistance);
    } catch (error) {
        return 12; // Default fallback
    }
}

// Function to update club strength based on render distance
export function updateClubStrengthByRenderDistance(clubConfig, renderDistance) {
    const powerMultiplier = getRenderDistancePowerMultiplier(renderDistance);
    return {
        ...clubConfig,
        strengthMultiplier: clubConfig.baseStrengthMultiplier * powerMultiplier
    };
}