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

import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.utils.BauMemberUpdateEvent;
import de.steamwar.command.SWCommand;
import de.steamwar.linkage.Linked;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;

@Linked
public class LoaderCommand extends SWCommand implements Listener {

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

    private boolean loaderNullCheck(Loader loader, Player p) {
        if (loader == null) {
            BauSystem.MESSAGE.send("LOADER_NO_LOADER", p);
            return true;
        }
        return false;
    }

    @Register(value = "setup", description = "LOADER_HELP_SETUP")
    public void setupLoader(@Validator Player player) {
        if (Loader.getLoader(player) != null) {
            BauSystem.MESSAGE.send("LOADER_SETUP_STOP_FIRST", player);
            return;
        }
        Loader.newLoader(player);
        BauSystem.MESSAGE.send("LOADER_NEW", player);
        BauSystem.MESSAGE.send("LOADER_HOW_TO_START", player);
    }

    @Register(value = "start", description = "LOADER_HELP_START")
    public void startLoader(@Validator Player player) {
        Loader loader = Loader.getLoader(player);
        if (loaderNullCheck(loader, player)) return;
        loader.start();
        BauSystem.MESSAGE.send("LOADER_ACTIVE", player);
    }

    @Register(value = "stop", description = "LOADER_HELP_STOP")
    public void stopLoader(@Validator Player player) {
        Loader loader = Loader.getLoader(player);
        if (loaderNullCheck(loader, player)) return;
        loader.stop();
        BauSystem.MESSAGE.send("LOADER_STOP", player);
    }

    @Register(value = "pause", description = "LOADER_HELP_PAUSE")
    public void pauseLoader(@Validator Player player) {
        Loader loader = Loader.getLoader(player);
        if (loaderNullCheck(loader, player)) return;
        loader.pause();
        BauSystem.MESSAGE.send("LOADER_PAUSED", player);
    }

    @Register(value = "gui", description = "LOADER_HELP_GUI")
    public void guiLoader(@Validator Player player) {
        Loader loader = Loader.getLoader(player);
        if (loaderNullCheck(loader, player)) return;
        loader.gui(Loader.SettingsSorting.ALL);
    }

    @Register(value = "wait", description = "LOADER_HELP_WAIT")
    public void shotDelayLoader(@Validator Player p, @Min(intValue = 1) @ErrorMessage("LOADER_SMALL_TIME") int delay) {
        Loader loader = Loader.getLoader(p);
        if (loaderNullCheck(loader, p)) return;
        if (loader.setTicksBetweenShots(delay)) {
            BauSystem.MESSAGE.send("LOADER_NEW_TIME", p, delay);
        } else {
            BauSystem.MESSAGE.send("LOADER_SMALL_TIME", p);
        }
    }

    @Register(value = "speed", description = "LOADER_HELP_SPEED")
    public void speedLoader(@Validator Player p, @Min(intValue = 0) @ErrorMessage("LOADER_SMALL_TIME") int delay) {
        Loader loader = Loader.getLoader(p);
        if (loaderNullCheck(loader, p)) return;
        BauSystem.MESSAGE.send("LOADER_NEW_LOAD_TIME", p, delay);
        loader.setTicksBetweenBlocks(delay);
    }

    @Register(value = "single", description = "LOADER_HELP_SINGLE")
    public void singleLoader(@Validator Player p) {
        Loader loader = Loader.getLoader(p);
        if (loaderNullCheck(loader, p)) return;
        loader.single();
        BauSystem.MESSAGE.send("LOADER_SINGLE_CMD", p);
    }

    @EventHandler
    public void onBauMemberUpdate(BauMemberUpdateEvent event) {
        event.getNewSpectator().forEach(player -> {
            Loader loader = Loader.getLoader(player);
            if (loader == null) return;
            loader.stop();
        });
    }
}
