package rip.tilly.bedwars.providers.scoreboard;

import org.bukkit.Bukkit;
import rip.tilly.bedwars.game.Game;
import rip.tilly.bedwars.game.GameTeam;
import rip.tilly.bedwars.managers.party.Party;
import rip.tilly.bedwars.managers.queue.QueueEntry;
import rip.tilly.bedwars.playerdata.PlayerState;
import rip.tilly.bedwars.utils.TimeUtils;
import rip.tilly.bedwars.utils.assemble.AssembleAdapter;
import org.bukkit.entity.Player;
import rip.tilly.bedwars.BedWars;
import rip.tilly.bedwars.playerdata.PlayerData;
import rip.tilly.bedwars.utils.CC;

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

public class ScoreboardProvider implements AssembleAdapter {

    private BedWars main = BedWars.getInstance();

    @Override
    public String getTitle(Player player) {
        return CC.translate("&d&lBedWars");
    }

    @Override
    public List<String> getLines(Player player) {
        PlayerData playerData = this.main.getPlayerDataManager().getPlayerData(player.getUniqueId());

        if (!playerData.getPlayerSettings().isScoreboardEnabled()) {
            return null;
        }

        switch (playerData.getPlayerState()) {
            case SPAWN:
            case QUEUE:
                return this.spawnScoreboard(playerData);
            case RESPAWNING:
            case PLAYING:
                return this.playingScoreboard(playerData);
            case SPECTATING:
                return null;
        }

        return null;
    }

    private List<String> spawnScoreboard(PlayerData playerData) {
        List<String> lines = new ArrayList<String>();

        lines.add(CC.scoreboardBar);

        if (this.main.getPartyManager().getParty(playerData.getUniqueId()) != null) {
            Party party = this.main.getPartyManager().getParty(playerData.getUniqueId());

            lines.add("&9Party Leader: &d" + Bukkit.getPlayer(party.getLeader()).getName());
            lines.add("&9Party Members: &d" + party.getMembers().size() + "&7/&d" + party.getLimit());
            lines.add(CC.scoreboardBar);
        }

        if (playerData.getPlayerState() == PlayerState.QUEUE) {
            QueueEntry queueEntry = this.main.getQueueManager().getQueueEntry(playerData.getUniqueId());

            if (queueEntry != null) {
                long queueTime = System.currentTimeMillis() - (this.main.getQueueManager().getPlayerQueueTime(playerData.getUniqueId()));

                String formattedQueueTime = TimeUtils.formatIntoMMSS(Math.round(queueTime / 1000L));

                lines.add("&e" + queueEntry.getGameType().getName() + " Queue");
                lines.add("&fTime: &d" + formattedQueueTime);
                lines.add(CC.scoreboardBar);
            }
        }

        lines.add("&fOnline: &d" + this.main.getServer().getOnlinePlayers().size());
        lines.add("&fPlaying: &d" + this.main.getGameManager().getPlaying());

        lines.add(" ");

        lines.add("&fLevel: &d" + playerData.getLevel());
        String finishedProgress = "";
        int notFinishedProgress = 10;
        for (int i = 0; i < playerData.getXp() * 100; i++) {
            if (i % 10 == 0) {
                finishedProgress += "⬛";

                notFinishedProgress--;
            }
        }

        String leftOverProgress = "";
        for (int i = 1; i <= notFinishedProgress; i++) {
            leftOverProgress += "⬛";
        }

        lines.add("&8" + finishedProgress + "&7" + leftOverProgress + " &7(" + ((int) (playerData.getXp() * 100)) + "%&7)");

        lines.add(" ");
        lines.add("&dtilly.rip");
        lines.add(CC.scoreboardBar);

        return CC.translate(lines);
    }

    private List<String> playingScoreboard(PlayerData playerData) {
        List<String> lines = new ArrayList<String>();
        Game game = this.main.getGameManager().getGame(playerData);
        GameTeam yourTeam = game.getTeamByName(playerData.getPlayerTeam().getName());
        GameTeam opposingTeam = game.getTeams().get(playerData.getTeamId() == 1 ? 0 : 1);

        lines.add(CC.scoreboardBar);
        lines.add("&fDuration: &d" + game.getDuration());
        lines.add(" ");
        if (yourTeam.isHasBed()) {
            lines.add("&f[" + yourTeam.getPlayerTeam().getChatColor() + yourTeam.getPlayerTeam().getSmallName() + "&f] &a&l✓ &7(YOU)");
        } else if (yourTeam.getPlayingPlayers().size() > 0) {
            lines.add("&f[" + yourTeam.getPlayerTeam().getChatColor() + yourTeam.getPlayerTeam().getSmallName() + "&f] &7" + yourTeam.getPlayingPlayers().size() + " &7(YOU)");
        } else {
            lines.add("&f[" + yourTeam.getPlayerTeam().getChatColor() + yourTeam.getPlayerTeam().getSmallName() + "&f] &c&l✗ &7(YOU)");
        }
        if (opposingTeam.isHasBed()) {
            lines.add("&f[" + opposingTeam.getPlayerTeam().getChatColor() + opposingTeam.getPlayerTeam().getSmallName() + "&f] &a&l✓");
        } else if (opposingTeam.getPlayingPlayers().size() > 0) {
            lines.add("&f[" + opposingTeam.getPlayerTeam().getChatColor() + opposingTeam.getPlayerTeam().getSmallName() + "&f] &7" + yourTeam.getPlayingPlayers().size());
        } else {
            lines.add("&f[" + opposingTeam.getPlayerTeam().getChatColor() + opposingTeam.getPlayerTeam().getSmallName() + "&f] &c&l✗");
        }
        lines.add(" ");
        lines.add("&fKills: &d" + playerData.getGameKills());
        lines.add("&fBeds Destroyed: &d");
        lines.add(" ");
        lines.add("&dtilly.rip");
        lines.add(CC.scoreboardBar);

        return CC.translate(lines);
    }

    private List<String> spectatingScoreboard(PlayerData playerData) {
        List<String> lines = new ArrayList<String>();

        lines.add(CC.scoreboardBar);
        lines.add(CC.scoreboardBar);

        return CC.translate(lines);
    }
}
