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

import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.features.simulator.SimulatorWatcher;
import de.steamwar.bausystem.utils.bossbar.BauSystemBossbar;
import de.steamwar.bausystem.utils.bossbar.BossBarService;
import org.bukkit.Bukkit;
import org.bukkit.boss.BarColor;
import org.bukkit.boss.BarStyle;
import org.bukkit.entity.Player;
import org.bukkit.event.HandlerList;
import org.bukkit.event.Listener;

public abstract class StabStep {

    protected final StabData data;

    protected StabStep(StabData data) {
        this.data = data;
        run();
    }

    protected final void run() {
        Bukkit.getScheduler().runTaskLater(BauSystem.getInstance(), () -> {
            for (Player player : Bukkit.getOnlinePlayers()) {
                BauSystemBossbar bossbar = BossBarService.instance.get(player, data.region, "simulator_stab_generator");
                bossbar.setColor(BarColor.GREEN);
                bossbar.setStyle(BarStyle.SEGMENTED_10);
                bossbar(bossbar, false);
            }
        }, 1);

        if (this instanceof Listener) {
            Bukkit.getPluginManager().registerEvents((Listener) this, BauSystem.getInstance());
        }
        Bukkit.getScheduler().runTaskLater(BauSystem.getInstance(), this::start, 20);
    }

    protected abstract void start();

    protected final void runSimulator(Runnable onFinish) {
        SimulatorExecutor.run(null, data.simulator, () -> {
            Bukkit.getScheduler().runTaskLater(BauSystem.getInstance(), () -> {
                if (this instanceof Listener) {
                    HandlerList.unregisterAll((Listener) this);
                }
                onFinish.run();
            }, 20);
        });
    }

    protected abstract void bossbar(BauSystemBossbar bossbar, boolean stopped);

    protected final void stop() {
        data.simulator.setStabGenerator(null);
        SimulatorWatcher.update(data.simulator);

        for (Player player : Bukkit.getOnlinePlayers()) {
            BauSystemBossbar bossbar = BossBarService.instance.get(player, data.region, "simulator_stab_generator");
            bossbar.setColor(BarColor.GREEN);
            bossbar.setStyle(BarStyle.SEGMENTED_10);
            bossbar(bossbar, true);
        }
        new Thread(() -> {
            try {
                Thread.sleep(4000);
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            } finally {
                for (Player player : Bukkit.getOnlinePlayers()) {
                    BossBarService.instance.remove(player, data.region, "simulator_stab_generator");
                }
            }
        }).start();
    }
}
