/*
    This file is a part of the SteamWar software.

    Copyright (C) 2020  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.bausystem.world;

import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.FlatteningWrapper;
import net.md_5.bungee.api.ChatMessageType;
import net.md_5.bungee.api.chat.TextComponent;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.Listener;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;

import java.util.*;

public class Detonator implements Listener {

    public static final ItemStack WAND;

    public static final Map<Player, Set<Detoloader.DetonatorActivation>> PLAYER_LOCS = new HashMap<>();

    static {
        WAND = new ItemStack(Material.BLAZE_ROD, 1);
        ItemMeta im = WAND.getItemMeta();

        im.setDisplayName("§eFernzünder");

        List<String> lorelist = Arrays.asList("§eLinks Klick §8- §7Setzte einen Punkt zum Aktivieren",
                "§eLinks Klick + Shift §8- §7Füge einen Punkt hinzu", "§eRechts Klick §8- §7Löse alle Punkte aus");
        im.setLore(lorelist);

        WAND.setItemMeta(im);
    }

    public static Detonator getDetonator(Player player, ItemStack item) {
        return new Detonator(player, PLAYER_LOCS.get(player));
    }

    public static ItemStack setLocation(Player player, ItemStack item, Detoloader.DetonatorActivation detoloader) {
        PLAYER_LOCS.computeIfAbsent(player, player1 -> new HashSet<>()).clear();
        PLAYER_LOCS.get(player).add(detoloader);
        return item;
    }

    public static ItemStack toggleLocation(ItemStack item, Player player, Detoloader detoloader, Location location) {
        if (PLAYER_LOCS.computeIfAbsent(player, player1 -> new HashSet<>()).stream().anyMatch(activation -> activation.location.equals(location))) {
            DetonatorListener.print(player, detoloader.addBack ? "§e" + detoloader.getBlock() + " entfernt" :
                    detoloader.getBlock(), Detonator.getDetonator(player, player.getInventory().getItemInMainHand()).getLocs().size() - 1);
            PLAYER_LOCS.computeIfAbsent(player, player1 -> new HashSet<>()).removeIf(activation -> activation.location.equals(location));
            return item;
        } else {
            DetonatorListener.print(player, detoloader.addBack ? "§e" + detoloader.getBlock() + " hinzugefügt" :
                    detoloader.getBlock(), Detonator.getDetonator(player, player.getInventory().getItemInMainHand()).getLocs().size() + 1);
            if (detoloader.getActivation() == 0) {
                PLAYER_LOCS.computeIfAbsent(player, player1 -> new HashSet<>()).add(new Detoloader.DetonatorActivation(location));
                return item;
            } else {
                PLAYER_LOCS.computeIfAbsent(player, player1 -> new HashSet<>()).add(new Detoloader.DetonatorActivation(detoloader.getActivation(), location));
                return item;
            }
        }
    }

    public static void execute(Player player) {
        execute(player, PLAYER_LOCS.get(player));
    }

    private static void execute(Player player, Set<Detoloader.DetonatorActivation> locs) {
        for (Detoloader.DetonatorActivation activation : locs) {

            if (activation.activation == -1) {
                FlatteningWrapper.impl.setRedstone(activation.location, !FlatteningWrapper.impl.getLever(activation.location.getBlock()));
            } else {
                FlatteningWrapper.impl.setRedstone(activation.location, true);
                Bukkit.getScheduler().runTaskLater(BauSystem.getPlugin(), () -> FlatteningWrapper.impl.setRedstone(activation.location, false), activation.activation);
            }
        }
        player.spigot().sendMessage(ChatMessageType.ACTION_BAR, TextComponent.fromLegacyText("§e" + locs.size() + " §7Punkt" + (locs.size() > 1 ? "e" : "") + " ausgelöst!"));
    }

    public static void clear(Player player) {
        PLAYER_LOCS.computeIfAbsent(player, player1 -> new HashSet<>()).clear();
    }

    private final Set<Detoloader.DetonatorActivation> locs;
    private final Player player;

    private Detonator(Player player, Set<Detoloader.DetonatorActivation> locs) {
        this.player = player;
        this.locs = locs;
    }

    public Set<Detoloader.DetonatorActivation> getLocs() {
        return locs;
    }

    public Player getPlayer() {
        return player;
    }
}
