/*
 *   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.simulator;

import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.SWUtils;
import de.steamwar.bausystem.features.simulator.data.Simulator;
import de.steamwar.bausystem.features.simulator.execute.SimulatorExecutor;
import de.steamwar.command.PreviousArguments;
import de.steamwar.command.SWCommand;
import de.steamwar.command.TypeMapper;
import de.steamwar.command.TypeValidator;
import de.steamwar.linkage.Linked;
import de.steamwar.linkage.LinkedInstance;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import java.util.Collection;

@Linked
public class SimulatorCommand extends SWCommand {

    @LinkedInstance
    public SimulatorCursor simulatorCursor;

    public SimulatorCommand() {
        super("sim", "simulator");
    }

    @Register(description = "SIMULATOR_HELP")
    public void genericCommand(@Validator Player p) {
        SWUtils.giveItemToPlayer(p, SimulatorStorage.getWand(p));
        simulatorCursor.calcCursor(p);
    }

    @Register(value = "change", description = "SIMULATOR_CHANGE_HELP")
    public void change(@Validator Player p) {
        if (!SimulatorCursor.isSimulatorItem(p.getInventory().getItemInMainHand()) && !SimulatorCursor.isSimulatorItem(p.getInventory().getItemInOffHand())) {
            BauSystem.MESSAGE.send("SIMULATOR_NO_SIM_IN_HAND", p);
            return;
        }
        SimulatorStorage.openSimulatorSelector(p);
    }

    @Register(value = "copy", description = "SIMULATOR_COPY_HELP")
    public void copy(@Validator Player p, @ErrorMessage("SIMULATOR_NOT_EXISTS") Simulator simulator, @Validator("simulatorName") String name) {
        if (!SimulatorStorage.copy(simulator, name)) {
            BauSystem.MESSAGE.send("SIMULATOR_ERROR_COPY", p);
        }
    }

    @Register(value = "delete", description = "SIMULATOR_DELETE_HELP")
    public void delete(@Validator Player p, @ErrorMessage("SIMULATOR_NOT_EXISTS") Simulator simulator) {
        SimulatorStorage.delete(simulator);
        BauSystem.MESSAGE.send("SIMULATOR_DELETED", p);
    }

    @Register(value = "start", description = "SIMULATOR_START_HELP")
    public void start(@Validator Player p, @ErrorMessage("SIMULATOR_NOT_EXISTS") Simulator simulator) {
        SimulatorExecutor.run(p, simulator, null);
    }

    @Register(value = "rename", description = "SIMULATOR_RENAME_HELP")
    public void rename(@Validator Player p, @ErrorMessage("SIMULATOR_NOT_EXISTS") Simulator simulator, @Validator("simulatorName") String name) {
        String oldName = simulator.getName();
        if (SimulatorStorage.copy(simulator, name)) {
            SimulatorStorage.delete(simulator);
            BauSystem.MESSAGE.send("SIMULATOR_RENAMED", p, oldName, name);
        } else {
            BauSystem.MESSAGE.send("SIMULATOR_ERROR_COPY", p);
        }
    }

    @ClassMapper(value = Simulator.class, local = true)
    public TypeMapper<Simulator> allSimulators() {
        return new TypeMapper<>() {
            @Override
            public Simulator map(CommandSender commandSender, PreviousArguments previousArguments, String s) {
                return SimulatorStorage.getSimulator(s);
            }

            @Override
            public Collection<String> tabCompletes(CommandSender commandSender, PreviousArguments previousArguments, String s) {
                return SimulatorStorage.getSimulatorNames();
            }
        };
    }

    @Validator(value = "simulatorName", local = true)
    public TypeValidator<String> simulatorName() {
        return (commandSender, name, messageSender) -> {
            if (SimulatorStorage.getSimulator(name) != null) {
                messageSender.send("SIMULATOR_NAME_ALREADY_EXISTS");
                return false;
            }
            if (!name.matches("[a-zA-Z_0-9-]+")) {
                messageSender.send("SIMULATOR_NAME_INVALID");
                return false;
            }
            return true;
        };
    }
}
