// @formatter:off
/*
 * Pexel Project - Minecraft minigame server platform. 
 * Copyright (C) 2014 Matej Kormuth <http://www.matejkormuth.eu>
 * 
 * This file is part of Pexel.
 * 
 * Pexel is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
 * 
 * Pexel is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along with this program. If not, see
 * <http://www.gnu.org/licenses/>.
 *
 */
// @formatter:on
package eu.matejkormuth.pexel.PexelCore.kits;

import java.util.ArrayList;
import java.util.List;

import org.bukkit.entity.Player;

import eu.matejkormuth.pexel.PexelCore.actions.JavaArbitraryAction;
import eu.matejkormuth.pexel.PexelCore.menu.InventoryMenu;
import eu.matejkormuth.pexel.PexelCore.menu.InventoryMenuItem;
import eu.matejkormuth.pexel.PexelCore.util.ParametrizedRunnable;

/**
 * Class representating inventory menu for kits.
 * 
 * @author Mato Kormuth
 * 
 */
public class KitInventoryMenu {
    private final InventoryMenu  menu;
    private ParametrizedRunnable onKitSelected;
    
    /**
     * Creates a new inventory menu for selecting kit.
     * 
     * @param kits
     *            kits in inventory menu.
     */
    public KitInventoryMenu(final Kit... kits) {
        List<InventoryMenuItem> items = new ArrayList<InventoryMenuItem>();
        for (int i = 0; i < kits.length; i++) {
            final Kit kit = kits[i];
            items.add(new InventoryMenuItem(kit.getIcon(), new JavaArbitraryAction(
                    new ParametrizedRunnable() {
                        @Override
                        public void run(final Object... args) {
                            kit.applyKit(((Player) args[0]));
                            KitInventoryMenu.this.onKitSelected.run(args[0], kit);
                        }
                    }), i, false));
        }
        
        this.menu = new InventoryMenu(9, "Select kit: ", items);
    }
    
    public void setOnKitSelected(final ParametrizedRunnable runnable) {
        this.onKitSelected = runnable;
    }
    
    /**
     * Shows inventory to player.
     * 
     * @param player
     *            player to show menu to
     */
    public void showTo(final Player player) {
        this.menu.showTo(player);
    }
}
