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

{% if charts is undefined %}
	{% set charts = dbc.get_charts_artists(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 prevartists = dbc.get_charts_artists({'timerange':compare}) %}

		{% set lastranks = {} %}
		{% for a in prevartists %}
			{% if lastranks.update({a.artist:a.rank}) %}{% endif %}
		{% endfor %}

		{% for a in charts %}
			{% if a.artist in lastranks %}
				{% if a.update({'last_rank':lastranks[a.artist]}) %}{% 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['artist']) }}

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