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

package pw.yumc.Residence.economy;

import com.earth2me.essentials.Essentials;
import com.earth2me.essentials.api.Economy;
import com.earth2me.essentials.api.NoLoanPermittedException;
import com.earth2me.essentials.api.UserDoesNotExistException;

/**
 *
 * @author Administrator
 */
public class EssentialsEcoAdapter implements EconomyInterface {

    Essentials plugin;

    public EssentialsEcoAdapter(final Essentials p) {
        plugin = p;
        final String serverland = "Server Land";
        if (!Economy.playerExists(serverland)) {
            Economy.createNPC(serverland);
        }
    }

    @Override
    public boolean add(final String playerName, final double amount) {
        if (Economy.playerExists(playerName)) {
            try {
                Economy.add(playerName, amount);
                return true;
            } catch (final UserDoesNotExistException ex) {
                return false;
            } catch (final NoLoanPermittedException ex) {
                return false;
            }
        }
        return false;
    }

    @Override
    public boolean canAfford(final String playerName, final double amount) {
        try {
            if (Economy.playerExists(playerName)) {
                return Economy.hasEnough(playerName, amount);
            }
            return false;
        } catch (final UserDoesNotExistException ex) {
            return false;
        }
    }

    @Override
    public double getBalance(final String playerName) {
        try {
            if (Economy.playerExists(playerName)) {
                return Economy.getMoney(playerName);
            }
        } catch (final UserDoesNotExistException ex) {
        }
        return 0;
    }

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

    @Override
    public boolean subtract(final String playerName, final double amount) {
        if (Economy.playerExists(playerName)) {
            try {
                Economy.subtract(playerName, amount);
                return true;
            } catch (final UserDoesNotExistException ex) {
                return false;
            } catch (final NoLoanPermittedException ex) {
                return false;
            }
        }
        return false;
    }

    @Override
    public boolean transfer(final String playerFrom, final String playerTo, final double amount) {
        try {
            if (Economy.playerExists(playerFrom) && Economy.playerExists(playerTo) && Economy.hasEnough(playerFrom, amount)) {
                if (!subtract(playerFrom, amount))
                    return false;
                if (!add(playerTo, amount)) {
                    add(playerFrom, amount);
                    return false;
                }
                return true;
            }
        } catch (final UserDoesNotExistException ex) {
            return false;
        }
        return false;
    }

}
