/*
 *   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 de.steamwar.command.AbstractValidator;
import de.steamwar.command.TypeValidator;
import de.steamwar.sql.SchematicNode;
import de.steamwar.sql.SteamwarUser;
import lombok.experimental.UtilityClass;
import org.bukkit.entity.Player;

@UtilityClass
public class SchematicValidator {

    private static boolean nodeNullCheck(AbstractValidator.MessageSender messageSender, SchematicNode node) {
        if (node == null) {
            messageSender.send("COMMAND_INVALID_NODE");
            return false;
        }
        return true;
    }

    public static TypeValidator<SchematicNode> isSchemValidator() {
        return (commandSender, schematicNode, messageSender) -> {
            if (!nodeNullCheck(messageSender, schematicNode)) {
                return false;
            }
            if (schematicNode.isDir()) {
                messageSender.send("COMMAND_MUST_SCHEM");
                return false;
            }
            return true;
        };
    }

    public static TypeValidator<SchematicNode> isDirValidator() {
        return (commandSender, schematicNode, messageSender) -> {
            if (!nodeNullCheck(messageSender, schematicNode)) {
                return false;
            }
            if (!schematicNode.isDir()) {
                messageSender.send("COMMAND_MUST_DIR");
                return false;
            }
            return true;
        };
    }

    public static TypeValidator<SchematicNode> isOwnerValidator() {
        return (commandSender, schematicNode, messageSender) -> {
            if (!nodeNullCheck(messageSender, schematicNode)) {
                return false;
            }
            if (schematicNode.getOwner() != SteamwarUser.get(((Player) commandSender).getUniqueId()).getId()) {
                messageSender.send("COMMAND_NOT_OWN");
                return false;
            }
            return true;
        };
    }

    public static TypeValidator<SchematicNode> isOwnerSchematicValidator() {
        return (commandSender, schematicNode, messageSender) -> {
            if (!nodeNullCheck(messageSender, schematicNode)) {
                return false;
            }
            if (schematicNode.getOwner() != SchematicCommandUtils.getUser((Player) commandSender).getId()) {
                messageSender.send("COMMAND_NOT_OWN");
                return false;
            }
            if (schematicNode.isDir()) {
                messageSender.send("COMMAND_MUST_SCHEM");
                return false;
            }
            return true;
        };
    }
}
