package com.bitnix.dirtcryptostore.manager;

import com.bitnix.dirtcryptostore.DirtCryptoStorePlugin;
import com.bitnix.dirtcryptostore.model.Product;
import com.bitnix.dirtcryptostore.model.WalletConfig;
import org.bukkit.configuration.ConfigurationSection;

import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;

public class ConfigManager {

    private final DirtCryptoStorePlugin plugin;
    private final Map<String, Product> products = new LinkedHashMap<>();
    private final Map<String, WalletConfig> wallets = new LinkedHashMap<>();

    public ConfigManager(DirtCryptoStorePlugin plugin) {
        this.plugin = plugin;
    }

    public void load() {
        products.clear();
        wallets.clear();
        loadWallets();
        loadProducts();
    }

    private void loadWallets() {
        ConfigurationSection section = plugin.getConfig().getConfigurationSection("wallets");
        if (section == null) {
            return;
        }

        for (String key : section.getKeys(false)) {
            ConfigurationSection walletSection = section.getConfigurationSection(key);
            if (walletSection == null) {
                continue;
            }

            WalletConfig wallet = new WalletConfig(
                    key,
                    walletSection.getBoolean("enabled", true),
                    walletSection.getString("display-name", key),
                    walletSection.getString("coin", "BTC"),
                    walletSection.getString("address", ""),
                    walletSection.getString("explorer-type", "BTC"),
                    walletSection.getInt("min-confirmations", 2),
                    walletSection.getStringList("allow-products")
            );

            wallets.put(key.toLowerCase(), wallet);
        }
    }

    private void loadProducts() {
        ConfigurationSection section = plugin.getConfig().getConfigurationSection("products");
        if (section == null) {
            return;
        }

        for (String key : section.getKeys(false)) {
            ConfigurationSection productSection = section.getConfigurationSection(key);
            if (productSection == null) {
                continue;
            }

            Product product = new Product(
                    key,
                    productSection.getBoolean("enabled", true),
                    productSection.getString("display-name", key),
                    productSection.getString("material", "STONE"),
                    productSection.getInt("custom-model-data", 0),
                    productSection.getBoolean("glow", false),
                    productSection.getInt("slot", -1),
                    productSection.getString("wallet", "default"),
                    productSection.getDouble("price-usd", 0.0D),
                    productSection.getLong("base-sats", 0L),
                    productSection.getString("permission-required", ""),
                    productSection.getBoolean("purchasable-once", false),
                    productSection.getStringList("commands"),
                    productSection.getStringList("lore")
            );

            products.put(key.toLowerCase(), product);
        }
    }

    public Collection<Product> getProducts() {
        return Collections.unmodifiableCollection(products.values());
    }

    public Product getProduct(String id) {
        if (id == null) {
            return null;
        }
        return products.get(id.toLowerCase());
    }

    public WalletConfig getWallet(String id) {
        if (id == null) {
            return null;
        }
        return wallets.get(id.toLowerCase());
    }

    public boolean isWalletAllowedForProduct(WalletConfig wallet, String productId) {
        if (wallet == null || productId == null) {
            return false;
        }

        if (wallet.getAllowProducts().isEmpty()) {
            return true;
        }

        for (String allowed : wallet.getAllowProducts()) {
            if (allowed.equalsIgnoreCase(productId)) {
                return true;
            }
        }

        return false;
    }
}
