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

import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.region.Point;
import de.steamwar.bausystem.region.Region;
import de.steamwar.bausystem.region.utils.RegionExtensionType;
import de.steamwar.bausystem.region.utils.RegionType;
import de.steamwar.bausystem.utils.FlatteningWrapper;
import de.steamwar.command.SWCommand;
import de.steamwar.linkage.Linked;
import org.bukkit.entity.Player;

@Linked
public class SelectCommand extends SWCommand {

    public SelectCommand() {
        super("select");
    }

    @Register(description = {"SELECT_HELP", "SELECT_EXTENSION_HELP"})
    public void baurahmenCommand(@Validator Player p, RegionType regionType, @OptionalValue("NORMAL") RegionExtensionType regionExtensionType) {
        Region region = Region.getRegion(p.getLocation());

        if (region.getType().isGlobal()) {
            BauSystem.MESSAGE.send("SELECT_GLOBAL_REGION", p);
            return;
        }

        if (!regionType.getHasType().test(region)) {
            BauSystem.MESSAGE.send("SELECT_NO_TYPE", p, BauSystem.MESSAGE.parse(regionType.getChatValue(), p));
            return;
        }

        setSelection(regionType, regionExtensionType, region, p);
    }

    @Register("tb")
    public void baurahmenCommand(@Validator Player p, @OptionalValue("NORMAL") RegionExtensionType regionExtensionType) {
        baurahmenCommand(p, RegionType.TESTBLOCK, regionExtensionType);
    }

    private void setSelection(RegionType regionType, RegionExtensionType regionExtensionType, Region region, Player p) {
        Point minPoint = regionType.getToMinPoint().apply(region, regionExtensionType.isExtension());
        Point maxPoint = regionType.getToMaxPoint().apply(region, regionExtensionType.isExtension());

        FlatteningWrapper.impl.setSelection(p, minPoint, maxPoint);
        BauSystem.MESSAGE.send("SELECT_MESSAGE", p, minPoint.getX(), minPoint.getY(), minPoint.getZ(), maxPoint.getX(), maxPoint.getY(), maxPoint.getZ());
    }
}
