package org.academiadecodigo.bootcamp11.arabiannights;

import org.academiadecodigo.bootcamp11.arabiannights.genies.Genie;
import org.academiadecodigo.bootcamp11.arabiannights.genies.GrumpyGenie;
import org.academiadecodigo.bootcamp11.arabiannights.genies.HappyGenie;
import org.academiadecodigo.bootcamp11.arabiannights.genies.RecyclableDemon;

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

public class MagicLamp {
    private final int MAXGENIES;
    private final int MAXWISHESPERGENIE;

    private int geniesReleased;
    private int timesRecycled;
    private Genie[] genies;

    public MagicLamp(int maxGenies) {
        this.timesRecycled = 0;
        this.MAXGENIES = maxGenies;
        this.MAXWISHESPERGENIE = Utils.getRandomInt(1, 4);
        fillLamp();
    }

    public int getMaxGenies() {
        return this.MAXGENIES;
    }

    public int getRemainingGenies() {
        return this.MAXGENIES - this.geniesReleased;
    }

    public int getTimesRecycled() {
        return this.timesRecycled;
    }

    private void fillLamp() {
        this.geniesReleased = 0;
        this.genies = new Genie[this.MAXGENIES + 1];
        for (int i = 0; i < this.MAXGENIES; i++) {
            genies[i] = (i % 2 == 0) ?
                        new HappyGenie(this.MAXWISHESPERGENIE) :
                        new GrumpyGenie(this.MAXWISHESPERGENIE);
        }
        genies[this.MAXGENIES] = new RecyclableDemon();
    }

    public Genie rub() {
        if (this.geniesReleased <= this.MAXGENIES) {
            Genie newGenie = genies[this.geniesReleased++];
            newGenie.introduction();
            return newGenie;
        } else {
            System.out.println("<nothing happens>");
            return null;
        }
    }

    public void suckGenie(Genie genie) {
        if (genie instanceof RecyclableDemon) {
            RecyclableDemon demon = (RecyclableDemon) genie;
            if (demon.isRecyclable()) {
                System.out.println("Lamp: Recyclable Demon accepted, resetting.");
                demon.useToRecycle();
                this.timesRecycled++;
                fillLamp();
                System.out.println("Lamp: You can rub me again, kind sir.\n");
            } else {
                System.out.println("Lamp: This demon has been used already (for some reason you still have it??)\n");
            }
        } else {
            System.out.println("Lamp: This isn't a recyclable demon.\n");
        }
    }

    @Override
    public boolean equals(Object other) {
        if (!(other instanceof MagicLamp)) {
            return false;
        }
        MagicLamp otherLamp = (MagicLamp) other;

        if (getMaxGenies() != otherLamp.getMaxGenies()) {
            System.out.println("capacity is different");
        } else if (getRemainingGenies() != otherLamp.getRemainingGenies()) {
            System.out.println("remaining genies is different");
        } else if (getTimesRecycled() != otherLamp.getTimesRecycled()) {
            System.out.println("number of recycles is different");
        } else {
            System.out.println("they're the same");
        }
        return (this.getMaxGenies() == otherLamp.getMaxGenies() &&
                this.getRemainingGenies() == otherLamp.getRemainingGenies() &&
                this.getTimesRecycled() == otherLamp.getTimesRecycled());
    }
}
