/*
 * Copyright (c) 2015 Legend Zero LLC
 *
 * 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 com.legendzero.lzlib.gui;

import org.bukkit.entity.Player;
import org.bukkit.event.inventory.InventoryClickEvent;

import java.util.Arrays;
import java.util.Iterator;
import java.util.function.Consumer;

public interface GuiClickHandler extends Consumer<InventoryClickEvent> {

    static GuiClickHandler performCommands(String... commands) {
        return event -> Arrays.stream(commands).forEach(cmd -> ((Player) event.getWhoClicked()).performCommand(cmd));
    }

    static GuiClickHandler performCommands(Iterable<String> commands) {
        return event -> commands.forEach(cmd -> ((Player) event.getWhoClicked()).performCommand(cmd));
    }

    static GuiClickHandler performCommands(Iterator<String> commands) {
        return event -> commands.forEachRemaining(cmd -> ((Player) event.getWhoClicked()).performCommand(cmd));
    }

    static GuiClickHandler performLRCommands(String[] leftCommands, String[] rightCommands) {
        return event -> {
            if (event.getClick().isLeftClick()) {
                Arrays.stream(leftCommands).forEach(cmd -> ((Player) event.getWhoClicked()).performCommand(cmd));
            } else if (event.getClick().isRightClick()) {
                Arrays.stream(rightCommands).forEach(cmd -> ((Player) event.getWhoClicked()).performCommand(cmd));
            }
        };
    }

    static GuiClickHandler performLRCommands(Iterable<String> leftCommands, Iterable<String> rightCommands) {
        return event -> {
            if (event.getClick().isLeftClick()) {
                leftCommands.forEach(cmd -> ((Player) event.getWhoClicked()).performCommand(cmd));
            } else if (event.getClick().isRightClick()) {
                rightCommands.forEach(cmd -> ((Player) event.getWhoClicked()).performCommand(cmd));
            }
        };
    }

    static GuiClickHandler performLRCommands(Iterator<String> leftCommands, Iterator<String> rightCommands) {
        return event -> {
            if (event.getClick().isLeftClick()) {
                leftCommands.forEachRemaining(cmd -> ((Player) event.getWhoClicked()).performCommand(cmd));
            } else if (event.getClick().isRightClick()) {
                rightCommands.forEachRemaining(cmd -> ((Player) event.getWhoClicked()).performCommand(cmd));
            }
        };
    }
}

@@ -1505,32 +1505,138 @@
 stream(commands)
+%0A                .map(cmd -%3E cmd.replaceAll(%22%25player%25%22, event.getWhoClicked().getName()))%0A                
 .forEach(cmd -%3E 
@@ -1841,32 +1841,49 @@
 getWhoClicked())
+%0A                
 .performCommand(
@@ -1877,32 +1877,88 @@
 rformCommand(cmd
+.replaceAll(%22%25player%25%22, event.getWhoClicked().getName())
 ));%0A    %7D%0A%0A    s
@@ -2105,32 +2105,49 @@
 getWhoClicked())
+%0A                
 .performCommand(
@@ -2141,32 +2141,88 @@
 rformCommand(cmd
+.replaceAll(%22%25player%25%22, event.getWhoClicked().getName())
 ));%0A    %7D%0A%0A    s
@@ -2421,32 +2421,154 @@
 am(leftCommands)
+%0A                        .map(cmd -%3E cmd.replaceAll(%22%25player%25%22, event.getWhoClicked().getName()))%0A                        
 .forEach(cmd -%3E 
@@ -2724,16 +2724,138 @@
 ommands)
+%0A                        .map(cmd -%3E cmd.replaceAll(%22%25player%25%22, event.getWhoClicked().getName()))%0A                        
 .forEach
@@ -3211,35 +3211,116 @@
 icked())
-.performCommand(cmd
+%0A                        .performCommand(cmd.replaceAll(%22%25player%25%22, event.getWhoClicked().getName())
 ));%0A    
@@ -3450,35 +3450,116 @@
 icked())
-.performCommand(cmd
+%0A                        .performCommand(cmd.replaceAll(%22%25player%25%22, event.getWhoClicked().getName())
 ));%0A    
@@ -3857,35 +3857,116 @@
 icked())
-.performCommand(cmd
+%0A                        .performCommand(cmd.replaceAll(%22%25player%25%22, event.getWhoClicked().getName())
 ));%0A    
@@ -4105,35 +4105,116 @@
 icked())
-.performCommand(cmd
+%0A                        .performCommand(cmd.replaceAll(%22%25player%25%22, event.getWhoClicked().getName())
 ));%0A    

Add replacement of %player% with the executing player.