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

import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.Permission;
import de.steamwar.bausystem.world.AutoLoader;
import de.steamwar.bausystem.world.Welt;
import de.steamwar.command.SWCommand;
import org.bukkit.entity.Player;

public class CommandLoader extends SWCommand {

    public CommandLoader() {
        super("loader");
    }

    @Register(help = true)
    public void genericHelp(Player p, String... args) {
        p.sendMessage("§8/§eloader setup §8- §7Startet die Aufnahme der Aktionen");
        p.sendMessage("§8/§7loader undo §8- §7Entfernt die zuletzt aufgenommene Aktion");
        p.sendMessage("§8/§eloader start §8- §7Spielt die zuvor aufgenommenen Aktionen ab");
        p.sendMessage("§8/§7loader wait §8[§7Ticks§8] - §7Setzt die Wartezeit zwischen Schüssen");
        p.sendMessage("§8/§7loader speed §8[§7Ticks§8] - §7Setzt die Wartezeit zwischen Aktionen");
        p.sendMessage("§8/§eloader stop §8- §7Stoppt die Aufnahme bzw. das Abspielen");
        p.sendMessage("§7Der AutoLader arbeitet mit §eIngame§8-§eTicks §8(20 Ticks pro Sekunde)");
    }

    @Register({"setup"})
    public void setupCommand(Player p) {
        setup(p);
    }

    @Register({"undo"})
    public void undoCommand(Player p) {
        undo(p);
    }

    @Register({"start"})
    public void startCommand(Player p) {
        start(p);
    }

    @Register({"stop"})
    public void stopCommand(Player p) {
        stop(p);
    }

    @Register({"wait"})
    public void waitCommand(Player p, int time) {
        wait(p, time);
    }

    @Register({"speed"})
    public void speedCommand(Player p, int time) {
        speed(p, time);
    }

    private void setup(Player player) {
        AutoLoader.getLoader(player).setup();
    }

    private void undo(Player player) {
        AutoLoader loader = loader(player);
        if (loader == null)
            return;

        if (!loader.isSetup()) {
            player.sendMessage("§cDer AutoLader wird in den Setup-Zustand versetzt");
            setup(player);
        }

        loader.undo();
    }

    private void start(Player player) {
        AutoLoader loader = loader(player);
        if (loader == null)
            return;

        loader.start();
    }

    private void stop(Player player) {
        if (!AutoLoader.hasLoader(player)) {
            player.sendMessage(BauSystem.PREFIX + "§cDu hast keinen aktiven AutoLader");
            return;
        }
        AutoLoader.getLoader(player).stop();
    }

    private void wait(Player player, int time) {
        AutoLoader loader = loader(player);
        if (loader == null) {
            loader = AutoLoader.getLoader(player);
        }
        loader.wait(time);
    }

    private void speed(Player player, int time) {
        AutoLoader loader = loader(player);
        if (loader == null) {
            loader = AutoLoader.getLoader(player);
        }
        loader.blockWait(time);
    }

    private AutoLoader loader(Player player) {
        if (AutoLoader.hasLoader(player)) {
            return AutoLoader.getLoader(player);
        }
        player.sendMessage(BauSystem.PREFIX + "§cDu hast keinen aktiven AutoLader");
        player.sendMessage(BauSystem.PREFIX + "§7Es wird ein neuer AutoLader gestartet");
        setup(player);
        return null;
    }
}
