/*
 *   This file is a part of the SteamWar software.
 *
 *   Copyright (C) 2025 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.special.easter;

import de.steamwar.command.SWCommand;
import de.steamwar.inventory.SWItem;
import de.steamwar.inventory.SWListInv;
import de.steamwar.lobby.LobbySystem;
import de.steamwar.sql.UserConfig;
import org.bukkit.Material;
import org.bukkit.entity.Player;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

public class EggHuntCommand extends SWCommand {

    public EggHuntCommand() {
        super("egghunt", "easteregg", "easter", "egg", "eh");
    }

    @Register
    public void genericCommand(Player player, @OptionalValue("ALL") Selection selection) {
        AtomicInteger atomicInteger = new AtomicInteger();
        String found = UserConfig.getConfig(player.getUniqueId(), EggHunt.EGG_HUNT_CONFIG_KEY);
        AtomicInteger foundCount = new AtomicInteger();
        List<SWListInv.SWListEntry<Egg>> entries = EggHunt.getEggList().stream()
                .map(egg -> {
                    int index = atomicInteger.getAndIncrement();
                    boolean isFound = found != null && found.length() > index && found.charAt(index) == '1';
                    if (isFound) foundCount.getAndIncrement();
                    if (selection == Selection.FOUND && !isFound) return null;
                    if (selection == Selection.NOT_FOUND && isFound) return null;
                    SWItem swItem = egg.getItem(player, isFound);
                    if (swItem == null) return null;
                    return new SWListInv.SWListEntry<>(swItem, egg);
                })
                .filter(Objects::nonNull)
                .sorted(Comparator.comparing(eggSWListEntry -> eggSWListEntry.getObject().getDifficulty()))
                .collect(Collectors.toList());
        SWListInv<Egg> inv = new SWListInv<>(player, LobbySystem.getMessage().parse("EASTER_EGG_MENU", player, foundCount.get(), EggHunt.getEggList().size()), false, entries, (clickType, egg) -> {
        });
        inv.setItem(49, new SWItem(Material.WHITE_DYE, (byte) 15, LobbySystem.getMessage().parse(Selection.ALL.key, player), Collections.emptyList(), selection == Selection.ALL, clickType -> {
            genericCommand(player, Selection.ALL);
        }));
        inv.setItem(48, new SWItem(Material.RED_DYE, (byte) 1, LobbySystem.getMessage().parse(Selection.NOT_FOUND.key, player), Collections.emptyList(), selection == Selection.NOT_FOUND, clickType -> {
            genericCommand(player, Selection.NOT_FOUND);
        }));
        inv.setItem(50, new SWItem(Material.GREEN_DYE, (byte) 2, LobbySystem.getMessage().parse(Selection.FOUND.key, player), Collections.emptyList(), selection == Selection.FOUND, clickType -> {
            genericCommand(player, Selection.FOUND);
        }));
        inv.open();
    }

    public enum Selection {
        ALL("EASTER_EGG_SELECTION_ALL"),
        FOUND("EASTER_EGG_SELECTION_FOUND"),
        NOT_FOUND("EASTER_EGG_SELECTION_NOT_FOUND");

        private final String key;

        Selection(String key) {
            this.key = key;
        }
    }
}
