package com.tiedup.remake.util.tasks;

import com.tiedup.remake.core.TiedUpMod;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * Loads and manages job tasks for slave work system.
 *
 *
 * A job is an ItemTask that a slave must complete
 * (fetch X items) within a time limit or face punishment.
 *
 * Based on original JobLoader from 1.12.2
 */
public class JobLoader {

    private static final Random RANDOM = new Random();

    /** List of available jobs */
    private static final List<ItemTask> JOBS = new ArrayList<>();

    /** Whether jobs have been initialized */
    private static boolean initialized = false;

    /**
     * Initialize default jobs.
     * Called on mod initialization.
     */
    public static void init() {
        if (initialized) return;

        // Default jobs - common gathering tasks
        // Easy jobs
        JOBS.add(new ItemTask("minecraft:cobblestone", 16));
        JOBS.add(new ItemTask("minecraft:dirt", 16));
        JOBS.add(new ItemTask("minecraft:oak_log", 8));
        JOBS.add(new ItemTask("minecraft:wheat", 10));
        JOBS.add(new ItemTask("minecraft:potato", 10));
        JOBS.add(new ItemTask("minecraft:carrot", 10));
        JOBS.add(new ItemTask("minecraft:apple", 5));

        // Medium jobs
        JOBS.add(new ItemTask("minecraft:iron_ore", 5));
        JOBS.add(new ItemTask("minecraft:coal", 10));
        JOBS.add(new ItemTask("minecraft:raw_iron", 5));
        JOBS.add(new ItemTask("minecraft:raw_copper", 10));
        JOBS.add(new ItemTask("minecraft:leather", 5));
        JOBS.add(new ItemTask("minecraft:string", 8));
        JOBS.add(new ItemTask("minecraft:paper", 10));
        JOBS.add(new ItemTask("minecraft:book", 3));

        // Hard jobs
        JOBS.add(new ItemTask("minecraft:raw_gold", 3));
        JOBS.add(new ItemTask("minecraft:diamond", 1));
        JOBS.add(new ItemTask("minecraft:emerald", 1));
        JOBS.add(new ItemTask("minecraft:blaze_rod", 2));
        JOBS.add(new ItemTask("minecraft:ender_pearl", 2));
        JOBS.add(new ItemTask("minecraft:obsidian", 4));

        initialized = true;

        TiedUpMod.LOGGER.info(
            "[JobLoader] Loaded {} default jobs",
            JOBS.size()
        );
    }

    /**
     * Get a random job task.
     *
     * @return Random ItemTask for a job
     */
    public static ItemTask getRandomJob() {
        if (JOBS.isEmpty()) {
            // Fallback if not initialized
            return new ItemTask("minecraft:cobblestone", 16);
        }

        return JOBS.get(RANDOM.nextInt(JOBS.size()));
    }

    /**
     * Get a random job from a difficulty tier.
     *
     * @param difficulty 0 = easy, 1 = medium, 2 = hard
     * @return Random ItemTask from that tier
     */
    public static ItemTask getRandomJob(int difficulty) {
        if (JOBS.isEmpty()) {
            return new ItemTask("minecraft:cobblestone", 16);
        }

        // Simple tier system based on list indices
        int tierSize = JOBS.size() / 3;
        int startIndex = difficulty * tierSize;
        int endIndex = Math.min(startIndex + tierSize, JOBS.size());

        if (startIndex >= JOBS.size()) {
            startIndex = 0;
            endIndex = tierSize;
        }

        int range = endIndex - startIndex;
        if (range <= 0) range = 1;

        return JOBS.get(startIndex + RANDOM.nextInt(range));
    }

    /**
     * Get all available jobs.
     *
     * @return List of all jobs
     */
    public static List<ItemTask> getAllJobs() {
        return new ArrayList<>(JOBS);
    }

    /**
     * Check if jobs are available.
     *
     * @return true if at least one job is configured
     */
    public static boolean hasJobs() {
        return !JOBS.isEmpty();
    }

    /**
     * Add a custom job.
     *
     * @param job The job to add
     */
    public static void addJob(ItemTask job) {
        if (job != null) {
            JOBS.add(job);
        }
    }

    /**
     * Clear all jobs (for reloading).
     */
    public static void clear() {
        JOBS.clear();
        initialized = false;
    }
}
