{% import 'snippets/links.jinja' as links %}
{% import 'snippets/entityrow.jinja' as entityrow %}

{% if charts is undefined %}
	{% set charts = dbc.get_charts_tracks(filterkeys,limitkeys) %}
{% endif %}
{% if compare %}
	{% if compare is true %}
		{% set compare = limitkeys.timerange.next(step=-1) %}
		{% if compare is none %}{% set compare = False %}{% endif %}
	{% endif %}
	{% if compare %}
		{% set prevtracks = dbc.get_charts_tracks(filterkeys,{'timerange':compare}) %}

		{% set lastranks = {} %}
		{% for t in prevtracks %}
			{% if lastranks.update({"|".join(t.track.artists)+"||"+t.track.title:t.rank}) %}{% endif %}
		{% endfor %}

		{% for t in charts %}
			{% if "|".join(t.track.artists)+"||"+t.track.title in lastranks %}
				{% if t.update({'last_rank':lastranks["|".join(t.track.artists)+"||"+t.track.title]}) %}{% endif %}
			{% endif %}
		{% endfor %}
	{% endif %}
{% endif %}

{% set firstindex = amountkeys.page * amountkeys.perpage %}
{% set lastindex = firstindex + amountkeys.perpage %}

{% set maxbar = charts[0]['scrobbles'] if charts != [] else 0 %}
<table class='list'>
	{% for e in charts %}
		{% if loop.index0 >= firstindex and loop.index0 < lastindex %}
		<tr>
			<!-- Rank -->
			<td class="rank">{%if loop.changed(e.scrobbles) %}#{{ e.rank }}{% endif %}</td>
			<!-- Rank change -->
			{% if compare %}
				{% if e.last_rank is undefined %}<td class='rankup' title='New'>🆕</td>
				{% elif e.last_rank < e.rank %}<td class='rankdown' title='Down from #{{ e.last_rank }}'>↘</td>
				{% elif e.last_rank > e.rank %}<td class='rankup' title='Up from #{{ e.last_rank }}'>↗</td>
				{% elif e.last_rank == e.rank %}<td class='ranksame' title='Unchanged'>➡</td>
				{% endif %}
			{% endif %}

			<!-- artist -->
			{{ entityrow.row(e['track']) }}

			<!-- scrobbles -->
			<td class="amount">{{ links.link_scrobbles([{'track':e.track,'timerange':limitkeys.timerange}],amount=e['scrobbles']) }}</td>
			<td class="bar">{{ links.link_scrobbles([{'track':e.track,'timerange':limitkeys.timerange}],percent=e['scrobbles']*100/maxbar) }}</td>
		</tr>
		{% endif %}
	{% endfor %}
</table>
