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

import com.sk89q.worldedit.*;
import com.sk89q.worldedit.extent.clipboard.Clipboard;
import de.steamwar.command.*;
import de.steamwar.core.VersionDependent;
import de.steamwar.linkage.Linked;
import de.steamwar.schematicsystem.CheckSchemType;
import de.steamwar.schematicsystem.SchematicSystem;
import de.steamwar.schematicsystem.autocheck.AutoCheckerResult;
import de.steamwar.sql.*;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

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

@SuppressWarnings("unused")
@Linked
public class SchematicCommand extends SWCommand {

    public SchematicCommand() {
        super("schematic", new String[] {"schem", "/schem", "/schematic"});
    }

    @Register("help")
    public void pagedHelp(Player player, HelpPage page) {
        printHelpPage(player, page);
    }

    @Register
    public void genericHelp(Player player, String... args) {
        printHelpMainPage(player);
    }

    @Register(value = "togglepublic", noTabComplete = true)
    public void togglePublicMode(Player player) {
        SteamwarUser user = SteamwarUser.get(player.getUniqueId());
        if (!user.hasPerm(UserPerm.MODERATION)) {
            genericHelp(player);
            return;
        }

        if (togglePublic(player)) {
            SchematicSystem.MESSAGE.send("COMMAND_PUBLIC_ON", player);
        } else {
            SchematicSystem.MESSAGE.send("COMMAND_PUBLIC_OFF", player);
        }
    }

    @Mapper("publicMapper")
    public TypeMapper<SchematicNode> publicNodeTypeMapper() {
        return SchematicMapper.publicNodeTypeMapper();
    }

    @Mapper("memberMapper")
    public static TypeMapper<NodeMember> nodeMemberTypeMapper() {
        return SchematicMapper.nodeMemberTypeMapper();
    }

    @Mapper("dirMapper")
    public static TypeMapper<SchematicNode> dirNodeTypeMapper() {
        return SchematicMapper.dirNodeTypeMapper();
    }

    @Mapper("publicDirMapper")
    public static TypeMapper<SchematicNode> publicDirNodeTypeMapper() {
        return SchematicMapper.publicDirNodeTypeMapper();
    }

    @Mapper("dirStringMapper")
    public static TypeMapper<String> stringTabMapper() {
        return SchematicMapper.stringTabMapper();
    }

    @Mapper("stringMapper")
    public static TypeMapper<String> stringMapper() {
        return SchematicMapper.stringMapper();
    }

    @ClassMapper(SchematicType.class)
    public static TypeMapper<SchematicType> typeTypeMapper() {
        return SchematicMapper.typeTypeMapper();
    }

    @AbstractSWCommand.ClassMapper(value = SchematicNode.class, local = true)
    public static TypeMapper<SchematicNode> nodeTypeMapper() {
        return SchematicMapper.nodeTypeMapper();
    }

    @ClassMapper(value = CheckSchemType.class, local = true)
    public static TypeMapper<CheckSchemType> checkSchemTypeTypeMapper() {
        return SchematicMapper.checkSchemTypeTypeMapper();
    }

    @Override
    protected void sendMessage(CommandSender sender, String message, Object[] args) {
        SchematicSystem.MESSAGE.send(message, sender, args);
    }

    @Validator(value = "isSchemValidator", local = true)
    public static TypeValidator<SchematicNode> isSchemValidator() {
        return SchematicValidator.isSchemValidator();
    }

    @Validator(value = "isDirValidator", local = true)
    public static TypeValidator<SchematicNode> isDirValidator() {
        return SchematicValidator.isDirValidator();
    }

    @Validator(value = "isOwnerValidator", local = true)
    public static TypeValidator<SchematicNode> isOwnerValidator() {
        return SchematicValidator.isOwnerValidator();
    }

    @Validator(value = "isOwnerSchematicValidator", local = true)
    public static TypeValidator<SchematicNode> isOwnerSchematicValidator() {
        return SchematicValidator.isOwnerSchematicValidator();
    }

    public enum Extend {
        AUSFAHREN,
        NORMAL
    }

    public static final ISchematicCommand impl = VersionDependent.getVersionImpl(SchematicSystem.getInstance());

    public interface ISchematicCommand {
        Clipboard fixClipboard(Clipboard clipboard, AutoCheckerResult result, CheckSchemType type) throws Exception;
        void createCopy(EditSession editSession, Clipboard clipboard) throws WorldEditException;
    }
}
