// ==PREPROCESSOR==
// @name "DarkOne4Mod - Cover Panel"
// @version "1.0 build20160929"
// @author "tedGo, includes partial code by T.P Wang"
// @import "%fb2k_path%themes\DarkOne4Mod\Others\JScripts\DarkOne4Mod - Global Script.js"
// ==/PREPROCESSOR==

// ----- VARIABLES -----
var g_font = gdi.Font("Segoe UI", 50, 0);
var g_textrender = gdi.CreateStyleTextRender();
var g_img = descr_timer = cycle_timer = null;
var g_active = false;
var g_fade = 255;
var g_state = 0;
var metadb;

// ----- CREATE OPTIONS -----
var a_arr = new Array("Picture 1", "Picture 2", "Picture 3", "Picture 4", "Picture 5");
var b_arr = new Array("Front cover", "Back cover", "Disc", "Icon", "Artist");

function artType() {
	at = window.GetProperty("Enable Custom Pictures", false);
	c_arr = at ? a_arr : b_arr;
}

artType();

function aspectRatio() {
	ar = window.GetProperty("Aspect ratio", 0);
	if (typeof ar < 0 || ar > 2) ar = 0;
	return ar;
}

aspectRatio();

// ----- GET PICTURE -----
var a_img = gdi.Image(imgPath + "DarkOne.png");
var b_img = gdi.Image(imgPath + "Radio.png");
var c_img = gdi.Image(imgPath + "AudioCD.png");
var d_img = gdi.Image(imgPath + "NoImage.png");

var e_arr = [];
e_arr[0] = gdi.Image(imgPath + "NoFront.png");
e_arr[1] = gdi.Image(imgPath + "NoBack.png");
e_arr[2] = gdi.Image(imgPath + "NoDisc.png");
e_arr[3] = gdi.Image(imgPath + "NoIcon.png");
e_arr[4] = gdi.Image(imgPath + "NoArtist.png");

var f_arr = [];
f_arr[0] = window.GetProperty(a_arr[0],"");
f_arr[1] = window.GetProperty(a_arr[1],"");
f_arr[2] = window.GetProperty(a_arr[2],"");
f_arr[3] = window.GetProperty(a_arr[3],"");
f_arr[4] = window.GetProperty(a_arr[4],"");

var g_ext = window.GetProperty("File extension order", "jpg|png|gif|bmp|tif");
var g_arr = g_ext.split("|");

var g_art = null;

function getRightImage(switchstate) {
	metadb = fb.GetNowPlaying();

	if (g_art) {
		g_img.Dispose();
		g_art = null;
	}

	if (metadb) {
		var f_img = null;
		if (fb.PlaybackLength <= 0) {
			f_img = b_img;
			g_active = false;
		} else if (metadb.RawPath.indexOf("cdda://") == 0) {
			f_img = c_img;
			g_active = false;
		} else {
			var old_state = g_state;
			do {
				switchstate && switchState();
				if (at) {
					var arr = utils.Glob(fb.TitleFormat(f_arr[g_state]).Eval() + ".*").toArray();
					var g_break = false;
					for (var n = 0; n < g_arr.length && !g_break; n++) {
						for (var i = 0; i < arr.length; i++) {
							var re = new RegExp("\." + g_arr[n] + "$", "i");
							if (arr[i].match(re)) {
								g_art = gdi.Image(arr[i]);
								g_break = true;
								break;
							}
						}
					}
					f_img = g_art;
				} else {
					g_art = utils.GetAlbumArtV2(metadb, g_state);
					f_img = g_art;
				}
			}
			while (switchstate && old_state != g_state && !f_img);
			g_active = true;
		}
	} else {
		f_img = a_img;
		g_active = false;
	}

	if (f_img) g_img = f_img;
	else if (!switchstate) g_img = at ? d_img : e_arr[g_state];

	return g_img ? true : false;
}

getRightImage(false);

// ----- CREATE ACTIONS -----
function getTimer() {
	if (descr_timer) {
		window.ClearTimeout(descr_timer);
		descr_timer = null;
	}

	descr_timer = window.SetTimeout(
		function () {
			window.Repaint();
			window.ClearTimeout(descr_timer);
			descr_timer = null;
		}, 1500);

	window.Repaint();
}

function switchState() {
	if (g_state == 4) g_state = 0;
	else g_state++;
}

function switchType() {
	window.SetProperty("Enable Custom Pictures", at ? false : true);
	artType();
	g_state = 0;
	getRightImage(false);
	if (g_active && !g_art) getRightImage(true);
	getTimer();
}

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

	for (var i = 0; i < c_arr.length; i++) {
		b.AppendMenuItem(0, 101 + i, c_arr[i]);
	}
	b.CheckMenuRadioItem(101, 105, g_state + 101);

	a.AppendMenuItem(0, 1, "Keep aspect ratio");
	a.AppendMenuItem(0, 2, "Noexpansion");
	a.AppendMenuItem(0, 3, "Stretch");
	a.CheckMenuRadioItem(1, 3, ar + 1);
	a.AppendMenuSeparator();
	a.AppendMenuItem(0, 4, at ? "Album art" : "Custom pictures");
	a.AppendMenuSeparator();
	!ac && b.AppendTo(a, 0 | 16, at ? "Pictures" : "Artwork type");
	!ac && a.AppendMenuSeparator();
	a.AppendMenuItem(ac ? 8 : 0, 5, "Auto cycle image");
	ac && a.AppendMenuItem(ct ? 8 : 0, 6, "Faded transition");
	a.AppendMenuSeparator();
	a.AppendMenuItem(0, 7, "Manage attached pictures");
	a.AppendMenuItem(0, 8, "Remove all pictures");
	a.AppendMenuSeparator();
	a.AppendMenuItem(0, 9, "Properties");
	a.AppendMenuItem(0, 10, "Configure...");

	idx = a.TrackPopupMenu(x, y);

	switch (true) {
		case (idx >= 101 && idx <= 105):
			g_state = idx - 101;
			getRightImage(false);
			getTimer();
			break;

		case (idx >= 1 && idx <= 3):
			window.SetProperty("Aspect ratio", idx - 1);
			aspectRatio();
			window.Repaint();
			break;

		case (idx == 4):
			switchType();
			break;

		case (idx == 5):
			window.SetProperty("Auto Cycle Image", ac ? false : true);
			ac = window.GetProperty("Auto Cycle Image");
			ac && !g_art && getRightImage(true);
			ac && window.Repaint();
			break;

		case (idx == 6):
			window.SetProperty("Auto Cycle Transition Fader on", ct ? false : true);
			ct = window.GetProperty("Auto Cycle Transition Fader on");
			break;

		case (idx == 7):
			fb.RunContextCommand("Manage attached pictures");
			break;

		case (idx == 8):
			fb.RunContextCommand("Remove all pictures");
			break;

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

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

	a.Dispose();
}

// ----- DRAW -----
var overlay_active = window.GetProperty("Back Overlay: Activate", true);
var overlay_colour = window.GetProperty("Back Overlay: Colour", "0-61-98-255");
var g_acol = CustomColour(overlay_colour);

var descr_tcolour = window.GetProperty("Description: Text Colour", "41-143-204-255");
var g_bcol = CustomColour(descr_tcolour);

var descr_bcolour = window.GetProperty("Description: Back Colour", "10-36-51-255");
var g_ccol = CustomColour(descr_bcolour);

function on_paint(gr) {
	!window.IsTransparent && gr.FillSolidRect(0, 0, ww, wh, ui_backcol);
	fb.IsPlaying && overlay_active && gr.FillSolidRect(0, 0, ww, wh, g_acol);

	if (g_img) {
		var w, h, x, y;
		if (ar == 0 || ar == 1 && (g_img.Width > ww || g_img.Height > wh)) {
			var img_scale = Math.min(ww / g_img.Width, wh / g_img.Height);
			w = g_img.Width * img_scale;
			h = g_img.Height * img_scale;
			x = (ww - w) / 2;
			y = (wh - h) / 2;
		} else if (ar == 1) {
			w = g_img.Width;
			h = g_img.Height;
			x = (ww - w) / 2;
			y = (wh - h) / 2;
		} else {
			w = ww;
			h = wh;
			x = y = 0;
		}
		gr.DrawImage(g_img, x, y, w, h, 0, 0, g_img.Width, g_img.Height, 0, Math.abs(g_fade));
	}

	if (descr_timer) {
		gr.SetTextRenderingHint(5);
		gr.SetSmoothingMode(4);
		gr.SetInterpolationMode(7);
		gr.FillGradRect(-1, -1, ww + 1, wh + 1, 90, 0, g_ccol);
		g_textrender.GlowText(g_bcol, RGBA(0, 128, 255, 4), 32);
		g_textrender.EnableShadow(true);
		g_textrender.ResetShadow();
		g_textrender.RenderStringRect(gr, c_arr[g_state], g_font, 0, wh - 90, ww, 60, 0x11005000);
	}
}

// ----- MOUSE ACTIONS -----
function on_mouse_move(x, y) {
	!ac && g_active && window.SetCursor(32649);
}

function on_mouse_lbtn_down(x, y) {
	if (!ac && g_active) {
		getRightImage(true);
		getTimer();
	}
}

function on_mouse_mbtn_down(x, y) {
	g_active && switchType();
}

function on_mouse_rbtn_up(x, y) {
	if (g_active) {
		CustomMenu(x, y);
		return true;
	}
}

function on_mouse_wheel(step) {
	if (!ac && g_active) {
		g_state -= step;
		if (g_state < 0 ) g_state = 4;
		else if (g_state > 4) g_state = 0;
		getRightImage(false);
		getTimer();
	}
}

// ----- EVENTS -----
var ac = window.GetProperty("Auto Cycle Image", false);
var ci = window.GetProperty("Auto Cycle Interval in s", 15);
var ct = window.GetProperty("Auto Cycle Transition Fader on", false);

function on_size() {
	ww = window.Width;
	wh = window.Height;
}

function on_playback_new_track(metadb) {
	getRightImage(false);
	if (ac && g_active && !g_art) getRightImage(true);
	window.Repaint();
}

function on_playback_time(time) {
	if (ac && g_active && g_art && time > 1 && Math.round(time % ci) == 1) {
		if (ct) {
			if (cycle_timer) {
				window.ClearInterval(cycle_timer);
				cycle_timer = null;
			}
			cycle_timer = window.SetInterval(
				function () {
					g_fade -= 51;
					if (g_fade <= -255) {
						g_fade = 255;
						window.ClearInterval(cycle_timer);
						cycle_timer = null;
					}
					if (g_fade == 0) getRightImage(true);
					window.Repaint();
				}, 50);            
		} else {
			getRightImage(true);
			window.Repaint();
		}
	}
}

function on_playback_stop(reason) {
	if (cycle_timer) {
		window.ClearInterval(cycle_timer);
		cycle_timer = null;
		g_fade = 255;
	}

	if (descr_timer) {
		window.ClearTimeout(descr_timer);
		descr_timer = null;
	}

	if (reason != 2) {
		getRightImage(false);
		window.Repaint();
	}
}