/*
 *   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.schematicsystem.commands.schematiccommand.parts;

import de.steamwar.command.AbstractSWCommand;
import de.steamwar.command.SWCommand;
import de.steamwar.linkage.Linked;
import de.steamwar.schematicsystem.SchematicSystem;
import de.steamwar.schematicsystem.commands.schematiccommand.GUI;
import de.steamwar.schematicsystem.commands.schematiccommand.SchematicCommand;
import de.steamwar.schematicsystem.commands.schematiccommand.SchematicCommandUtils;
import de.steamwar.sql.NodeData;
import de.steamwar.sql.SchematicNode;
import de.steamwar.sql.SteamwarUser;
import net.md_5.bungee.api.chat.ClickEvent;
import net.md_5.bungee.api.chat.ComponentBuilder;
import net.md_5.bungee.api.chat.HoverEvent;
import net.md_5.bungee.api.chat.TextComponent;
import org.bukkit.entity.Player;

import java.util.List;

import static de.steamwar.schematicsystem.commands.schematiccommand.SchematicCommandUtils.*;

@AbstractSWCommand.PartOf(SchematicCommand.class)
@Linked
public class ViewPart extends SWCommand {
    public ViewPart() {
        super(null);
    }

    @Register("list")
    @Register({"list", "/"})
    public void schemList(Player player, @OptionalValue(value = "1", onlyUINIG = true) int page) {
        createCachedSchemList(player, SchematicNode.list(getUser(player), null), Math.max(page - 1, 0), null, SchematicCommandUtils.SchematicListBehavior.DEFAULT);
    }

    @Register({"list", "public"})
    public void schemListPublic(Player player, @Validator("isDirValidator") @Mapper("publicDirMapper") SchematicNode node, @OptionalValue("1") int page) {
        schemList(player, node, page);
    }

    @Register({"list", "public"})
    @Register({"list", "public", "/"})
    public void schemListPublic(Player player, @OptionalValue(value = "1", onlyUINIG = true) int page) {
        createCachedSchemList(player, SchematicNode.list(SteamwarUser.byId(0), null), Math.max(page - 1, 0), null, SchematicCommandUtils.SchematicListBehavior.builder().setPublics(true).setPageCommandGen(integer -> "/schem list public " + integer).build());
    }

    @Register("list")
    public void schemList(Player player, @Validator("isDirValidator") @Mapper("dirMapper") SchematicNode node, @OptionalValue("1") int page) {
        SteamwarUser user = getUser(player);
        createCachedSchemList(player, SchematicNode.list(user, node.getId()), Math.max(page - 1, 0), node, SchematicCommandUtils.SchematicListBehavior.builder().setPublics(node.getOwner() == 0).setPageCommandGen(value -> "/schem list " + (node.getOwner() == 0 ? "public " : "") + node.generateBreadcrumbs(user) + " " + value).build());
    }

    @Register({"info", "public"})
    public void schemInfoPublic(Player player, @Mapper("publicMapper") SchematicNode node) {
        schemInfo(player, SchematicNode.getSchematicNode(node.getId()));
    }

    @Register("info")
    public void schemInfo(Player player, @ErrorMessage("COMMAND_INVALID_NODE") SchematicNode node) {
        printSchemInfo(player, node);
    }

    @Register("revisions")
    public void revisions(Player player, @Validator("isSchemValidator") SchematicNode node) {
        List<NodeData> revisions = NodeData.get(node);
        if (revisions.isEmpty()) {
            SchematicSystem.MESSAGE.send("REVISIONS_EMPTY", player);
            return;
        }

        SchematicSystem.MESSAGE.send("REVISIONS_TITLE", player);
        for (int j = Math.max(0, revisions.size() - 10); j < revisions.size(); j++) {
            player.spigot().sendMessage(
                    new ComponentBuilder(SchematicSystem.MESSAGE.parseToComponent("REVISIONS_REVISION_NUMBER", false, player, j + 1, revisions.get(j).getCreatedAt()))
                            .event(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/schem load " + (node.getOwner() == 0 ? "public " : "") + node.generateBreadcrumbs(getUser(player)) + " " + (j + 1)))
                            .event(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new TextComponent[]{SchematicSystem.MESSAGE.parseToComponent("UTIL_INFO_ACTION_LOAD_HOVER", false, player)}))
                            .create()
            );
        }
    }

    @Register(value = "page", noTabComplete = true)
    public void pageCommand(Player player, int page) {
        cachedSchemList(player, page);
    }

    @Register({"l", "public"})
    @Register({"load", "public"})
    public void loadSchemPublic(Player player, @Validator("isSchemValidator") @Mapper("publicMapper") SchematicNode node, @OptionalValue("-1") int revision) {
        loadSchem(player, node, revision);
    }

    @Register("l")
    @Register("load")
    public void loadSchem(Player player, @Validator("isSchemValidator") SchematicNode node, @OptionalValue("-1") int revision) {
        SchematicCommandUtils.loadSchem(player, node, revision);
    }

    @Register("gui")
    public void gui(Player player) {
        GUI.list(player);
    }

    @Register("download")
    public void download(Player player, @Validator("isSchemValidator") SchematicNode node) {
        SchematicCommandUtils.download(player, node);
    }
}
