package foleon.miningcore;

import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;

import java.io.File;

public class MiningCore extends JavaPlugin implements CommandExecutor {
    private FileConfiguration config;

    @Override
    public void onEnable() {
        File configFile = new File(getDataFolder(), "config.yml");
        config = getConfig();
        if (!configFile.exists()) {
            config.set("balance.default", 0);
            saveConfig();
        }
        getCommand("pay").setExecutor(this);
        getCommand("money").setExecutor(this);
        getCommand("givemoney").setExecutor(this);
    }

    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        if (command.getName().equalsIgnoreCase("pay")) {
            if (!(sender instanceof Player)) {
                sender.sendMessage(ChatColor.RED + "This command can only be executed by players!");
                return true;
            }

            Player player = (Player) sender;

            if (args.length < 2) {
                player.sendMessage(ChatColor.RED + "Usage: /pay <player> <amount>");
                return true;
            }

            Player target = getServer().getPlayer(args[0]);

            if (target == null) {
                player.sendMessage(ChatColor.RED + "Player not found!");
                return true;
            }

            double amount;
            try {
                amount = Double.parseDouble(args[1]);
            } catch (NumberFormatException e) {
                player.sendMessage(ChatColor.RED + "Invalid amount!");
                return true;
            }

            double balance = getBalance(player);
            if (balance < amount) {
                player.sendMessage(ChatColor.RED + "Insufficient funds!");
                return true;
            }

            // Transfer the money
            modifyBalance(player, -amount);
            modifyBalance(target, amount);

            player.sendMessage(ChatColor.GREEN + String.format("You paid %s $%.2f", target.getName(), amount));
            target.sendMessage(ChatColor.GREEN + String.format("%s paid you $%.2f", player.getName(), amount));

            return true;
        } else if (command.getName().equalsIgnoreCase("money")) {
            if (!(sender instanceof Player)) {
                sender.sendMessage(ChatColor.RED + "This command can only be executed by players!");
                return true;
            }

            Player player = (Player) sender;
            double balance = getBalance(player);
            player.sendMessage(ChatColor.YELLOW + String.format("Your balance: $%.2f", balance));

            return true;
        } else if (command.getName().equalsIgnoreCase("givemoney")) {
            if (!sender.hasPermission("miningcore.admin")) {
                sender.sendMessage(ChatColor.RED + "You do not have permission to use this command!");
                return true;
            }

            if (args.length < 2) {
                sender.sendMessage(ChatColor.RED + "Usage: /givemoney <player> <amount>");
                return true;
            }

            Player target = getServer().getPlayer(args[0]);

            if (target == null) {
                sender.sendMessage(ChatColor.RED + "Player not found!");
                return true;
            }

            double amount;
            try {
                amount = Double.parseDouble(args[1]);
            } catch (NumberFormatException e) {
                sender.sendMessage(ChatColor.RED + "Invalid amount!");
                return true;
            }

            modifyBalance(target, amount);
            sender.sendMessage(ChatColor.GREEN + String.format("You gave %s $%.2f", target.getName(), amount));

            return true;
        }

        return false;
    }

    private void modifyBalance(Player player, double amount) {
        String uuid = player.getUniqueId().toString();
        double balance = getBalance(player) + amount;
        config.set("balance." + uuid, balance);
        saveConfig();
    }

    private double getBalance(Player player) {
        String uuid = player.getUniqueId().toString();
        return config.getDouble("balance." + uuid);
    }
} rfr vj;yj htfkbpjdfnm d 'njv gkfubyt rjvfyle .ещз rjnjhfz gjrf;tn cgbcjr njg buhjrjd gj ,fkfyce&