package com.tiketal.overwatch.util;

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

public class DisplayBar {

	private BossBar bar;
	
	public DisplayBar(String name, String color, String style) {
		color = color.toUpperCase();
		style = style.toUpperCase();
		try {
			bar = Bukkit.createBossBar(name, BarColor.valueOf(color), BarStyle.valueOf(style));
		} catch (Exception e) {
			bar = Bukkit.createBossBar(name, BarColor.WHITE, BarStyle.SOLID);
		}
		bar.setVisible(true);
	}
	
	public void show(Player player) {
		bar.addPlayer(player);
	}
	
	public void hide(Player player) {
		bar.removePlayer(player);
	}
	
	public void setTitle(String title) {
		bar.setTitle(title);
	}
	
	public void setVisible(boolean visible) {
		bar.setVisible(visible);
	}
	
	public void setColor(String color) {
		try {
			bar.setColor(BarColor.valueOf(color.toUpperCase()));
		} catch (Exception e) {
			bar.setColor(BarColor.WHITE);
		}
	}
	
	public void setStyle(String style) {
		try {
			bar.setStyle(BarStyle.valueOf(style.toUpperCase()));
		} catch (Exception e) {
			bar.setStyle(BarStyle.SOLID);
		}
	}
	
	public void progress(double  amt) {
		if (amt < 0) amt *= -1;
		if (amt > 1) amt = 1;
		bar.setProgress(amt);
	}
	
	public void reset() {
		for (Player player : bar.getPlayers()) {
			bar.removePlayer(player);
		}
	}
}
