package com.infogroup.infoboard.scoreboard;

import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.scoreboard.*;

public class Board {

	private Scoreboard scoreboard;
	private Objective objective;

	/**
	 * Create a brand new objective and scoreboard
	 */
	public Board() {
		this.scoreboard = Bukkit.getScoreboardManager().getNewScoreboard();
		setup();
	}

	/**
	 * Use the current objective and scoreboard that the player is shown
	 *
	 * @param player
	 */
	public Board(Player player) {
		this.scoreboard = player.getScoreboard();
		this.objective = scoreboard.getObjective(DisplaySlot.SIDEBAR);
	}

	/**
	 * Use the current objective and scoreboard
	 *
	 * @param board
	 */
	public Board(Scoreboard board) {
		this.scoreboard = board;
		this.objective = board.getObjective(DisplaySlot.SIDEBAR);
	}

	/**
	 * Add a line to the board
	 *
	 * @param line
	 * @param row
	 */
	public void add(String line, int row) {

		row = 15 - row;
		if (!Bukkit.getVersion().contains("1.13")) {
			if (line.length() > 16) {
				addCreatingTeam(line, row);
			}
		} else {
			Score score = objective.getScore(line);
			//score.setScore(1);
			score.setScore(row);
		}
	}

	/**
	 * Add a line well creating a team to go with it(Allows for up to 48 characters
	 * in a line(Prefix and Suffix))
	 *
	 * @param line
	 * @param row
	 */
	private void addCreatingTeam(String line, int row) {
		String prefix = null, name, suffix = null;

		if (line.length() > 48) {
			line = line.substring(0, 47);
		}
		if (line.length() <= 16) {
			name = line;

		} else if (line.length() <= 32) {
			name = line.substring(0, 16);
			suffix = line.substring(16, line.length());
		} else {
			prefix = line.substring(0, 16);
			name = line.substring(16, 32);
			suffix = line.substring(32, line.length());
		}

		//TODO TEST

		if ((prefix != null) || (suffix != null)) {
			//TODO TEST
			Team team = scoreboard.getTeam(name);

			if (team == null) {
				team = scoreboard.registerNewTeam(name);
			}
			//TODO TEST
			team.addEntry(name);
			if (prefix != null) {
				team.setPrefix(prefix);
			}
			team.setSuffix(suffix);
		}
		Score score = objective.getScore(name);
		score.setScore(1);
		score.setScore(row);
	}

	/**
	 * Get the score for the line
	 *
	 * @param line
	 * @return score
	 */
	public Score getScore(String line) {
		return this.objective.getScore(line);
	}

	/**
	 * Get the scoreboard
	 *
	 * @return the scoreboard
	 */
	public Scoreboard getScoreboard() {
		return this.scoreboard;
	}

	/**
	 * Get the title
	 *
	 * @return the title
	 */
	public String getTitle() {
		return this.objective.getDisplayName();
	}

	/**
	 * Set the title
	 *
	 * @param title
	 */
	public void setTitle(String title) {
		this.objective.setDisplayName(title);
	}

	/**
	 * Remove the line from the board
	 *
	 * @param line
	 */
	public void remove(String line) {
		scoreboard.resetScores(line);
		if (scoreboard.getTeam(line) != null) {
			scoreboard.getTeam(line).unregister();
		}
	}

	/**
	 * Set up the new objective
	 */
	private void setup() {
		objective = scoreboard.registerNewObjective("InfoBoard", "dummy");
		objective.setDisplaySlot(DisplaySlot.SIDEBAR);
	}

	/**
	 * Update a line (Remove the current line, add the new line)
	 *
	 * @param line
	 * @param row
	 */
	public void update(String line, int row) {
		String name;
		if (line.length() <= 16) {
			name = line;
		} else if (line.length() <= 32) {
			name = line.substring(0, 16);
		} else {
			name = line.substring(16, 32);
		}
		if (!scoreboard.getEntries().contains(name)) {
			for (String op : scoreboard.getEntries()) {
				if (objective.getScore(op).getScore() == 15 - row) {
					remove(op);
					break;
				}
			}
			add(line, row);
		}
	}
}
@@ -1064,16 +1064,122 @@
 , row);%0A
+%09%09%09%7D else %7B%0A%09%09%09%09Score score = objective.getScore(line);%0A%09%09%09%09//score.setScore(1);%0A%09%09%09%09score.setScore(row);%0A
 %09%09%09%7D%0A%09%09%7D
@@ -1754,31 +1754,16 @@
 tring(16
-, line.length()
 );%0A%09%09%7D e
@@ -1870,23 +1870,8 @@
 g(32
-, line.length()
 );%0A%09

Build is ready for 1.13 and below