package com.loganmagnan.eventcore.scoreboard;

import aether.scoreboard.Board;
import aether.scoreboard.BoardAdapter;
import aether.scoreboard.cooldown.BoardCooldown;
import com.loganmagnan.eventcore.EventCore;
import com.loganmagnan.eventcore.managers.event.teams.Team;
import com.loganmagnan.eventcore.playerdata.PlayerData;
import com.loganmagnan.eventcore.playerdata.PlayerSettings;
import com.loganmagnan.eventcore.utils.ColorUtils;
import com.loganmagnan.eventcore.utils.Constants;
import org.bukkit.entity.Player;
import org.bukkit.scoreboard.Scoreboard;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

public class ScoreboardProvider implements BoardAdapter {

    private EventCore main = EventCore.getInstance();

    @Override
    public String getTitle(Player player) {
        return ColorUtils.getMessageType("&b&l" + this.main.getEventManager().getEvent().getName());
    }

    @Override
    public List<String> getScoreboard(Player player, Board board, Set<BoardCooldown> cooldowns) {
        PlayerData playerData = this.main.getPlayerDataManager().getPlayerData(player.getUniqueId());

        if (playerData == null) {
            return null;
        }

        PlayerSettings playerSettings = playerData.getPlayerSettings();

        if (!playerSettings.isScoreboardEnabled()) {
            return null;
        }

        switch (playerData.getPlayerState()) {
            case SPAWN:
                return this.getSpawnScoreboard(player, playerData);
        }

        return null;
    }

    @Override
    public void onScoreboardCreate(Player player, Scoreboard scoreboard) {

    }

    private List<String> getSpawnScoreboard(Player player, PlayerData playerData) {
        List<String> lines = new ArrayList<>();

        int players = 0;

        for (Team team : this.main.getEventManager().getEvent().getTeams()) {
            players += team.getPlayingPlayers().size();
        }

        lines.add(ColorUtils.getMessageType(""));
        lines.add(ColorUtils.getMessageType("&3┏ &3&l" + player.getName()));
        lines.add(ColorUtils.getMessageType("&3┝ &fOnline: &3" + this.main.getServer().getOnlinePlayers().size()));
        lines.add(ColorUtils.getMessageType("&3┕ &fPing: &3" + player.getPing()));
        lines.add(ColorUtils.getMessageType(""));
        lines.add(ColorUtils.getMessageType("&3┏ &3&lEvent Information"));
        lines.add(ColorUtils.getMessageType("&3┝ &fPlayers: &3" + (this.main.getEventManager().getEvent().isStarted() ? players : this.main.getServer().getOnlinePlayers().size())));
        lines.add(ColorUtils.getMessageType("&3┕ &fTeams: &3" + this.main.getEventManager().getEvent().getAmountOfTeams()));
        lines.add(ColorUtils.getMessageType(""));
        lines.add(ColorUtils.getMessageType("&7&o" + Constants.INFORMATION.get("server-ip-address")));

        return lines;
    }
}
