<%inherit file="base.html" />
<%!
	from headphones import db
	import headphones
%>

<%def name="headerIncludes()">
	<div id="subhead_container">
			&nbsp;
	</div>	
	<a href="manage" class="back">&laquo; Back to manage overview</a>
</%def>


<%def name="body()">
	<div class="table_wrapper">
	<div id="manageheader" class="title">
		<h1 class="clearfix"><img src="interfaces/default/images/icon_manage.png" alt="manage"/>Manage Albums</h1>
	</div>
	<form action="markAlbums" method="get" id="markAlbums">
	<div id="markalbum">Mark selected albums as 
		<select name="action" onChange="doAjaxCall('markAlbums',$(this),'table',true);" data-error="You didn't select any albums">
  			<option disabled="disabled" selected="selected">Choose...</option>
  			<option value="Wanted">Wanted</option>
  			<option value="WantedNew">Wanted (new only)</option>
            <option value="WantedLossless">Wanted (lossless)</option>
  			<option value="Skipped">Skipped</option>
  			<option value="Downloaded">Downloaded</option>
		</select>
		<input type="hidden" value="Go">
	</div>
	<table class="display" id="album_table">
		<thead>
			<tr>
				<th id="select"><input type="checkbox" onClick="toggle(this)" /></th>
				<th id="albumname">Album</th>
                <th id="artistname">Artist</th>
				<th id="reldate">Date</th>
				<th id="type">Type</th>
				<th id="status">Status</th>
				<th id="have">Have</th>
				<th id="bitrate">Bitrate</th>
				<th id="albumformat">Format</th>
			</tr>
		</thead>
		<tbody>
		%for album in albums:
			<%
				if album['Status'] == 'Skipped':
					grade = 'Z'
				elif album['Status'] == 'Wanted':
					grade = 'X'
				elif album['Status'] == 'Snatched':
					grade = 'C'
				else:
					grade = 'A'

				myDB = db.DBConnection()					
				totaltracks = len(myDB.select('SELECT TrackTitle from tracks WHERE AlbumID=?', [album['AlbumID']]))
				havetracks = len(myDB.select('SELECT TrackTitle from tracks WHERE AlbumID=? AND Location IS NOT NULL', [album['AlbumID']])) + len(myDB.select('SELECT TrackTitle from have WHERE ArtistName like ? AND AlbumTitle LIKE ?', [album['ArtistName'], album['AlbumTitle']]))
				
				try:
					percent = (havetracks*100.0)/totaltracks
					if percent > 100:
						percent = 100
				except (ZeroDivisionError, TypeError):
					percent = 0
					totaltracks = '?'
					
				avgbitrate = myDB.action("SELECT AVG(BitRate) FROM tracks WHERE AlbumID=?", [album['AlbumID']]).fetchone()[0]
				if avgbitrate:
					bitrate = str(int(avgbitrate)/1000) + ' kbps'
				else:
					bitrate = ''

				albumformatcount = myDB.action("SELECT COUNT(DISTINCT Format) FROM tracks WHERE AlbumID=?", [album['AlbumID']]).fetchone()[0]
				if albumformatcount == 1:
					albumformat = myDB.action("SELECT DISTINCT Format FROM tracks WHERE AlbumID=?", [album['AlbumID']]).fetchone()[0]
				elif albumformatcount > 1:
					albumformat = 'Mixed'
				else:
					albumformat = ''

				lossy_formats = [str.upper(fmt) for fmt in headphones.LOSSY_MEDIA_FORMATS]

			%>
			<tr class="grade${grade}">
				<td id="select"><input type="checkbox" name="${album['AlbumID']}" class="checkbox" /></td>
				<td id="albumname"><a href="albumPage?AlbumID=${album['AlbumID']}">${album['AlbumTitle']}</a></td>
                <td id="artistname"><a href="artistPage?ArtistID=${album['ArtistID']}">${album['ArtistName']}</a></td>
				<td id="reldate">${album['ReleaseDate']}</td>
				<td id="type">${album['Type']}</td>
				<td id="status">${album['Status']}</td>
				<td id="have"><span title="${percent}"><span><div class="progress-container"><div style="width:${percent}%"><div class="havetracks">${havetracks}/${totaltracks}</div></div></div></td>
				<td id="bitrate">${bitrate}</td>
				<td id="albumformat">${albumformat}</td>
			</tr>	
		%endfor
		</tbody>
	</table>
	</form>
	</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 initThisPage() {
		$('#album_table').dataTable({
			"bDestroy": true,
			"aoColumns": [
                null,
				null,
				null,
				null,
				null,
				null,
				{ "sType": "title-numeric"},
				null,
				null
			],
			"aoColumnDefs": [
			          { 'bSortable': false, 'aTargets': [ 0 ] }
			],				
			"oLanguage": {
				"sLengthMenu":"Show _MENU_ albums per page",
				"sEmptyTable": "No album information available",
				"sInfo":"Showing _TOTAL_ albums",
				"sInfoEmpty":"Showing 0 to 0 of 0 albums",
				"sInfoFiltered":"(filtered from _MAX_ total albums)",
				"sSearch": ""},
			"bPaginate": false,
			"aaSorting": [[5, 'desc']]

		});
		resetFilters("albums");
	}
	$(document).ready(function() {
		initThisPage();
	});
	</script>
</%def>