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

package pw.yumc.Residence.vaultinterface;

import org.bukkit.Server;
import org.bukkit.entity.Player;
import org.bukkit.plugin.RegisteredServiceProvider;

import com.bekvon.bukkit.residence.permissions.PermissionsInterface;

import net.milkbowl.vault.chat.Chat;
import net.milkbowl.vault.economy.Economy;
import net.milkbowl.vault.permission.Permission;
import pw.yumc.Residence.economy.EconomyInterface;

/**
 *
 * @author Administrator
 */
public class ResidenceVaultAdapter implements EconomyInterface, PermissionsInterface {

    public static Permission permissions = null;
    public static Economy economy = null;
    public static Chat chat = null;

    public ResidenceVaultAdapter(final Server s) {
        this.setupPermissions(s);
        this.setupEconomy(s);
        this.setupChat(s);
    }

    @SuppressWarnings("deprecation")
    @Override
    public boolean add(final String playerName, final double amount) {
        return economy.depositPlayer(playerName, amount).transactionSuccess();
    }

    @SuppressWarnings("deprecation")
    @Override
    public boolean canAfford(final String playerName, final double amount) {
        return economy.has(playerName, amount);
    }

    public boolean chatOK() {
        return chat != null;
    }

    public boolean economyOK() {
        return economy != null;
    }

    @SuppressWarnings("deprecation")
    @Override
    public double getBalance(final String playerName) {
        return economy.getBalance(playerName);
    }

    public String getChatName() {
        if (chat != null)
            return chat.getName();
        return "";
    }

    public String getEconomyName() {
        if (economy != null)
            return economy.getName();
        return "";
    }

    @Override
    public String getName() {
        return "Vault";
    }

    public String getPermissionsName() {
        if (permissions != null)
            return permissions.getName();
        return "";
    }

    @Override
    public String getPlayerGroup(final Player player) {
        final String group = permissions.getPrimaryGroup(player).toLowerCase();
        if (group == null)
            return group;
        return group.toLowerCase();
    }

    @Override
    @SuppressWarnings("deprecation")
    public String getPlayerGroup(final String player, final String world) {
        final String group = permissions.getPrimaryGroup(world, player);
        if (group == null)
            return group;
        return group.toLowerCase();
    }

    public boolean permissionsOK() {
        if (permissions != null && !permissions.getName().equalsIgnoreCase("SuperPerms"))
            return true;
        return false;
    }

    @SuppressWarnings("deprecation")
    @Override
    public boolean subtract(final String playerName, final double amount) {
        return economy.withdrawPlayer(playerName, amount).transactionSuccess();
    }

    @SuppressWarnings("deprecation")
    @Override
    public boolean transfer(final String playerFrom, final String playerTo, final double amount) {
        if (economy.withdrawPlayer(playerFrom, amount).transactionSuccess()) {
            if (economy.depositPlayer(playerTo, amount).transactionSuccess())
                return true;
            economy.depositPlayer(playerFrom, amount);
        }
        return false;
    }

    private boolean setupChat(final Server s) {
        final RegisteredServiceProvider<Chat> chatProvider = s.getServicesManager().getRegistration(net.milkbowl.vault.chat.Chat.class);
        if (chatProvider != null) {
            chat = chatProvider.getProvider();
        }
        return (chat != null);
    }

    private boolean setupEconomy(final Server s) {
        final RegisteredServiceProvider<Economy> economyProvider = s.getServicesManager().getRegistration(net.milkbowl.vault.economy.Economy.class);
        if (economyProvider != null) {
            economy = economyProvider.getProvider();
        }
        return (economy != null);
    }

    private boolean setupPermissions(final Server s) {
        final RegisteredServiceProvider<Permission> permissionProvider = s.getServicesManager().getRegistration(net.milkbowl.vault.permission.Permission.class);
        if (permissionProvider != null) {
            permissions = permissionProvider.getProvider();
        }
        return (permissions != null);
    }

}