/*
 * This file is a part of the SteamWar software.
 *
 * Copyright (C) 2021  SteamWar.de-Serverteam
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program 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 Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

package de.steamwar.lobby.portal;

import de.steamwar.lobby.LobbySystem;
import de.steamwar.lobby.command.ModifyCommand;
import de.steamwar.lobby.listener.Portals;
import de.steamwar.network.NetworkSender;
import de.steamwar.network.packets.client.ExecuteCommandPacket;
import de.steamwar.sql.SteamwarUser;
import org.bukkit.Location;
import org.bukkit.entity.Player;

import java.util.Iterator;
import java.util.Map;
import java.util.logging.Level;

public class CommandPortal implements PortalHandler {

    private final String command;

    public CommandPortal(Map<String, Object> section, Portal portal) {
        this.command = (String) section.get("command");
    }

    public CommandPortal(String command) {
        this.command = command;
    }

    @Override
    public void handle(Player player, Location loc) {
        String[] parts = command.split("\\\\");

        int[] stackIds = new int[parts.length-1];
        int maxId = 0;
        for(int i = 1; i < parts.length; i++) {
            stackIds[i-1] = Integer.parseInt(parts[i].substring(0, 1));
            if(stackIds[i-1] > maxId)
                maxId = stackIds[i-1];
        }

        Iterator<Portal> stack = Portals.getStack(player).iterator();
        String[] pieces = new String[stackIds.length];
        while (maxId > 0) {
            if(!stack.hasNext()) {
                LobbySystem.getPlugin().getLogger().log(Level.WARNING, "Stackportal with missing elements: " + player.getName() + " /" + command);
                player.sendMessage("§cEigentlich solltest du gerade gar nicht durch dieses Portal durchgehen können...");
                return;
            }

            Portal portal = stack.next();
            if(portal.type() == PortalType.STACK) {
                for(int i = 0; i < stackIds.length; i++) {
                    if(stackIds[i] == maxId) {
                        pieces[i] = ((StackPortal) portal.getHandler()).getText();
                    }
                }

                maxId--;
            }
        }

        StringBuilder cmd = new StringBuilder(parts[0]);
        for(int i = 0; i < pieces.length; i++) {
            cmd.append(pieces[i]).append(parts[i+1].substring(1));
        }

        if(ModifyCommand.modifying(player))
            player.sendMessage("/" + cmd);
        NetworkSender.send(new ExecuteCommandPacket(SteamwarUser.get(player.getUniqueId()).getId(), cmd.toString()));
    }

    @Override
    public void serialize(Map<String, Object> map) {
        map.put("command", command);
    }

    @Override
    public PortalType type() {
        return PortalType.COMMAND;
    }

    @Override
    public void delete() {
        // Nothing to remove
    }

    @Override
    public String toString() {
        return "Command: /" + command;
    }
}
