
package com.gamingmesh.jobs.config;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;

import org.bukkit.Bukkit;
import org.bukkit.boss.BarColor;
import org.bukkit.boss.BarStyle;
import org.bukkit.boss.BossBar;
import org.bukkit.entity.Player;

import com.gamingmesh.jobs.Jobs;
import com.gamingmesh.jobs.container.BossBarInfo;
import com.gamingmesh.jobs.container.Job;
import com.gamingmesh.jobs.container.JobProgression;
import com.gamingmesh.jobs.container.JobsPlayer;
import com.gamingmesh.jobs.stuff.ToggleBarHandling;
import com.gamingmesh.jobs.CMILib.VersionChecker.Version;

public class BossBarManager {

    private Jobs plugin;

    public BossBarManager(Jobs plugin) {
	this.plugin = plugin;
    }

    public synchronized void ShowJobProgression(final JobsPlayer player) {
	if (Jobs.getVersionCheckManager().getVersion().isLower(Version.v1_9_R1))
	    return;

	if (player == null)
	    return;

	List<String> temp = new ArrayList<>();
	temp.addAll(player.getUpdateBossBarFor());

	for (String one : temp) {
	    for (JobProgression oneJob : player.getJobProgression()) {
		if (one.equalsIgnoreCase(oneJob.getJob().getName()))
		    ShowJobProgression(player, oneJob);
	    }
	}
	player.clearUpdateBossBarFor();
    }

    public synchronized void ShowJobProgression(final JobsPlayer player, final JobProgression jobProg) {
	if (Jobs.getVersionCheckManager().getVersion().isLower(Version.v1_9_R1))
	    return;

	String playerUUID = player.getPlayer().getUniqueId().toString();
	if (!Jobs.getGCManager().BossBarsMessageByDefault)
	    return;

	Boolean show = ToggleBarHandling.getBossBarToggle().getOrDefault(playerUUID, true);
	if (!show)
	    return;

	BossBar bar = null;
	BossBarInfo OldOne = null;
	for (BossBarInfo one : player.getBossBarInfo()) {
	    if (!one.getJobName().equalsIgnoreCase(jobProg.getJob().getName()))
		continue;

	    one.cancel();
	    bar = one.getBar();
	    OldOne = one;
	    break;
	}
	NumberFormat formatter = new DecimalFormat("#0.00");

	String message = Jobs.getLanguage().getMessage("command.stats.output",
	    "%joblevel%", Integer.valueOf(jobProg.getLevel()).toString(),
	    "%jobname%", jobProg.getJob().getChatColor() + jobProg.getJob().getName(),
	    "%jobxp%", formatter.format(Math.round(jobProg.getExperience() * 100.0) / 100.0),
	    "%jobmaxxp%", jobProg.getMaxExperience());

	if (bar == null) {
	    BarColor color = getColor(jobProg.getJob());
	    if (color == null) {
		switch (player.getBossBarInfo().size()) {
		case 1:
		    color = BarColor.GREEN;
		    break;
		case 2:
		    color = BarColor.RED;
		    break;
		case 3:
		    color = BarColor.WHITE;
		    break;
		case 4:
		    color = BarColor.YELLOW;
		    break;
		case 5:
		    color = BarColor.PINK;
		    break;
		case 6:
		    color = BarColor.PURPLE;
		    break;
		default:
		    color = BarColor.BLUE;
		    break;
		}
	    }
	    bar = Bukkit.createBossBar(message, color, BarStyle.SEGMENTED_20);
	} else
	    bar.setTitle(message);

	double percentage = jobProg.getExperience() / jobProg.getMaxExperience();
	try {
	    bar.setProgress(percentage);
	    if (OldOne == null) {
		Player target = Bukkit.getPlayer(player.getPlayer().getUniqueId());
		if (target == null)
		    return;
		bar.addPlayer(target);
		OldOne = new BossBarInfo(player.getName(), jobProg.getJob().getName(), bar);
		player.getBossBarInfo().add(OldOne);
	    }
	    bar.setVisible(true);
	} catch (NoSuchMethodError e) {
	}

	if (OldOne == null)
	    return;

	OldOne.setId(Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
	    @Override
	    public void run() {
		for (BossBarInfo one : player.getBossBarInfo()) {
		    if (!one.getPlayerName().equalsIgnoreCase(player.getName()))
			continue;

		    if (!one.getJobName().equalsIgnoreCase(jobProg.getJob().getName()))
			continue;

		    BossBar tempBar = one.getBar();
		    tempBar.setVisible(false);
		    break;
		}
		return;
	    }
	}, Jobs.getGCManager().BossBarTimer * 20L));

    }

    private static BarColor getColor(Job job) {
	if (job.getBossbar() == null)
	    return null;
	for (BarColor color : BarColor.values()) {
	    if (job.getBossbar().equalsIgnoreCase(color.name()))
		return color;
	}
	return null;
    }
}

@@ -1,10 +1,8 @@
-%0D%0A
 package 
@@ -283,42 +283,8 @@
 ar;%0D
-%0Aimport org.bukkit.entity.Player;%0D
 %0A%0D%0Ai
@@ -3187,20 +3187,8 @@
 ;%0D%0A%09
-try %7B%0D%0A%09    
 bar.
@@ -3213,177 +3213,109 @@
 tage
-);%0D%0A%09    if (OldOne == null) %7B%0D%0A%09%09Player target = Bukkit.getPlayer(player.getPlayer().getUniqueId());%0D%0A%09%09if (target == null)%0D%0A%09%09    return;%0D%0A%09%09bar.addPlayer(target);%0D%0A%09%09
+ %3E 1.0 ? 1.0 : percentage);%0D%0A%0D%0A%09if (OldOne == null) %7B%0D%0A%09    bar.addPlayer(player.getPlayer());%0D%0A%09    
 OldO
@@ -3389,17 +3389,20 @@
 bar);%0D%0A%09
-%09
+    
 player.g
@@ -3432,28 +3432,22 @@
 One);%0D%0A%09
-    %7D%0D%0A%09    
+%7D%0D%0A%0D%0A%09
 bar.setV
@@ -3464,84 +3464,8 @@
 e);%0D
-%0A%09%7D catch (NoSuchMethodError e) %7B%0D%0A%09%7D%0D%0A%0D%0A%09if (OldOne == null)%0D%0A%09    return;%0D
 %0A%0D%0A%09

Fix bossbar progress is higher than 1.0