// ==PREPROCESSOR==
// @name "DarkOne4Mod - Display Panel"
// @version "1.0 build20160929"
// @author "super-gau and tedGo, includes partial codes by fbuser, Br3tt and T.P Wang"
// @import "%fb2k_path%themes\DarkOne4Mod\Others\JScripts\DarkOne4Mod - Global Script.js"
// ==/PREPROCESSOR==

// ----- VARIABLES -----
var t_rem = window.GetProperty("Remain Time on", false);
var fileinfo, g_trackinfo, d_font, t_font, f_size, t_size;
var g_timer = v_timer = null;
var v_change = false;
var p_backcol = RGB(31, 31, 31);

// ----- COLOURS -----
var d_arr = new Array("Blue", "Green", "Light Blue", "Red", "White", "Yellow");
var c_arr = new Array(RGB(41, 143, 204), RGB(6, 176, 37), RGB(191, 223, 255), RGB(255, 0, 0), RGB(255, 255, 255), RGB(255, 223, 63));

function InitColours() {
	d_col = window.GetProperty("Display Colour", 0);
	g_acol = c_arr[d_col];
	g_bcol = combColours(p_backcol, c_arr[4], 0.02);
}

InitColours();

// ----- CREATE HELPERS -----
String.prototype.repeat = function(n) {
	return (new Array(n + 1)).join(this);
}

function pad(x, y, z) {
	z || (z = ' ');
	return x.length < y ? x + z.repeat(y - x.length) : x;
}

function pad_right(x, y, z) {
	z || (z = ' ');
	return x.length < y ? z.repeat(y - x.length) + x : x
}

function TimeFmt(t) {
	var zpad = function(n) {
		var str = n.toString();
		return str.length < 2 ? "0" + str : str;
	}

	var h = Math.floor(t / 3600);
	t -= h * 3600;
	var m = Math.floor(t / 60);
	t -= m * 60;
	var s = Math.floor(t);

	return zpad(h) + ":" + zpad(m) + ":" + zpad(s);
}

// ----- CREATE INFO -----
Info.prototype.setColours = function() {
	var metadb = fb.GetNowPlaying();

	if (metadb) {
		fileinfo = metadb.GetFileInfo();
		this.Colours[0] = fileinfo.InfoValue(fileinfo.InfoFind("encoding")) == "lossless" ? g_acol : g_bcol;
		this.Colours[1] = fileinfo.InfoValue(fileinfo.InfoFind("encoding")) == "lossless" ? g_bcol : g_acol;
		this.Colours[2] = fileinfo.InfoValue(fileinfo.InfoFind("md5")) ? g_acol : g_bcol;
		this.Colours[3] = fb.TitleFormat("[%replaygain_track_gain%]").Eval() ? g_acol : g_bcol;
		this.Colours[4] = fileinfo.MetaValue(fileinfo.MetaFind("tracknumber"), 0) ? ui_btntxtcol : g_bcol;
		this.Colours[5] = fileinfo.MetaValue(fileinfo.MetaFind("totaltracks"), 0) ? ui_btntxtcol : g_bcol;
		this.Colours[6] = ui_btntxtcol;
	} else for (var i = 0; i < this.Colours.length; i++) this.Colours[i] = g_bcol;
}

Info.prototype.setTrackNo = function() {
	var metadb = fb.GetNowPlaying();
	var a, b, c, d;

	if (metadb) {
		a = fb.TitleFormat("[$num(%tracknumber%,2)]").Eval();
		b = fb.TitleFormat("[$num(%totaltracks%,2)]").Eval();
		c = a ? a : "";
		d = b ? b : "";
		this.TrackNo = c;
		this.TotalNo = d;
		g_trackinfo = fb.PlaybackLength <= 0 ? 2 : metadb.RawPath.indexOf("FOO_LASTFM") == 0 ? 1 : 0;
	} else {
		this.TrackNo = "";
		this.TotalNo = "";
	}
}

Info.prototype.setPBTime = function() {
	this.Elapse = TimeFmt(fb.PlaybackTime);
	this.Remain = fb.PlaybackLength < 0 ? "-- : -- : --" : TimeFmt(fb.PlaybackLength - fb.PlaybackTime);
}

Info.prototype.setBitrate = function() {
	this.Bitrate = pad_right(fb.TitleFormat("%bitrate%").Eval(), 5);
}

Info.prototype.setInfo = function() {
	this.setColours();
	this.setTrackNo();
	this.setPBTime();
	this.setBitrate();
}

function Info() {
	this.Colours = new Array(7);
	this.setInfo();
}

info = new Info();

// ----- CREATE MENU -----
function CustomMenu(x, y) {
	var a = window.CreatePopupMenu();
	var idx;

	for (var i = 0; i < d_arr.length; i++) {
		a.AppendMenuItem(0, 1 + i, d_arr[i]);
	}
	a.CheckMenuRadioItem(1, 6, d_col + 1);

	a.AppendMenuSeparator();
	a.AppendMenuItem(0, 7, "Properties");
	a.AppendMenuItem(0, 8, "Configure...");

	idx = a.TrackPopupMenu(x, y);

	switch (true) {
		case (idx >= 1 && idx <= 6):
			window.SetProperty("Display Colour", idx - 1);
			InitColours();
			info.setColours();
			window.Repaint();
			fb.RunMainMenuCommand("DarkOne4Mod - " + d_arr[d_col]);
			break;

		case (idx == 7):
			window.ShowProperties();
			break;

		case (idx == 8):
			window.ShowConfigure();
			break;
	}

	a.Dispose();
}

// ----- DRAW -----
function on_paint(gr) {
	!window.IsTransparent && gr.FillSolidRect(0, 0, ww, wh, p_backcol);

	gr.SetInterpolationMode(7);

	gr.DrawRect(1, 0, ww / 4 - 2, ww / 40, ww / 400, info.Colours[0]);
	gr.DrawRect(ww / 4 + 1, 0, ww / 4 - 2, ww / 40, ww / 400, info.Colours[1]);
	gr.DrawRect(ww / 2 + 1, 0, ww / 4 - 2, ww / 40, ww / 400, info.Colours[2]);
	gr.DrawRect(ww * 3 / 4 + 1, 0, ww / 4 - 2, ww / 40, ww / 400, info.Colours[3]);

	d_font = gdi.Font("Arial Black", f_size);

	gr.GdiDrawText("LOSSLESS", d_font, info.Colours[0], 1, 0, ww / 4 - 2, ww / 40, 5);
	gr.GdiDrawText("LOSSY", d_font, info.Colours[1], ww / 4 + 1, 0, ww / 4 - 2, ww / 40, 5);
	gr.GdiDrawText("AUDIO MD5", d_font, info.Colours[2], ww / 2 + 1, 0, ww / 4 - 2, ww / 40, 5);
	gr.GdiDrawText("REPLAYGAIN", d_font, info.Colours[3], ww *3 / 4 + 1, 0, ww / 4 - 2, ww / 40, 5);

	gr.GdiDrawText("TRACK", d_font, info.Colours[4], 0, ww * 9 / 200, ww / 10, ww / 50, 0);
	gr.GdiDrawText("TOTAL", d_font, info.Colours[5], ww * 41 / 200, ww * 9 / 200, ww / 10, ww / 50, 0);
	gr.GdiDrawText("ELAPSED", d_font, v_change || t_rem ? g_bcol : info.Colours[6], ww * 41 / 100, ww * 9 / 200, ww * 9 / 100, ww / 50, 0);
	gr.GdiDrawText("TIME", d_font, v_change ? g_bcol : info.Colours[6], ww * 41 / 80, ww * 9 / 200, ww / 20, ww / 50, 1);
	gr.GdiDrawText("REMAINING", d_font, v_change || !t_rem ? g_bcol : info.Colours[6], ww * 229 / 400, ww * 9 / 200, ww * 23 / 200, ww / 50, 2);
	gr.GdiDrawText("VOLUME", d_font, v_change ? ui_btntxtcol : g_bcol, ww * 39 / 50, ww * 9 / 200, ww / 12, ww / 50, 2);
	gr.GdiDrawText("KBPS", d_font, v_change ? g_bcol : info.Colours[6], ww * 22 / 25, ww * 9 / 200, ww * 4 / 75, ww / 50, 2);

	t_font = gdi.Font("Microsoft Sans Serif", t_size);

	if (g_trackinfo == 0) {
		gr.GdiDrawText(info.TrackNo, t_font, g_acol, 0, ww * 3 / 50, ww * 41 / 200, ww / 9, 0);
		gr.GdiDrawText(info.TotalNo, t_font, g_acol, ww * 41 / 200, ww * 3 / 50, ww * 41 / 200, ww / 9, 0);
	} else {
		var noTrack = g_trackinfo == 1 ? "last.fm" : "Stream";
		fb.IsPlaying && gr.GdiDrawText(noTrack, t_font, g_acol, 0, ww * 3 / 50, ww * 41 / 100, ww / 9, 0);
	}

	if (v_change) gr.GdiDrawText(fb.Volume.toFixed(2) + " dB", t_font, g_acol, ww * 57 / 100, ww * 3 / 50, ww * 9 / 25, ww / 9, 2);
	else {
		if (fb.IsPlaying) {
			var t_fmt = fb.IsPaused ? "-Paused-" : t_rem ? info.Remain : info.Elapse;
			gr.GdiDrawText(t_fmt, t_font, g_acol, ww * 41 / 100, ww * 3 / 50, ww * 7 / 25, ww / 9, 1);
			gr.GdiDrawText(info.Bitrate, t_font, g_acol, ww * 147 / 200, ww * 3 / 50, ww / 5, ww / 9, 2);

		}
	}

	g_signs && gr.DrawImage(g_signs, Math.floor(ww - ww * 9 / 200), ww / 20, ww * 9 / 200, ww * 3 / 100, 1944, fb.StopAfterCurrent ? d_col * 66 : 264, 54, 36, 0, fb.StopAfterCurrent ? 255 : 5);
	g_signs && gr.DrawImage(g_signs, Math.floor(ww - ww * 9 / 200), ww * 39 / 400, ww * 9 / 200, ww * 3 / 100, 2004 + plman.PlaybackOrder * 60, fb.IsPlaying ? d_col * 66 : 264, 54, 36, 0, fb.IsPlaying ? 255 : 5);
}

// ----- MOUSE ACTIONS -----
function on_mouse_rbtn_up(x, y) {
	CustomMenu(x, y);
	return true;
}

// ----- EVENTS -----
function on_size() {
	ww = window.Width;
	wh = window.Height;
	f_size = ww * 7 / 400;
	t_size = ww * 29 / 400;
}

function on_volume_change(val) {
	if (v_timer) {
		window.ClearTimeout(v_timer);
		v_timer = null;
	}

	v_timer = window.SetTimeout(
		function () {
			window.RepaintRect(Math.floor(ww * 41 / 100), Math.floor(ww * 9 / 200), ww * 105 / 200 + 2, ww * 9 / 100 + 2);
			window.ClearTimeout(v_timer);
			v_timer = null;
			v_change = false;
		}, 3000);

	v_change = true;
	window.RepaintRect(Math.floor(ww * 41 / 100), Math.floor(ww * 9 / 200), ww * 105 / 200 + 2, ww * 9 / 100 + 2);
}

function on_playback_order_changed(new_order) {
	window.RepaintRect(Math.floor(ww - ww * 9 / 200), Math.floor(ww * 39 / 400), ww * 9 / 200 + 2, ww * 3 / 100 + 3 );
}

function on_playback_time(time) {
	info.setPBTime();
	window.RepaintRect(Math.floor(ww * 41 / 100), Math.floor(ww * 3 / 50), ww * 71 / 200 + 2, ww / 9 + 2);
}

function on_playback_dynamic_info() {
	info.setBitrate()
	window.RepaintRect(Math.floor(ww * 147 / 200), Math.floor(ww * 3 / 50), ww / 5 + 2, ww / 9 + 2);
}

function on_playback_edited() {
	info.setInfo();
	window.Repaint();
}

function on_playback_new_track(metadb) {
	info.setInfo();
	window.Repaint();
}

function on_playback_pause(state) {
	window.RepaintRect(Math.floor(ww * 41 / 100), Math.floor(ww * 3 / 50), ww * 71 / 200 + 2, ww / 9 + 2);
}

function on_playback_stop(reason) {
	if (fileinfo) fileinfo.Dispose();

	if (reason != 2) {
		info.setInfo();
		fb.RunMainMenuCommand("ELPlaylist/Redraw");
	}

	window.Repaint();
}

function on_playlist_stop_after_current_changed(state) {
	window.RepaintRect(Math.floor(ww - ww * 9 / 200), Math.floor(ww / 20), ww * 9 / 200 + 2, ww * 3 / 100 + 2);
}

function on_notify_data(name, info) {
	if (name == "remTime") {
		window.SetProperty("Remain Time on", info == true ? true : false);
		t_rem = window.GetProperty("Remain Time on");
		window.RepaintRect(Math.floor(ww * 41 / 100), Math.floor(ww * 9 / 200), ww * 15 / 50 + 2, ww * 9 / 100 + 2);
	}
}

function on_script_unload() {
	if (fileinfo) fileinfo.Dispose();
	g_signs && g_signs.Dispose();
}