package de.viper.survivalplus.Manager;

import de.viper.survivalplus.SurvivalPlus;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;

import java.io.File;
import java.io.IOException;

public class ShopManager {

    private final SurvivalPlus plugin;
    private final File shopFile;
    private final FileConfiguration shopConfig;
    
    // Multiplikatoren für dynamische Preise
    private double buyMultiplier;
    private double sellMultiplier;

    public ShopManager(SurvivalPlus plugin) {
        this.plugin = plugin;
        this.shopFile = new File(plugin.getDataFolder(), "shop.yml");
        
        if (!shopFile.exists()) {
            plugin.saveResource("shop.yml", false);
        }
        this.shopConfig = YamlConfiguration.loadConfiguration(shopFile);
        
        // Multiplikatoren laden
        loadMultipliers();
    }
    
    public void loadMultipliers() {
        this.buyMultiplier = plugin.getConfig().getDouble("economy.buy-multiplier", 1.05);
        this.sellMultiplier = plugin.getConfig().getDouble("economy.sell-multiplier", 0.95);
        plugin.getLogger().info("Shop-Multiplikatoren geladen: Buy=" + buyMultiplier + ", Sell=" + sellMultiplier);
    }

    public double getCurrentPrice(String itemKey) {
        return shopConfig.getDouble("items." + itemKey + ".current-price", 0);
    }

    public int getStock(String itemKey) {
        return shopConfig.getInt("items." + itemKey + ".stock", 0);
    }

    /**
     * Spieler kauft Item: Preis steigt
     */
    public boolean buyItem(String itemKey, int amount) {
        int stock = shopConfig.getInt("items." + itemKey + ".stock", 0);
        
        // Fallback auf Base-Price wenn noch keine Transaktion stattfand (sonst wird 1.05 auf 0 gerechnet)
        double currentPrice = shopConfig.getDouble("items." + itemKey + ".current-price", 0);
        if (currentPrice == 0) {
            currentPrice = shopConfig.getDouble("items." + itemKey + ".base-price", 100);
        }
        
        // Lager aktualisieren (optional, oder unendlich)
        // shopConfig.set("items." + itemKey + ".stock", stock - amount);
        
        // Preis erhöhen (Dynamisch)
        double newPrice = currentPrice * buyMultiplier;
        shopConfig.set("items." + itemKey + ".current-price", newPrice);
        
        saveShop();
        return true;
    }

    /**
     * Spieler verkauft Item: Preis sinkt
     */
    public boolean sellItem(String itemKey, int amount) {
        // Fallback auf Base-Price
        double currentPrice = shopConfig.getDouble("items." + itemKey + ".current-price", 0);
        if (currentPrice == 0) {
            currentPrice = shopConfig.getDouble("items." + itemKey + ".base-price", 100);
        }

        // Lager aktualisieren (optional)
        // shopConfig.set("items." + itemKey + ".stock", stock + amount);
        
        // Preis senken (Dynamisch)
        double newPrice = currentPrice * sellMultiplier;
        shopConfig.set("items." + itemKey + ".current-price", newPrice);
        
        saveShop();
        return true;
    }

    public void addOrUpdateItem(String itemKey, double basePrice, int stock) {
        shopConfig.set("items." + itemKey + ".base-price", basePrice);
        shopConfig.set("items." + itemKey + ".stock", stock);
        // Wenn der Preis noch nicht existiert (neues Item), setze ihn auf Base-Preis
        if (!shopConfig.contains("items." + itemKey + ".current-price")) {
            shopConfig.set("items." + itemKey + ".current-price", basePrice);
        }
        saveShop();
    }

    private void saveShop() {
        try {
            shopConfig.save(shopFile);
        } catch (IOException e) {
            plugin.getLogger().severe("Fehler beim Speichern von shop.yml");
            e.printStackTrace();
        }
    }

    public FileConfiguration getShopConfig() {
        return shopConfig;
    }
}