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 sale prices for slave trading.
 *
 *
 * Provides default sale prices and can be extended
 * to load from config files in the future.
 *
 * Based on original SaleLoader from 1.12.2
 */
public class SaleLoader {

    private static final Random RANDOM = new Random();

    /** List of available sale prices */
    private static final List<ItemTask> SALES = new ArrayList<>();

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

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

        // Default sale prices
        SALES.add(new ItemTask("minecraft:iron_ingot", 10));
        SALES.add(new ItemTask("minecraft:iron_ingot", 20));
        SALES.add(new ItemTask("minecraft:gold_ingot", 10));
        SALES.add(new ItemTask("minecraft:gold_ingot", 15));
        SALES.add(new ItemTask("minecraft:diamond", 3));
        SALES.add(new ItemTask("minecraft:diamond", 5));
        SALES.add(new ItemTask("minecraft:emerald", 5));
        SALES.add(new ItemTask("minecraft:emerald", 10));

        initialized = true;

        TiedUpMod.LOGGER.info(
            "[SaleLoader] Loaded {} default sales",
            SALES.size()
        );
    }

    /**
     * Get a random sale price.
     *
     * @return Random ItemTask for sale price
     */
    public static ItemTask getRandomSale() {
        if (SALES.isEmpty()) {
            // Fallback if not initialized
            return new ItemTask("minecraft:iron_ingot", 10);
        }

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

    /**
     * Get all available sale prices.
     *
     * @return List of all sales
     */
    public static List<ItemTask> getAllSales() {
        return new ArrayList<>(SALES);
    }

    /**
     * Check if sales are available.
     *
     * @return true if at least one sale is configured
     */
    public static boolean hasSales() {
        return !SALES.isEmpty();
    }

    /**
     * Add a custom sale price.
     *
     * @param sale The sale to add
     */
    public static void addSale(ItemTask sale) {
        if (sale != null) {
            SALES.add(sale);
        }
    }

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