package org.academiadecodigo.bootcamp11.moneyinthebank;

/**
 * MIT License
 * (c) 2017 Ricardo Constantino
 */
public class PiggyBank {
    private long moneyInCents;
    private String name;

    public PiggyBank() {
        this.name = "Piggy-bank";
        this.moneyInCents = 0L;
    }

    public PiggyBank(double money) {
        this.name = "Piggy-bank";
        this.moneyInCents = eurToCents(money);
    }

    public PiggyBank(String name, double money) {
        this.name = name;
        this.moneyInCents = eurToCents(money);
    }

    public double checkBalance() {
        return centsToEur(this.moneyInCents);
    }

    public double addToBank(double moneyToChange) {
        if (moneyToChange < 0) {
            moneyToChange = Math.min(Math.abs(moneyToChange), centsToEur(this.moneyInCents));
            System.out.printf("> withdrawing €%.2f from %s\n",
                    moneyToChange, this.name);
            moneyToChange *= -1;
        } else {
            System.out.printf("> depositing €%.2f in %s\n",
                    moneyToChange, this.name);
        }
        this.moneyInCents += eurToCents(moneyToChange);
        return moneyToChange;
    }

    public double giefMonies(double moneyToSpend) {
        return Math.abs(addToBank(moneyToSpend * -1));
    }

    private static long eurToCents(double euros) {
        // bank rounding ;D
        double cents = Math.floor(euros * 100);
        return Math.round(cents);
    }

    private static double centsToEur(long cents) {
        return cents / 100.0;
    }
}
