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

import de.steamwar.command.SWCommand;
import de.steamwar.linkage.Linked;
import de.steamwar.messages.Chatter;
import de.steamwar.messages.Message;
import net.kyori.adventure.text.event.ClickEvent;

import java.util.function.Function;

@Linked
public class HelpCommand extends SWCommand {

    public HelpCommand() {
        super("help", "?");
    }

    @Register
    public void genericCommand(Chatter sender) {
        printPage(sender, ClickEvent::runCommand,
                "HELP_LOBBY", "/l",
                "HELP_BAU", "/build",
                "HELP_BAUSERVER", "/help build",
                "HELP_FIGHT", "/fight",
                "HELP_CHALLENGE", "/challenge",
                "HELP_HISTORIC", "/historic",
                "HELP_TEAM", "/team",
                "HELP_JOIN", "/join",
                "HELP_LOCAL", "/local");
    }

    @Register({"build","world"})
    public void buildWorld(Chatter sender) {
        printPage(sender, "HELP_BAU_GROUP_WORLD_TITLE", "HELP_TNT", "HELP_FIRE", "HELP_FREEZE", "HELP_TPSLIMIT", "HELP_PROTECT", "HELP_RESET");
    }

    @Register({"build","player"})
    public void buildPlayer(Chatter sender) {
        printPage(sender, "HELP_BAU_GROUP_PLAYER_TITLE", "HELP_SPEED", "HELP_NV", "HELP_DEBUGSTICK", "HELP_TRACE", "HELP_LOADER");
    }

    @Register({"build","worldedit"})
    @Register({"build","we"})
    @Register({"build","world-edit"})
    @Register({"build","edit"})
    public void buildWorldedit(Chatter sender) {
        printPage(sender, "HELP_BAU_GROUP_WE_TITLE", "HELP_WE_POS1", "HELP_WE_POS2", "HELP_WE_COPY", "HELP_WE_PASTE", "HELP_WE_FLOPY", "HELP_WE_FLOPYP", "HELP_WE_ROTATE_90", "HELP_WE_ROTATE_180", "HELP_WE_ROTATE_N90");
    }

    @Register({"build","other"})
    public void buildOther(Chatter sender) {
        printPage(sender, "HELP_BAU_GROUP_OTHER_TITLE", "HELP_TESTBLOCK", "HELP_SKULL", "HELP_BAUINFO");
        sender.prefixless("HELP_SCHEMSUBMIT", new Message("HELP_SCHEMSUBMIT_HOVER"), ClickEvent.openUrl("https://www.youtube.com/watch?v=9QrQ3UBWveE"));
    }

    @Register("build")
    public void sendBauHelpGroup(Chatter sender) {
        printPage(sender, ClickEvent::runCommand,
                "HELP_BAU_GROUP_ADMIN", "/help build admin",
                "HELP_BAU_GROUP_WORLD", "/help build world",
                "HELP_BAU_GROUP_PLAYER", "/help build player",
                "HELP_BAU_GROUP_WE", "/help build we",
                "HELP_BAU_GROUP_OTHER", "/help build other");
    }

    @Register("buildserver")
    @Register({"build","admin"})
    @Register({"build","owner"})
    @Register({"build","bauwelt"})
    public void sendBauHelp(Chatter sender) {
        printPage(sender, ClickEvent::suggestCommand,
                "HELP_BAU_TP", "/build tp ",
                "HELP_BAU_ADDMEMBER", "/build addmember ",
                "HELP_BAU_DELMEMBER", "/build delmember ",
                "HELP_BAU_SET_SPECTATOR", "/build setSpectator ",
                "HELP_BAU_SET_BUILDER", "/build setBuilder ",
                "HELP_BAU_SET_SUPERVISOR", "/build setSupervisor ",
                "HELP_BAU_STOP", "/build stop",
                "HELP_BAU_DELETE", "/build delete ",
                "HELP_BAU_TESTARENA", "/build testarena ",
                "HELP_BAU_LOCK", "/build lock ",
                "HELP_BAU_UNLOCK", "/build unlock");
    }

    private static void printPage(Chatter sender, Function<String, ClickEvent> action, String... args) {
        for(int i = 0; i < args.length; i += 2) {
            String message = args[i];
            String hoverMessage = message + "_HOVER";
            String command = args[i+1];

            sender.system(message, new Message(hoverMessage), action.apply(command));
        }
    }

    private static void printPage(Chatter sender, String title, String... messages) {
        sender.system(title);
        for (String message : messages) {
            sender.prefixless(message);
        }
    }
}
