/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package pw.yumc.Residence.economy;

import org.bukkit.ChatColor;
import org.bukkit.entity.Player;

import com.bekvon.bukkit.residence.protection.ClaimedResidence;

import pw.yumc.Residence.ResidenceMain;

/**
 *
 * @author Administrator
 */
public class ResidenceBank {
    ResidenceMain plugin;
    ClaimedResidence res;
    int storedMoney;

    public ResidenceBank(final ResidenceMain plugin, final ClaimedResidence parent) {
        this.plugin = plugin;
        storedMoney = 0;
        res = parent;
    }

    public void add(final int amount) {
        storedMoney = storedMoney + amount;
    }

    public void deposit(final Player player, final int amount, final boolean resadmin) {
        if (!plugin.getConfigManager().enableEconomy()) {
            player.sendMessage(ChatColor.RED + plugin.getLanguage().getPhrase("MarketDisabled"));
        }
        if (!resadmin && !res.getPermissions().playerHas(player.getName(), "bank", false)) {
            player.sendMessage(ChatColor.RED + plugin.getLanguage().getPhrase("NoBankAccess"));
            return;
        }
        if (!plugin.getEconomyManager().canAfford(player.getName(), amount)) {
            player.sendMessage(ChatColor.RED + plugin.getLanguage().getPhrase("NotEnoughMoney"));
            return;
        }
        if (plugin.getEconomyManager().subtract(player.getName(), amount)) {
            this.add(amount);
            player.sendMessage(ChatColor.GREEN + plugin.getLanguage().getPhrase("BankDeposit", ChatColor.YELLOW + String.format("%d", amount) + ChatColor.GREEN));
        }
    }

    public int getStoredMoney() {
        return storedMoney;
    }

    public boolean hasEnough(final int amount) {
        if (storedMoney >= amount) {
            return true;
        }
        return false;
    }

    public void setStoredMoney(final int amount) {
        storedMoney = amount;
    }

    public void subtract(final int amount) {
        storedMoney = storedMoney - amount;
        if (storedMoney < 0) {
            storedMoney = 0;
        }
    }

    public void withdraw(final Player player, final int amount, final boolean resadmin) {
        if (!plugin.getConfigManager().enableEconomy()) {
            player.sendMessage(ChatColor.RED + plugin.getLanguage().getPhrase("MarketDisabled"));
        }
        if (!resadmin && !res.getPermissions().playerHas(player.getName(), "bank", false)) {
            player.sendMessage(ChatColor.RED + plugin.getLanguage().getPhrase("NoBankAccess"));
            return;
        }
        if (!hasEnough(amount)) {
            player.sendMessage(ChatColor.RED + plugin.getLanguage().getPhrase("BankNoMoney"));
            return;
        }
        if (plugin.getEconomyManager().add(player.getName(), amount)) {
            this.subtract(amount);
            player.sendMessage(ChatColor.GREEN + plugin.getLanguage().getPhrase("BankWithdraw", ChatColor.YELLOW + String.format("%d", amount) + ChatColor.GREEN));
        }
    }
}
