package de.steamwar.bausystem.features.util;

import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.Permission;
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.command.TypeValidator;
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.isGlobal()) {
            BauSystem.MESSAGE.send("SELECT_GLOBAL_REGION", p);
            return;
        }

        if (!region.hasType(regionType)) {
            BauSystem.MESSAGE.send("SELECT_NO_TYPE", p, BauSystem.MESSAGE.parse(regionType.getChatValue(), p));
            return;
        }
        if (regionExtensionType == RegionExtensionType.EXTENSION && !region.hasExtensionType(regionType)) {
            BauSystem.MESSAGE.send("SELECT_NO_EXTENSION", 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 = region.getMinPoint(regionType, regionExtensionType);
        Point maxPoint = region.getMaxPoint(regionType, regionExtensionType);

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