/*
 * This file is part of helper, licensed under the MIT License.
 *
 *  Copyright (c) lucko (Luck) <luck@lucko.me>
 *  Copyright (c) contributors
 *
 *  Permission is hereby granted, free of charge, to any person obtaining a copy
 *  of this software and associated documentation files (the "Software"), to deal
 *  in the Software without restriction, including without limitation the rights
 *  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 *  copies of the Software, and to permit persons to whom the Software is
 *  furnished to do so, subject to the following conditions:
 *
 *  The above copyright notice and this permission notice shall be included in all
 *  copies or substantial portions of the Software.
 *
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 *  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 *  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 *  SOFTWARE.
 */

package me.lucko.helper.menu.scheme;

import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;

import me.lucko.helper.menu.Gui;
import me.lucko.helper.menu.Item;
import me.lucko.helper.menu.Slot;
import me.lucko.helper.utils.annotation.NonnullByDefault;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

/**
 * A utility to help place items into a {@link Gui}
 */
@NonnullByDefault
public class MenuPopulator {

    private final Gui gui;
    private final ImmutableList<Integer> slots;
    private List<Integer> remainingSlots;

    public MenuPopulator(Gui gui, MenuScheme scheme) {
        Preconditions.checkNotNull(gui, "gui");
        Preconditions.checkNotNull(scheme, "scheme");

        this.remainingSlots = scheme.getMaskedIndexes();
        Preconditions.checkArgument(this.remainingSlots.size() > 0, "no slots in scheme");

        this.gui = gui;
        this.slots = ImmutableList.copyOf(this.remainingSlots);
    }

    public MenuPopulator(Gui gui, List<Integer> slots) {
        Preconditions.checkNotNull(gui, "gui");
        Preconditions.checkNotNull(slots, "slots");

        Preconditions.checkArgument(slots.size() > 0, "no slots in list");

        this.gui = gui;
        this.slots = ImmutableList.copyOf(slots);
        this.remainingSlots = new ArrayList<>(this.slots);
    }

    private MenuPopulator(MenuPopulator other) {
        this.gui = other.gui;
        this.slots = other.slots;
        this.remainingSlots = new ArrayList<>(this.slots);
    }

    /**
     * Gets an immutable copy of the slots used by this populator.
     *
     * @return the slots used by this populator.
     */
    public ImmutableList<Integer> getSlots() {
        return this.slots;
    }

    /**
     * Resets the slot order used by this populator to the state it was in upon construction
     */
    public void reset() {
        this.remainingSlots = new ArrayList<>(this.slots);
    }

    public MenuPopulator consume(Consumer<Slot> action) {
        if (tryConsume(action)) {
            return this;
        } else {
            throw new IllegalStateException("No more slots");
        }
    }

    public MenuPopulator consumeIfSpace(Consumer<Slot> action) {
        tryConsume(action);
        return this;
    }

    public boolean tryConsume(Consumer<Slot> action) {
        Preconditions.checkNotNull(action, "action");
        if (remainingSlots.size() == 0) {
            return false;
        }

        int slot = remainingSlots.remove(0);
        action.accept(gui.getSlot(slot));
        return true;
    }

    /**
     * Places an item onto the {@link Gui} using the next available slot in the populator
     *
     * @param item the item to place
     * @return the populator
     * @throws IllegalStateException if there are not more slots
     */
    public MenuPopulator accept(Item item) {
        return consume(s -> s.applyFromItem(item));
    }

    /**
     * Places an item onto the {@link Gui} using the next available slot in the populator
     *
     * @param item the item to place
     * @return the populator
     */
    public MenuPopulator acceptIfSpace(Item item) {
        return consumeIfSpace(s -> s.applyFromItem(item));
    }

    /**
     * Places an item onto the {@link Gui} using the next available slot in the populator
     *
     * @param item the item to place
     * @return true if there was a slot left in the populator to place this item onto, false otherwise
     */
    public boolean placeIfSpace(Item item) {
        return tryConsume(s -> s.applyFromItem(item));
    }

    /**
     * Gets the number of remaining slots in the populator.
     *
     * @return the number of remaining slots
     */
    public int getRemainingSpace() {
        return remainingSlots.size();
    }

    /**
     * Gets if there is any space left in this populator
     *
     * @return if there is more space
     */
    public boolean hasSpace() {
        return remainingSlots.size() > 0;
    }

    public MenuPopulator copy() {
        return new MenuPopulator(this);
    }
}

@@ -1805,21 +1805,23 @@
 ;%0A    pr
-ivate
+otected
  List%3CIn
@@ -3694,19 +3694,17 @@
 ots.
-size() == 0
+isEmpty()
 ) %7B%0A
@@ -5227,32 +5227,33 @@
 %0A        return 
+!
 remainingSlots.s
@@ -5251,26 +5251,25 @@
 ngSlots.
-size() %3E 0
+isEmpty()
 ;%0A    %7D%0A

refactor MenuPopulator