package org.academiadecodigo.bootcamp11.arabiannights;

import org.academiadecodigo.bootcamp11.arabiannights.genies.Genie;

/**
 * MIT License
 * (c) 2017 Ricardo Constantino
 */

public class Alladin {
    private String name;
    private int lampsRubbed;
    private MagicLamp lamp;
    private MagicLamp[] lamps;
    private Genie currentGenie;
    private String[] wishes = {
            "a nice girl",
            "a nice car",
            "a nice PC",
            "a nice GPU",
            "lotsa money",
            "a mighty steed",
            "a big \"sword\"",
            "world peace",
            "obamacare"
    };
    private boolean[] wishesUsed = new boolean[wishes.length];

    public Alladin(String name) {
        this.name = name;
        this.lampsRubbed = 0;

        // set that no wish has been used yet
        for (boolean wish: this.wishesUsed) {
            wish = false;
        }
    }

    public void introduction(MagicLamp[] lamps) {
        if (lamps.length > 0) {
            say("Hey, I found " + lamps.length + " magic lamps!");
            this.lamps = lamps;
            compareLamps();
        } else {
            say("There's fuck all to do here.");
            System.exit(0);
        }
    }

    public void rubOneOut() {
        if (this.lamp == null && lampsRubbed < this.lamps.length) {
            this.lamp = lamps[lampsRubbed++];
        } else if (this.lamp == null) {
            say("There's no lamps to rub.");
        }

        if (this.lamp != null) {
            say("Let's rub one out.");
            this.currentGenie = this.lamp.rub();
        }
    }

    public void throwLampAway() {
        say("I'm sick of this lamp. Let's grab another one.");
        this.lamp = null;
        this.currentGenie = null;
    }

    public void makeAwish() {
        if (this.currentGenie != null) {
            int wishNumber = Utils.getRandomInt(0, this.wishes.length);
            while (wishWasUsed(wishNumber)) {
                wishNumber = Utils.getRandomInt(0, this.wishes.length);
            }
            say("I wish I had " + wishes[wishNumber] + ".");
            int beforeWish = this.currentGenie.getWishesLeft();
            this.currentGenie.grantWish(wishes[wishNumber]);
            if (beforeWish != this.currentGenie.getWishesLeft()) {
                // wish was granted
                wishesUsed[wishNumber] = true;
            }
        } else {
            say("There's no genie to ask a wish to.");
        }
    }

    public void pushGenieIntoLamp() {
        if (this.currentGenie != null) {
            say(String.format("I push the %s back into the lamp.", this.currentGenie.getGenieType()));
            this.lamp.suckGenie(this.currentGenie);
        } else {
            say("There's no genie to push into the lamp.");
        }
    }

    public void compareLamps() {
        System.out.print("Let's check if this lamp and the next have the same characteristics: ");
        if (this.lamps.length > this.lampsRubbed + 1) {
            lamps[this.lampsRubbed].equals(lamps[this.lampsRubbed+1]);
        } else {
            System.out.println("(there's no next lamp)\n");
        }
    }

    private boolean wishWasUsed(int wish) {
        return wishesUsed[wish];
    }

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