/*
 *   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.bausystem.features.shieldprinting;

import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.Permission;
import de.steamwar.bausystem.region.Region;
import de.steamwar.command.SWCommand;
import de.steamwar.command.TypeValidator;
import de.steamwar.linkage.Linked;
import org.bukkit.entity.Player;
import org.bukkit.event.Listener;

import java.util.HashMap;
import java.util.Map;

@Linked
public class ShieldPrintingCommand extends SWCommand implements Listener {

    public ShieldPrintingCommand() {
        super("shieldprinting");
        addDefaultHelpMessage("SHIELD_PRINTING_HELP_START");
        addDefaultHelpMessage("SHIELD_PRINTING_HELP_COPY");
        addDefaultHelpMessage("SHIELD_PRINTING_HELP_APPLY");
        addDefaultHelpMessage("SHIELD_PRINTING_HELP_STOP");
        addDefaultHelpMessage("SHIELD_PRINTING_HELP_STEP_1");
        addDefaultHelpMessage("SHIELD_PRINTING_HELP_STEP_2");
        addDefaultHelpMessage("SHIELD_PRINTING_HELP_STEP_3");
        addDefaultHelpMessage("SHIELD_PRINTING_HELP_STEP_4");
        addDefaultHelpMessage("SHIELD_PRINTING_HELP_STEP_5");
        addDefaultHelpMessage("SHIELD_PRINTING_HELP_STEP_6");
        addDefaultHelpMessage("SHIELD_PRINTING_HELP_STEP_7");
    }

    static final Map<Region, ShieldPrinting> SHIELD_PRINTING_MAP = new HashMap<>();

    @Register
    public void genericCommand(@Validator Player player, ShieldPrintingState shieldPrintingState) {
        Region region = Region.getRegion(player.getLocation());
        if (region.getType().isGlobal()) {
            BauSystem.MESSAGE.send("SHIELD_PRINTING_NO_REGION", player);
            return;
        }
        ShieldPrinting shieldPrinting;
        switch (shieldPrintingState) {
            case START:
                shieldPrinting = SHIELD_PRINTING_MAP.put(region, new ShieldPrinting(region));
                if (shieldPrinting != null) {
                    shieldPrinting.disable();
                }
                BauSystem.MESSAGE.send("SHIELD_PRINTING_START", player);
                break;
            case COPY:
                shieldPrinting = SHIELD_PRINTING_MAP.get(region);
                if (shieldPrinting == null) {
                    BauSystem.MESSAGE.send("SHIELD_PRINTING_NOT_RUNNING", player);
                    return;
                }
                shieldPrinting.copy();
                BauSystem.MESSAGE.send("SHIELD_PRINTING_COPY", player);
                break;
            case APPLY:
                shieldPrinting = SHIELD_PRINTING_MAP.get(region);
                if (shieldPrinting == null) {
                    BauSystem.MESSAGE.send("SHIELD_PRINTING_NOT_RUNNING", player);
                    return;
                }
                shieldPrinting.apply(player);
                break;
        }
    }

    @Register("stop")
    public void stopCommand(@Validator Player player) {
        Region region = Region.getRegion(player.getLocation());
        if (region.getType().isGlobal()) {
            BauSystem.MESSAGE.send("SHIELD_PRINTING_NO_REGION", player);
            return;
        }
        ShieldPrinting shieldPrinting = SHIELD_PRINTING_MAP.remove(region);
        if (shieldPrinting == null) {
            BauSystem.MESSAGE.send("SHIELD_PRINTING_NOT_RUNNING", player);
            return;
        }
        shieldPrinting.disable();
        BauSystem.MESSAGE.send("SHIELD_PRINTING_STOP", player);
    }

    @ClassValidator(value = Player.class, local = true)
    public TypeValidator<Player> validator() {
        return (commandSender, player, messageSender) -> {
            if (!Permission.BUILD.hasPermission(player)) {
                messageSender.send("NO_PERMISSION", player);
                return false;
            }
            Region region = Region.getRegion(player.getLocation());
            if (region.getType().isGlobal()) {
                messageSender.send("SHIELD_PRINTING_NO_REGION", player);
                return false;
            }
            return true;
        };
    }
}
