package com.Acrobot.ChestShop.Economy;

import com.Acrobot.Breeze.Utils.NumberUtil;
import com.Acrobot.ChestShop.Configuration.Properties;
import com.Acrobot.ChestShop.Signs.ChestShopSign;
import com.Acrobot.ChestShop.Utils.uName;
import org.bukkit.inventory.Inventory;

import static com.Acrobot.Breeze.Utils.NumberUtil.roundUp;

/**
 * @author Acrobot
 *         Economy management
 */
public class Economy {
    private static EconomyManager manager = new EconomyManager();

    public static boolean transactionCanFail() {
        return manager.transactionCanFail();
    }

    public static boolean isOwnerEconomicallyActive(Inventory inventory) {
        return !ChestShopSign.isAdminShop(inventory) || !getServerAccountName().isEmpty();
    }

    public static boolean hasAccount(String player) {
        return !player.isEmpty() && manager.hasAccount(uName.getName(player));
    }

    public static String getServerAccountName() {
        return Properties.SERVER_ECONOMY_ACCOUNT;
    }

    public static boolean isServerAccount(String acc) {
        return ChestShopSign.isAdminShop(acc);
    }

    public static boolean add(String name, double amount) {
        if (isServerAccount(name) && !getServerAccountName().isEmpty()) {
            name = getServerAccountName();
        }

        float taxAmount = isServerAccount(name) ? Properties.SERVER_TAX_AMOUNT : Properties.TAX_AMOUNT;

        double tax = getTax(taxAmount, amount);
        if (tax != 0) {
            if (!getServerAccountName().isEmpty()) {
                manager.add(getServerAccountName(), tax);
            }
            amount -= tax;
        }

        return manager.add(uName.getName(name), amount);
    }

    public static double getTax(float tax, double price) {
        return NumberUtil.roundDown((tax / 100F) * price);
    }

    public static boolean subtract(String name, double amount) {
        if (isServerAccount(name) && !getServerAccountName().isEmpty()) {
            name = getServerAccountName();
        }

        return manager.subtract(uName.getName(name), roundUp(amount));
    }

    public static boolean hasEnough(String name, double amount) {
        if (amount <= 0) {
            return true;
        }

        if (isServerAccount(name) && !getServerAccountName().isEmpty()) {
            name = getServerAccountName();
        }

        return manager.hasEnough(uName.getName(name), roundUp(amount));
    }

    public static double getBalance(String name) {
        if (isServerAccount(name) && !getServerAccountName().isEmpty()) {
            name = getServerAccountName();
        }

        return manager.balance(uName.getName(name));
    }

    public static String formatBalance(double amount) {
        return manager.format(roundUp(amount));
    }

    public static void setPlugin(EconomyManager plugin) {
        manager = plugin;
    }

    public static EconomyManager getManager() {
        return manager;
    }

    public static boolean isLoaded() {
        return manager.getClass() != EconomyManager.class;
    }
}

@@ -1187,36 +1187,52 @@
 verAccount(name)
- && 
+) %7B%0A            if (
 !getServerAccoun
@@ -1256,32 +1256,36 @@
 ) %7B%0A            
+  
+  
 name = getServer
@@ -1291,32 +1291,96 @@
 rAccountName();%0A
+            %7D else %7B%0A                return true;%0A            %7D%0A
         %7D%0A%0A     
@@ -1993,36 +1993,52 @@
 verAccount(name)
- && 
+) %7B%0A            if (
 !getServerAccoun
@@ -2062,32 +2062,36 @@
 ) %7B%0A            
+  
+  
 name = getServer
@@ -2097,32 +2097,96 @@
 rAccountName();%0A
+            %7D else %7B%0A                return true;%0A            %7D%0A
         %7D%0A%0A     
@@ -2412,36 +2412,52 @@
 verAccount(name)
- && 
+) %7B%0A            if (
 !getServerAccoun
@@ -2481,32 +2481,36 @@
 ) %7B%0A            
+  
+  
 name = getServer
@@ -2516,32 +2516,96 @@
 rAccountName();%0A
+            %7D else %7B%0A                return true;%0A            %7D%0A
         %7D%0A%0A     
@@ -2762,20 +2762,36 @@
 nt(name)
- && 
+) %7B%0A            if (
 !getServ
@@ -2823,32 +2823,36 @@
 ) %7B%0A            
+    
 name = getServer
@@ -2858,32 +2858,108 @@
 rAccountName();%0A
+            %7D else %7B%0A                return Double.MAX_VALUE;%0A            %7D%0A
         %7D%0A%0A     

Fix economy plugins