<%inherit file="base.html" />
<%!
	from headphones import db, helpers
	myDB = db.DBConnection()
%>

<%def name="headerIncludes()">

	<div id="subhead_container">
		<div id="back_to_previous_link">
			<a href="artistPage?ArtistID=${album['ArtistID']}" class="back">&laquo; Back to ${album['ArtistName']}</a>
		</div>
		<div id="subhead_menu">

			<a id="menu_link_delete" href="deleteAlbum?AlbumID=${album['AlbumID']}&ArtistID=${album['ArtistID']}"><i class="fa fa-trash-o"></i> Delete Album</a>
			%if album['Status'] == 'Skipped' or album['Status'] == 'Ignored':
			<a id="menu_link_wanted" href="javascript:void(0)" onclick="doAjaxCall('queueAlbum?AlbumID=${album['AlbumID']}&ArtistID=${album['ArtistID']}&new=False', $(this),true)" data-success="'${album['AlbumTitle']}' added to queue"><i class="fa fa-heart"></i> Mark Album as Wanted</a>
			%elif album['Status'] == 'Wanted':
			<a id="menu_link_check" href="javascript:void(0)" onclick="doAjaxCall('queueAlbum?AlbumID=${album['AlbumID']}&ArtistID=${album['ArtistID']}&new=True', $(this));" data-success="Forced checking successful"><i class="fa fa-search"></i> Force Check</a>
			<a id="menu_link_skipped" href="javascript:void(0)" onclick="doAjaxCall('unqueueAlbum?AlbumID=${album['AlbumID']}&ArtistID=${album['ArtistID']}', $(this),true);" data-success="'${album['AlbumTitle']}' marked as Skipped"><i class="fa fa-step-forward"></i> Mark Album as Skipped</a>
			%else:
			<a id="menu_link_retry" href="javascript:void(0)" onclick="doAjaxCall('queueAlbum?AlbumID=${album['AlbumID']}&ArtistID=${album['ArtistID']}&new=False', $(this),true);" data-success="Retrying the same version of '${album['AlbumTitle']}'"><i class="fa fa-refresh"></i> Retry Download</a>
			<a id="menu_link_new" href="javascript:void(0)" onclick="doAjaxCall('queueAlbum?AlbumID=${album['AlbumID']}&ArtistID=${album['ArtistID']}&new=True', $(this),true);" data-success="Looking for a new version of '${album['AlbumTitle']}'"><i class="fa fa-download"></i> Try New Version</a>
			%endif		
			<a class="menu_link_edit" id="album_chooser" href="javascript:void(0)"><i class="fa fa-pencil"></i> Choose Alternate Release</a>
				<div id="dialog" title="Choose an Alternate Release" style="display:none" class="configtable">
					<div class="links">
						<%
							alternate_albums = myDB.select("SELECT * from allalbums WHERE AlbumID=? ORDER BY ReleaseDate ASC", [album['AlbumID']])
						%>
						%if not alternate_albums:
							<p>No alternate releases found. Try refreshing the artist (if the artist is being refreshed, please wait until it's finished)</p>
							<h2><a id="refresh_artist" onclick="doAjaxCall('refreshArtist?ArtistID=${album['ArtistID']}', $(this)), true" href="javascript:void(0)" data-success="'${album['ArtistName']}' is being refreshed">Refresh Artist</a></h2>
						%else:
							%for alternate_album in alternate_albums:
								<%
									track_count = len(myDB.select("SELECT * from alltracks WHERE ReleaseID=?", [alternate_album['ReleaseID']]))
									mb_link = "http://musicbrainz.org/release/" + alternate_album['ReleaseID']
									have_track_count = len(myDB.select("SELECT * from alltracks WHERE ReleaseID=? AND Location IS NOT NULL", [alternate_album['ReleaseID']]))
									if alternate_album['AlbumID'] == alternate_album['ReleaseID']:
										alternate_album_name = "Headphones Default Release (" + str(alternate_album['ReleaseDate']) + ") [" + str(have_track_count) + "/" + str(track_count) + " tracks]"
									else:
										alternate_album_name = alternate_album['AlbumTitle'] + " (" + alternate_album['ReleaseCountry'] + ", " + str(alternate_album['ReleaseDate']) + ", " + alternate_album['ReleaseFormat'] + ") [" + str(have_track_count) + "/" + str(track_count) + " tracks]"
									
								%>
								<a href="javascript:void(0)" onclick="doAjaxCall('switchAlbum?AlbumID=${album['AlbumID']}&ReleaseID=${alternate_album['ReleaseID']}', $(this), 'table');" data-success="Switched release to: ${alternate_album_name}">${alternate_album_name}</a><a href="${mb_link}" target="_blank">MB</a><br>
							%endfor
						%endif
					</div>
				</div>
			<a class="menu_link_edit" id="edit_search_term" href="javascript:void(0)"><i class="fa fa-pencil"></i> Edit Search Term</a>
			<div id="dialog2" title="Enter your own search term for this album" style="display:none" class="configtable">
				<form action="editSearchTerm" method="GET" id="editSearchTerm">
					<input type="hidden" name="AlbumID" value="${album['AlbumID']}">
					<div class="row">
						<%
							if not album['SearchTerm']:
								search_term = ""
							else:
								search_term = album['SearchTerm']
						%>
						<input type="text" value="${search_term}" name="SearchTerm" size="40" />
					</div>
					<input type="button" value="Save changes" onclick="doAjaxCall('editSearchTerm',$(this),'tabs',true);return false;" data-success="Search term updated"/>
				</form>
			</div>
			<a class="menu_link_edit" id="choose_specific_download" href="javascript:void(0)" onclick="getAvailableDownloads()"><i class="fa fa-search"></i> Choose Specific Download</a>
			<div id="choose_specific_download_dialog" title="Choose a specific download for this album" style="display:none" class="configtable">
				<table class="display" id="downloads_table">
					<thead>
						<tr>
							<th id="title">Title</th>
							<th id="size">Size</th>
							<th id="provider">Provider</th>
							<th id="kind">Kind</th>
							<th id="matches" title="Matches quality and/or seed settings.">Matches</th>
						</tr>
					</thead>
					<tbody id="downloads_table_body">
					</tbody>
				</table>
			</div>
		</div>
	</div>
</%def>

<%def name="body()">
	<div class="table_wrapper">
		<div id="albumheader" class="clearfix">
			<div id="albumImg">
				<img height="200" alt="" class="albumArt" src="artwork/album/${album['AlbumID']}">
			</div>

            <h1 id="albumname">
                <a href="http://musicbrainz.org/release-group/${album['AlbumID']}" id="albumnamelink">${album['AlbumTitle']}</a>
            </h1>

            <h2 id="artistname">
                <a href="http://musicbrainz.org/artist/${album['ArtistID']}" id="artistnamelink">${album['ArtistName']}</a>
            </h2>

			<%
				totalduration = myDB.action("SELECT SUM(TrackDuration) FROM tracks WHERE AlbumID=?", [album['AlbumID']]).fetchone()[0]
				totaltracks = len(myDB.select("SELECT TrackTitle from tracks WHERE AlbumID=?", [album['AlbumID']]))
				try:
					albumduration = helpers.convert_milliseconds(totalduration)
				except:
					albumduration = 'n/a'
			
			%>
			<div class="albuminfo">
				<div id="albumInfo"></div>
				<ul>
					<li>Tracks: <span>${totaltracks}</span></li> 
					<li>Duration: <span>${albumduration}</span></li>
				</ul>
			</div>
		</div>
		<div id="track_wrapper">
			<table class="display" id="track_table">
				<thead>
					<tr>
						<th id="number">#</th>
						<th id="name">Track Title</th>
						<th id="duration">Duration</th>
						<th id="location">Local File</th>
						<th id="bitrate">Bit Rate</th>
						<th id="format">Format</th>
					</tr>
				</thead>
				<tbody>
				%for track in tracks:
					<%
						if track['Location']:
							grade = 'A'
							location = track['Location']
						else:
							grade = 'X'
							location = ''
							
						if track['BitRate']:
							bitrate = str(track['BitRate']/1000) + ' kbps'
						else:
							bitrate = ''

						try:
							trackduration = helpers.convert_milliseconds(track['TrackDuration'])
						except:
							trackduration = 'n/a'

						if not track['Format']:
							format = ''
						else:
							format = track['Format']
					%>
					<tr class="grade${grade}">
						<td id="number">${track['TrackNumber']}</td>
						<td id="name">${track['TrackTitle']}</td>
						<td id="duration">${trackduration}</td>
						<td id="location">${location}</td>
						<td id="bitrate">${bitrate}</td>
						<td id="format">${format}</td>
					</tr>
				%endfor
				<%
				unmatched = myDB.select('SELECT * from have WHERE ArtistName LIKE ? AND AlbumTitle LIKE ? AND Matched = "Failed" ORDER BY CAST(TrackNumber AS INTEGER)', [album['ArtistName'], album['AlbumTitle']])
				%>
				%if unmatched:
					 %for track in unmatched:
					 <%
					 	duration = helpers.convert_seconds(float(track['TrackLength']))
					 %>
					<tr class="gradeC">
						<td id="number">${track['TrackNumber']}</td>
						<td id="name">${track['TrackTitle']}</td>
						<td id="duration">${duration}</td>
						<td id="location">${track['Location']}</td>
						<td id="bitrate">${int(track['BitRate'])/1000} kbps</td>
						<td id="format">${track['Format']}</td>
					</tr>
					%endfor
				%endif
				</tbody>
			</table>
		</div>
	</div>
</%def>

<%def name="headIncludes()">
	<link rel="stylesheet" href="interfaces/default/css/data_table.css">
</%def>

<%def name="javascriptIncludes()">
	<script src="js/libs/jquery.dataTables.min.js"></script>
	<script>
	function getAlbumInfo() {
		var id = "${album['AlbumID']}";
		var elem = $("#albumInfo");
		getInfo(elem,id,'album');
	}

	function initThisPage() {
		$('#album_chooser').click(function() {
			$('#dialog').dialog({
				width: 500,
				maxHeight: 500
			});
			return false;
		});
		$('#edit_search_term').click(function() {
			$('#dialog2').dialog({
				width: 500,
				maxHeight: 500
			});
			return false;
		});
		$('#refresh_artist').click(function() {
			$('#dialog').dialog("close");
		});
		initActions();
		setTimeout(function(){
			initFancybox();
		}, 1000);

		$('#track_table').dataTable({
			"aaSorting": [],
			"bFilter": false,
			"bInfo": false,
			"bPaginate": false,
			"bDestroy": true
		});
	};

	function getAvailableDownloads() {
		ShowSpinner();
		$.getJSON("choose_specific_download?AlbumID=${album['AlbumID']}", function(data) {
			loader.remove();
			feedback.fadeOut();
			search_results = data
			for( var i = 0, len = data.length; i < len; i++ ) {
    			$('#downloads_table_body').append('<tr><td id="title"><a href="javascript:void(0)" onclick="downloadSpecificRelease('+i+')">'+data[i].title+'</a></td><td id="size"><span title='+data[i].size+'></span>'+(data[i].size / (1024*1024)).toFixed(2)+' MB</td><td id="provider">'+data[i].provider+'</td><td id="kind">'+data[i].kind+'</td><td id="matches">'+data[i].matches+'</td></tr>');
    		}
			$('#downloads_table').dataTable({
				"aoColumns": [
					null,
					{"sType": "title-numeric"},
					null,
					null,
					{"sType": "string"}
				],
				"aaSorting": [[ 4, 'desc']],
				"bFilter": false,
				"bInfo": false,
				"bPaginate": false,
				"bDestroy": true
			});
    		$("#choose_specific_download_dialog").dialog({
    			width: "80%",
    			maxHeight: 500
    		});
    		return false;
		});
	}

	function downloadSpecificRelease(i){

		title = search_results[i].title
		size = search_results[i].size
		url = search_results[i].url
		provider = search_results[i].provider
		kind = search_results[i].kind

		ShowSpinner();
		$.getJSON("download_specific_release?AlbumID=${album['AlbumID']}&title="+title+"&size="+size+"&url="+url+"&provider="+provider+"&kind=" + kind, function(data) {
			loader.remove();
			feedback.fadeOut();
			refreshSubmenu();
			$("#choose_specific_download_dialog").dialog("close");
		});
	}

	function ShowSpinner() {
		feedback = $("#ajaxMsg");
		update = $("#updatebar");
		if ( update.is(":visible") ) {
			var height = update.height() + 35;
			feedback.css("bottom",height + "px");
		} else {
			feedback.removeAttr("style");
		}
		loader = $("<i class='fa fa-refresh fa-spin'></i>");
		feedback.prepend(loader);
		feedback.fadeIn();
	}

    var loadingMessage = false;
    var spinner_active = false;
    var loadingtext_active = false;
    var refreshInterval;
    var wasLoading = false;
    var x = 0;

    function checkAlbumStatus() {
        $.getJSON("getAlbumjson?AlbumID=${album['AlbumID']}", function(data) {
            if (data['Status'] == "Loading"){
                wasLoading = true;
                $('#albumnamelink').text(data["AlbumTitle"]);
                $('#artistnamelink').text(data["ArtistName"]);
                if (loadingMessage == false){
                    $("#ajaxMsg").after( "<div id='ajaxMsg2' class='ajaxMsg'></div>" );
                    showArtistMsg("Getting album information");
                    loadingMessage = true;
                }
                if (spinner_active == false){
                    $('#albumname').prepend('<i class="fa fa-refresh fa-spin" id="albumnamespinner"></i>')
                    spinner_active = true;
                }
                if (loadingtext_active == false){
                    $('#albumname').append('<h3 id="loadingtext"><i>(Album information is currently being loaded)</i></h3>')
                    loadingtext_active = true;
                }
            }
            else{
                if (++x === 10) {
                    clearInterval(refreshInterval);
                }
                var sts = $("#artistname").text().trim();
                if (wasLoading == true || sts == "Loading"){
                    location.reload();
                $('#albumnamespinner').remove()
                $('#loadingtext').remove()
                $('#ajaxMsg2').remove()
                spinner_active = false
                loadingtext_active = false
                loadingMessage = false
                }
            }
        });
    }

    jQuery.extend( jQuery.fn.dataTableExt.oSort, {
		"title-numeric-pre": function ( a ) {
			var x = a.match(/title="*(-?[0-9\.]+)/)[1];
			return parseFloat( x );
		},

		"title-numeric-asc": function ( a, b ) {
			return ((a < b) ? -1 : ((a > b) ? 1 : 0));
		},

        "title-numeric-desc": function ( a, b ) {
            return ((a < b) ? 1 : ((a > b) ? -1 : 0));
        }
    } );

	$(document).ready(function()	{
		getAlbumInfo();
		initThisPage();
        checkAlbumStatus();
        refreshInterval = setInterval(function(){
            checkAlbumStatus();
        }, 3000);
	});

	</script>
</%def>
