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;

    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);
    }

    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: Lager sinkt, Preis steigt (Verknappung)
     */
    public boolean buyItem(String itemKey, int amount) {
        int stock = shopConfig.getInt("items." + itemKey + ".stock", 0);
        
        if (stock < amount) {
            return false;
        }
        
        double price = shopConfig.getDouble("items." + itemKey + ".current-price", 0);
        
        // Lager aktualisieren
        shopConfig.set("items." + itemKey + ".stock", stock - amount);
        
        // Preis erhöhen (Faktor 1.05 = +5%)
        double newPrice = price * 1.05;
        shopConfig.set("items." + itemKey + ".current-price", newPrice);
        
        saveShop();
        return true;
    }

    /**
     * Spieler verkauft Item: Lager steigt, Preis sinkt (Überfluss)
     */
    public boolean sellItem(String itemKey, int amount) {
        // Hier könnte man prüfen, ob der Shop genügend Geld hat (falls nötig)
        
        double price = shopConfig.getDouble("items." + itemKey + ".current-price", 0);
        int stock = shopConfig.getInt("items." + itemKey + ".stock", 0);
        
        // Lager aktualisieren
        shopConfig.set("items." + itemKey + ".stock", stock + amount);
        
        // Preis senken (Faktor 0.95 = -5%)
        double newPrice = price * 0.95;
        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);
        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;
    }
}