package org.academiadecodigo.bootcamp11.moneyinthebank;

/**
 * MIT License
 * (c) 2017 Ricardo Constantino
 */
public class Main {
    public static void main(String[] args) {
        double miniCost = 0.9;
        double initialWalletMoney = 3;
        double initialBankMoney = 5;
        String[] friendsToBuyBeersTo = {"Ricardo", "Drake", "Daniel", "Luís", "Aires"};

        int minisToDrinkLater = friendsToBuyBeersTo.length + 1;
        Wallet wallet = new Wallet(initialWalletMoney);
        PiggyBank piggy = new PiggyBank(initialBankMoney);
        Person ivan = new Person("Ivan", wallet, piggy);

        System.out.printf("\"I'm %s, and this is how I met your mother.\"\n",
                ivan.getName());
        System.out.println("\nLet's look at our wallet.");
        double walletBalance = ivan.checkWallet(true);

        if (minisToDrinkLater > 0) {
            System.out.print("I want to get some beers later,");
            if (walletBalance > miniCost * minisToDrinkLater) {
                System.out.print(" but I don't need this much in the wallet;");
                System.out.println(" we can place the rest in the piggy bank.");
            } else {
                System.out.println(" but I don't have enough right now.");
            }
        }

        System.out.println("\nLet's check our piggy-bank.");
        double piggyBalance = ivan.checkMoneyInTheBank(true);

        if (minisToDrinkLater > 0 &&
                walletBalance >= Math.ceil(miniCost * minisToDrinkLater)) {
            System.out.printf("The beers are €%.2f each, but I'll need ", miniCost);
            System.out.printf("€%.2f if I come with friends.\n",
                    miniCost*minisToDrinkLater);

            double moneyToSave = walletBalance - Math.ceil(miniCost * minisToDrinkLater);
            System.out.printf("We can place the €%.2f we don't need in the piggy.\n",
                    moneyToSave);
            ivan.depositMoney(moneyToSave);
        } else if (minisToDrinkLater > 0 &&
                walletBalance + piggyBalance >= miniCost * minisToDrinkLater) {
            System.out.println("I don't have enough money, but I do have it in the piggy.");
            double moneyToWithdraw = miniCost * minisToDrinkLater - walletBalance;
            ivan.withdrawMoney(moneyToWithdraw);
        } else {
            if (!(minisToDrinkLater > 0 && ivan.cry())) {
                System.out.println("\nI don't need beer, let's just play some DoTA.");
            }
            System.exit(0);
        }

        piggyBalance = ivan.checkMoneyInTheBank();
        walletBalance = ivan.checkWallet();

        System.out.println("\n================\n");
        System.out.println(  "-- at the bar --");
        if (friendsToBuyBeersTo.length == 1) {
            System.out.printf("I brought my friend %s, I have to buy them a beer too.\n",
                    friendsToBuyBeersTo[0]);
        } else if (minisToDrinkLater == 1) {
            System.out.println("I worked like a slave today, I deserve a nice cold one.");
        } else {
            System.out.printf("I brought my %d friends, %s, with me, ",
                    friendsToBuyBeersTo.length,
                    prettyPrint(friendsToBuyBeersTo));
            System.out.println("I have to buy them a beer too!");
        }


        ivan.buyBeers(minisToDrinkLater, miniCost);
        System.out.println("\n-- 2 sips later --");
        System.out.println("\"Are there any vegan beers? I don't like this one so much.\"");
    }

    private static String prettyPrint(String[] strings) {
        String result = strings[0];
        for (int i = 1; i < strings.length-1; i++) {
            result += ", " + strings[i];
        }
        return result + " and " + strings[strings.length - 1];
    }
}
