package com.zivilon.cinder_loe.carriage;

import lotr.common.LOTRMod;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;

public class CarriageUtils {

    public static boolean hasEnoughCoins(EntityPlayer player, int cost) {
        int total = 0;
        for (ItemStack stack : player.inventory.mainInventory) {
            if (stack != null && stack.getItem() == LOTRMod.silverCoin) {
                int value = getCoinValue(stack);
                total += value * stack.stackSize;
                if (total >= cost) {
                    return true;
                }
            }
        }
        return total >= cost;
    }

    public static boolean removeCoins(EntityPlayer player, int cost) {
        int total = 0;
        int[] counts = new int[3]; // 0 = $1, 1 = $10, 2 = $100

        // Step 1: Count total money and track counts
        for (ItemStack stack : player.inventory.mainInventory) {
            if (stack != null && stack.getItem() == LOTRMod.silverCoin) {
                int value = getCoinValue(stack);
                total += value * stack.stackSize;
                counts[stack.getItemDamage()] += stack.stackSize;
            }
        }

        if (total < cost) {
            return false; // Not enough money
        }

        // Step 2: Remove only coin stacks
        for (int i = 0; i < player.inventory.mainInventory.length; i++) {
            ItemStack stack = player.inventory.mainInventory[i];
            if (stack != null && stack.getItem() == LOTRMod.silverCoin) {
                player.inventory.setInventorySlotContents(i, null);
            }
        }

        // Step 3: Calculate remaining change
        int change = total - cost;

        // Step 4: Give back change as biggest coins first
        int[] coinValues = {100, 10, 1};
        for (int i = 0; i < coinValues.length; i++) {
            int value = coinValues[i];
            int count = change / value;
            change %= value;

            while (count > 0) {
                int stackSize = Math.min(count, LOTRMod.silverCoin.getItemStackLimit());
                ItemStack changeStack = new ItemStack(LOTRMod.silverCoin, stackSize, i);
                player.inventory.addItemStackToInventory(changeStack);
                count -= stackSize;
            }
        }

        player.inventory.markDirty();
        return true;
    }



    private static int getCoinValue(ItemStack stack) {
        int damage = stack.getItemDamage();
        switch (damage) {
            case 0: return 1;   // 1$
            case 1: return 10;  // 10$
            case 2: return 100; // 100$
            default: return 0;
        }
    }

    public static int calculateCost(double playerX, double playerZ, CarriageDestination dest) {
        if (dest.road.equals("sea")) {
            double dist = Math.sqrt(Math.pow(dest.x - playerX, 2) + Math.pow(dest.z - playerZ, 2));
            int cost = 500 + Math.max(1, (int)(dist / 50)); // 50 is the cost per block + 500 base cost
            return cost;
        }
        double dist = Math.sqrt(Math.pow(dest.x - playerX, 2) + Math.pow(dest.z - playerZ, 2));
        int cost = Math.max(1, (int)(dist / 50)); // 50 is the cost per block, with base cost of 1
        return cost;
    }

}
