content: package eu.the5zig.mod.modules.items.system;
  
  import eu.the5zig.mod.I18n;
  import eu.the5zig.mod.The5zigMod;
  import eu.the5zig.mod.gui.Gui;
  import eu.the5zig.mod.manager.spotify.SpotifyManager;
  import eu.the5zig.mod.manager.spotify.SpotifyStatus;
  import eu.the5zig.mod.manager.spotify.SpotifyTrack;
  import eu.the5zig.mod.modules.AbstractModuleItem;
  import eu.the5zig.mod.render.Base64Renderer;
  import eu.the5zig.mod.render.RenderLocation;
  import eu.the5zig.mod.util.GLUtil;
  import eu.the5zig.mod.util.ScrollingText;
  import eu.the5zig.util.Utils;
  
  import java.util.Locale;
  
  public class Spotify extends AbstractModuleItem {
  
  	private static final Base64Renderer base64Renderer = new Base64Renderer(The5zigMod.TRACK_LOCATION, 128, 128);
  
  	static {
  		base64Renderer.setInterpolateLinear(The5zigMod.isMinecraftVersionAvailable("1.8"));
  	}
  
  	private ScrollingText scrollingTrack;
  	private ScrollingText scrollingArtist;
  
  	@Override
  	public void registerSettings() {
  		getProperties().addSetting("showSpotifyAlways", false);
  	}
  
  	@Override
  	public void render(int x, int y, RenderLocation renderLocation, boolean dummy) {
  		Gui.drawRect(x, y, x + getWidth(dummy), y + getHeight(dummy) - 2, SpotifyManager.SPOTIFY_COLOR_BACKGROUND);
  
  		SpotifyManager spotifyManager = The5zigMod.getDataManager().getSpotifyManager();
  		SpotifyStatus status = spotifyManager != null && spotifyManager.isConnected() ? spotifyManager.getStatus() : null;
  		SpotifyTrack track = status != null && status.getTrack() != null ? status.getTrack() : null;
  
  		int trackLeft = x;
  		int trackTop = y;
  		int trackRight = x + getWidth(dummy) - 4;
  		int albumSize = getHeight(dummy) - 2;
  		// Album Preview
  		if (track != null && track.hasTrackInformation()) {
  			if (track.getImage() != null && !track.getImage().equals(base64Renderer.getBase64String())) {
  				base64Renderer.setBase64String(track.getImage(), "spotify/track_" + track.getTrackInformation().getId());
  			} else if (track.getImage() == null && base64Renderer.getBase64String() != null) {
  				base64Renderer.reset();
  			}
  		} else if (base64Renderer.getBase64String() != null) {
  			base64Renderer.reset();
  		}
  		base64Renderer.renderImage(trackLeft, trackTop, albumSize, albumSize);
  
  		// Track Info
  		int infoLeft = trackLeft + albumSize + 4;
  		int infoTop = trackTop + 3;
  		int maxInfoWidth = trackRight - infoLeft;
  		String trackName = track != null ? track.getType() == SpotifyTrack.Type.ad ? I18n.translate("modules.item.spotify.ad") :
  				track.getTrackInformation() != null ? track.getTrackInformation().getName() : "" : spotifyManager != null && spotifyManager.getDisconnectError() != null ? I18n.translate(
  				"modules.item.spotify.error." + spotifyManager.getDisconnectError().toString().toLowerCase(Locale.ROOT)) : null;
  		String artistName = track != null && track.getArtistInformation() != null ? track.getArtistInformation().getName() : null;
  
  		if (trackName != null && (scrollingTrack == null || !trackName.equals(scrollingTrack.getText()))) {
  			scrollingTrack = new ScrollingText(trackName, maxInfoWidth, 8, SpotifyManager.SPOTIFY_COLOR_BACKGROUND, 0xaaaaaa);
  		} else if (scrollingTrack != null && trackName == null) {
  			scrollingTrack = null;
  		}
  		if (artistName != null && (scrollingArtist == null || !artistName.equals(scrollingArtist.getText()))) {
  			scrollingArtist = new ScrollingText(artistName, maxInfoWidth, 6, SpotifyManager.SPOTIFY_COLOR_BACKGROUND, 0xaaaaaa);
  		} else if (scrollingArtist != null && artistName == null) {
  			scrollingArtist = null;
  		}
  		if (scrollingTrack != null) {
  			scrollingTrack.setChild(scrollingArtist);
  			scrollingTrack.setScale(getRenderSettings().getScale());
  			scrollingTrack.render(infoLeft, infoTop);
  		}
  		if (scrollingArtist != null) {
  			scrollingArtist.setParent(scrollingTrack);
  			scrollingArtist.setScale(getRenderSettings().getScale());
  			scrollingArtist.render(infoLeft, infoTop + 9);
  		}
  
  		// position
  		int positionLineTop = y + getHeight(dummy) - 8;
  		int positionLineBottom = positionLineTop + 2;
  		int positionLineLeft = infoLeft + 11;
  		int positionLineRight = trackRight - 11;
  		int positionWidth = positionLineRight - positionLineLeft;
  		Gui.drawRect(positionLineLeft, positionLineTop, positionLineRight, positionLineBottom, SpotifyManager.SPOTIFY_COLOR_SECOND);
  		double playingPosition = status != null ? status.getPlayingPosition() * 1000 : 0;
  		long millisSinceUpdate = status != null && status.isPlaying() ? (int) (System.currentTimeMillis() - status.getServerTime()) : 0;
  		double trackPosition = playingPosition + millisSinceUpdate;
  		long trackLength = track != null ? track.getLength() * 1000 : 1000;
  		double trackPercentage = trackPosition / trackLength;
  		Gui.drawRect(positionLineLeft, positionLineTop, positionLineLeft + (trackPercentage * positionWidth), positionLineBottom, SpotifyManager.SPOTIFY_COLOR_ACCENT);
  		drawScaledString(Utils.convertToClock((long) trackPosition), infoLeft, positionLineTop, 0xaaaaaa, 0.35f);
  		drawScaledString(Utils.convertToClock(trackLength), positionLineRight + 2, positionLineTop, 0xaaaaaa, 0.35f);
  	}
  
  	private void drawScaledString(String string, int x, int y, int color, float scale) {
  		GLUtil.pushMatrix();
  		GLUtil.translate(x, y, 1);
  		GLUtil.scale(scale, scale, scale);
  		The5zigMod.getVars().drawString(string, 0, 0, color);
  		GLUtil.popMatrix();
  	}
  
  	@Override
  	public boolean shouldRender(boolean dummy) {
  		if (dummy) {
  			return true;
  		}
  		if (((Boolean) getProperties().getSetting("showSpotifyAlways").get())) {
  			return true;
  		}
  		SpotifyManager manager = The5zigMod.getDataManager().getSpotifyManager();
  		return manager != null && manager.getStatus() != null && manager.getStatus().getTrack() != null && manager.getStatus().getTrack().hasTrackInformation();
  	}
  
  	@Override
  	public int getWidth(boolean dummy) {
  		return 120;
  	}
  
  	@Override
  	public int getHeight(boolean dummy) {
  		return 28;
  	}
  }
  