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

import com.sk89q.worldedit.IncompleteRegionException;
import com.sk89q.worldedit.WorldEdit;
import com.sk89q.worldedit.bukkit.BukkitAdapter;
import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldedit.regions.CuboidRegion;
import com.sk89q.worldedit.regions.RegionSelector;
import de.steamwar.command.SWCommand;
import de.steamwar.linkage.Linked;
import de.steamwar.teamserver.Builder;
import org.bukkit.Bukkit;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;

import java.io.File;
import java.io.IOException;

@Linked
public class ArenaconfigCommand extends SWCommand {

    public ArenaconfigCommand() {
        super("arenaconfig");
    }

    @Register
    public void genericHelp(Player p, String... args) {
        Builder.MESSAGE.send("ARENACONFIG_HELP", p);
    }

    @Register
    public void config(Player p, int lowerPlayerBorder) throws IOException {
        CuboidRegion region = getSelection(p);
        if (region == null) return;

        BlockVector3 pos1 = region.getPos1();
        BlockVector3 pos2 = region.getPos2();

        File file = new File(Bukkit.getWorlds().get(0).getWorldFolder(), "config.yml");
        YamlConfiguration config = YamlConfiguration.loadConfiguration(file);

        config.set("UnderBorder", lowerPlayerBorder);
        config.set("BlueCorner.x", pos1.x());
        config.set("BlueCorner.y", pos1.y());
        config.set("BlueCorner.z", pos1.z());
        config.set("BlueToRed.x", pos2.x() - pos1.x());
        config.set("BlueToRed.y", pos2.y() - pos1.y());
        config.set("BlueToRed.z", pos2.z() - pos1.z());

        config.save(file);
    }

    private CuboidRegion getSelection(Player player) {
        RegionSelector regionSelector = WorldEdit.getInstance()
                .getSessionManager()
                .get(BukkitAdapter.adapt(player))
                .getRegionSelector(BukkitAdapter.adapt(player.getWorld()));

        try {
            return (CuboidRegion) regionSelector.getRegion();
        } catch (IncompleteRegionException e) {
            Builder.MESSAGE.send("ARENACONFIG_SELECTION", player);
            return null;
        }
    }
}
