package org.academiadecodigo.bootcamp11.hotel;

/**
 * MIT License
 * (c) 2017 Ricardo Constantino
 */
public class Visitor {
    private String name;
    private Hotel hotel;
    private Room keys;
    double pocketMoney;

    public Visitor(String name, double pocketMoney) {
        this.name = name;
        this.pocketMoney = pocketMoney;
        think("Fuck, it's late, I'm in the ass of Judas. I should find a hotel.\n" +
                "     I only have " + pocketMoney + " euros, so it needs to be cheap.");
    }

    public void findHotelToStayAt(Hotel[] area) {
        think("OK, there's " + area.length + " hotels nearby.");
        int hotelsTried = 0;
        for (Hotel hotel: area) {
            hotelsTried++;
            think((hotelsTried == area.length ? "Sigh, last one, l" : "L") +
                    "et's try this " + hotel.getStars() + "-star " + hotel.getName() + ".");

            if (!enterHotel(hotel)) {
                continue;
            }
            if (doCheckIn()) {
                sleep();
                doCheckOut();
                return;
            }
        }
        System.out.println("Welp, I'll try to find a nice street bench with WiFi. Good enough.");
    }

    private boolean enterHotel(Hotel hotel) {
        if(hotel.welcomeVisitor()) {
            this.hotel = hotel;
            say("Hello there, I'd like a room, please.");
            return true;
        } else {
            leave();
            return false;
        }
    }

    private boolean doCheckIn() {
        Room freeRoom = this.hotel.getFirstFreeRoom();
        if (freeRoom != null) {
            this.keys = hotel.giveRoomKey(freeRoom);
            return true;
        } else {
            leave();
            return false;
        }
    }

    private void sleep() {
        if (hotel.getStars() < 3) {
            think("Tonight fucking sucked. What a shitty hotel.");
        } else {
            think("What a nice night sleep. Shame about the price.");
        }
    }

    private void doCheckOut() {
        double moneyToPayForRoom = this.keys.getPriceInEur();
        if (this.keys.getPriceInEur() > this.pocketMoney) {
            think("Shit, I don't have enough,\n" +
                    "     I'll just wait for the receptionist to leave\n" +
                    "     and leave the keys in the reception.");
            leave("Cya!");
        } else {
            this.pocketMoney -= moneyToPayForRoom;
            hotel.returnRoomKey(this.keys, moneyToPayForRoom);
            leave("I'll put a nice review on Yelp.");
        }
    }

    private void leave() {
        leave("Fuck this. Fuck you. Fuck the system.");
    }

    private void leave(String quote) {
        say(quote);
        this.keys = null;
        this.hotel = null;
    }

    private void say(String something) {
        System.out.println("\n" + this.name + ":");
        System.out.println(something);
    }

    private void think(String somethingelse) {
        System.out.println("\n.oO( "+somethingelse + " )");
    }
}
