/*
 *   This file is a part of the SteamWar software.
 *
 *   Copyright (C) 2026 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.experimental.redstone_engine;

import de.steamwar.bausystem.features.experimental.ExperimentalCommand;
import de.steamwar.bausystem.region.Region;
import de.steamwar.bausystem.utils.ScoreboardElement;
import de.steamwar.command.AbstractSWCommand;
import de.steamwar.command.SWCommand;
import de.steamwar.linkage.Linked;
import io.papermc.paper.configuration.WorldConfiguration;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.title.TitlePart;
import org.bukkit.Bukkit;
import org.bukkit.craftbukkit.CraftWorld;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;

import java.util.Collection;
import java.util.List;

@AbstractSWCommand.PartOf(ExperimentalCommand.class)
@Linked
public class RedstoneEngine extends SWCommand implements Listener, ScoreboardElement {

    public RedstoneEngine() {
        super("");
    }

    private WorldConfiguration.Misc getConfig() {
        return ((CraftWorld) Bukkit.getWorlds().get(0)).getHandle().paperConfig().misc;
    }

    @Register("redstone")
    @Register("redstoneengine")
    public void setRedstoneEngine(Player player, @StaticValue("alternate_current") String __, WorldConfiguration.Misc.AlternateCurrentUpdateOrder updateOrder) {
        WorldConfiguration.Misc misc = getConfig();
        misc.redstoneImplementation = WorldConfiguration.Misc.RedstoneImplementation.ALTERNATE_CURRENT;
        misc.alternateCurrentUpdateOrder = updateOrder;
        broadcastTitle(Bukkit.getOnlinePlayers());
    }

    @Register("redstone")
    @Register("redstoneengine")
    public void setRedstoneEngine(Player player, WorldConfiguration.Misc.RedstoneImplementation implementation) {
        getConfig().redstoneImplementation = implementation;
        broadcastTitle(Bukkit.getOnlinePlayers());
    }

    @EventHandler
    public void onPlayerJoin(PlayerJoinEvent event) {
        WorldConfiguration.Misc misc = getConfig();
        if (misc.redstoneImplementation != WorldConfiguration.Misc.RedstoneImplementation.EIGENCRAFT) {
            broadcastTitle(List.of(event.getPlayer()));
        }
    }

    private void broadcastTitle(Collection<? extends Player> players) {
        WorldConfiguration.Misc misc = getConfig();
        Component title = switch (misc.redstoneImplementation) {
            case VANILLA -> Component.text("⚠").color(NamedTextColor.RED).append(Component.text(" Redstone: Vanilla ").color(NamedTextColor.WHITE)).append(Component.text("⚠").color(NamedTextColor.RED));
            case EIGENCRAFT -> Component.text("Redstone: Eigencraft");
            case ALTERNATE_CURRENT -> Component.text("⚠").color(NamedTextColor.RED).append(Component.text(" Redstone: AC ").color(NamedTextColor.WHITE)).append(Component.text("⚠").color(NamedTextColor.RED));
        };
        Component subtitle;
        if (misc.redstoneImplementation != WorldConfiguration.Misc.RedstoneImplementation.ALTERNATE_CURRENT) {
            subtitle = Component.text().build();
        } else {
            subtitle = switch (misc.alternateCurrentUpdateOrder) {
                case VERTICAL_FIRST_INWARD -> Component.text("Y first inwards"); // Y before XZ
                case VERTICAL_FIRST_OUTWARD -> Component.text("Y first outwards"); // XZ before Y
                case HORIZONTAL_FIRST_INWARD -> Component.text("XZ first inwards"); // Y before XZ
                case HORIZONTAL_FIRST_OUTWARD -> Component.text("XZ first outwards"); // XZ before Y
            };
        }
        players.forEach(player -> {
            player.sendTitlePart(TitlePart.TITLE, title);
            player.sendTitlePart(TitlePart.SUBTITLE, subtitle);
        });
    }

    @Override
    public ScoreboardGroup getGroup() {
        return ScoreboardGroup.FOOTER;
    }

    @Override
    public int order() {
        return Integer.MAX_VALUE;
    }

    @Override
    public String get(Region region, Player p) {
        WorldConfiguration.Misc misc = getConfig();
        switch (misc.redstoneImplementation) {
            case ALTERNATE_CURRENT:
                switch (misc.alternateCurrentUpdateOrder) {
                    case VERTICAL_FIRST_INWARD:
                        return "§eRedstone§8: §cAC §8(§7Y in§8)";
                    case VERTICAL_FIRST_OUTWARD:
                        return "§eRedstone§8: §cAC §8(§7Y out§8)";
                    case HORIZONTAL_FIRST_INWARD:
                        return "§eRedstone§8: §cAC §8(§7XZ in§8)";
                    case HORIZONTAL_FIRST_OUTWARD:
                        return "§eRedstone§8: §cAC §8(§7XZ out§8)";
                }
                return "§eRedstone§8: §cAC";
            case EIGENCRAFT:
                return null;
            case VANILLA:
            default:
                return "§eRedstone§8: §cVanilla";
        }
    }
}
