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

import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.SWUtils;
import de.steamwar.bausystem.shared.Pair;
import de.steamwar.bausystem.utils.WorldEditUtils;
import de.steamwar.command.SWCommand;
import de.steamwar.linkage.Linked;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable;

@Linked
public class LaufbauCommand extends SWCommand {

    public LaufbauCommand() {
        super("laufbau", "laufsklave");
    }

    @Register(description = {"LAUFBAU_HELP", "LAUFBAU_PREPARE1", "LAUFBAU_PREPARE2"})
    public void laufbauSelection(@Validator Player player, @StaticValue(value = {"smallest", "blastresistant"}, allowISE = true) boolean preferingBlastResistance) {
        Pair<Location, Location> selection = WorldEditUtils.getSelection(player);
        if (selection == null) {
            BauSystem.MESSAGE.send("LAUFBAU_NO_WORLDEDIT", player);
            return;
        }

        Laufbau laufbau = new Laufbau(player, selection.getKey(), selection.getValue(), preferingBlastResistance);
        new BukkitRunnable() {
            @Override
            public void run() {
                long totalTime = 0;
                while (true) {
                    if (!laufbau.hasNext()) {
                        BauSystem.MESSAGE.send("LAUFBAU_DONE", player);
                        laufbau.getEditSession().flushSession();
                        WorldEditUtils.addToPlayer(player, laufbau.getEditSession());
                        cancel();
                        return;
                    }
                    long time = System.currentTimeMillis();
                    laufbau.next();
                    totalTime += System.currentTimeMillis() - time;
                    if (totalTime > 40) {
                        break;
                    }
                }
                Bukkit.getOnlinePlayers().forEach(p -> {
                    SWUtils.sendToActionbar(p, laufbau.actionBarMessage(p));
                });
            }
        }.runTaskTimer(BauSystem.getInstance(), 1, 1);
    }

    @Register(value = "settings", description = "LAUFBAU_HELP_SETTINGS")
    public void laufbauSettings(@Validator Player player) {
        new LaufbauSettings(player);
    }
}
