<%doc>
USAGE DOCUMENTATION :: PLEASE LEAVE THIS AT THE TOP OF THIS FILE

For Mako templating syntax documentation please visit: http://docs.makotemplates.org/en/latest/

Filename:           home_stats.html
Version:            0.1
Variable names:     data [array]

data[array_index] :: Usable parameters

data['stat_id']     Returns the name of the stat. Either 'top_tv', 'top_movies', 'popular_tv', 'popular_movies', 'top_user' or 'top_platform'
data['stat_type']   Returns the type of the stat. Either 'total_plays' or 'total_duration'
data['rows']        Returns an array containing stat data

data[array_index]['rows'] :: Usable parameters

row_id                  Return the db row id for a metadata item if one exists

== Only if 'stat_id' is 'top_tv' or 'popular_tv' or 'top_movies' or 'popular_movies' or 'top_music' or 'popular_music' or 'last_watched' ==
thumb                   Return the thumb for the media item.

== Only if 'stat_id' is 'top_tv' or 'popular_tv' or 'top_music' or 'popular_music' ==
grandparent_thumb       Returns location of the item's thumbnail. Use with pms_image_proxy.
rating_key              Returns the unique identifier for the media item.
title                   Returns the title for the associated stat.

== Only if 'stat_id' is 'top_tv' or 'top_movies' or 'top_music' or 'top_user' or 'top_platform' or 'top_libraries' ==
total_plays             Returns the count for the associated stat.
total_duration          Returns the total duration for the associated stat.

== Only of 'stat_id' is 'popular_tv' or 'popular_movies' or 'popular_music' ==
users_watched           Returns the count for the associated stat.

== Only if 'stat_id' is 'top_user' or 'last_watched' ==
user_thumb              Returns url of the user's gravatar. Returns '' if none exists.
user                    Returns the username for the associated stat.
user_id                 Returns the user id for the associated stat.
friendly_name           Returns the friendly name of the user for the associated stat.

== Only if 'stat_id' is 'top_platform' or 'last_watched' ==
player                  Returns the player name for the associated stat.

== Only if 'stat_id' is 'last_watched' ==
last_watch              Returns the time the media item was last watched.

== Only if 'stat_id' is 'most_concurrent' ==
count                   Returns the count of the most concurrent streams.
started                 Returns the start time of the most concurrent streams.
stopped                 Returns the stop time of the most concurrent streams.

DOCUMENTATION :: END
</%doc>

<%!
    from plexpy.helpers import cast_to_int, page

    # Human readable duration
    def hd(seconds):
        m, s = divmod(cast_to_int(seconds), 60)
        if s > 30:
            m += 1
        h, m = divmod(m, 60)
        return str(h).zfill(1) + ':' + str(m).zfill(2)
%>

% if data and any(top_stat['rows'] for top_stat in data):
% for top_stat in data:
% if top_stat['rows']:
<%
    stat_id = top_stat['stat_id']
    row0 = top_stat['rows'][0]
%>
<div class="dashboard-stats-instance" id="stats-instance-${stat_id}" data-stat_id="${stat_id}">
    <div class="dashboard-stats-container">
        % if stat_id in ('top_movies', 'popular_movies', 'top_tv', 'popular_tv', 'top_music', 'popular_music', 'last_watched'):
        <% fallback = 'art-live' if row0['live'] else 'art' %>
        <div id="stats-background-${stat_id}" class="dashboard-stats-background" style="background-image: url(${page('pms_image_proxy', row0['art'], row0['rating_key'], 500, 280, 40, '282828', 3, fallback=fallback)});">
        % elif stat_id == 'top_libraries':
        <% fallback = 'art-live' if row0['live'] else row0['library_art'] %>
        <div id="stats-background-${stat_id}" class="dashboard-stats-background" style="background-image: url(${page('pms_image_proxy', row0['art'] or row0['library_art'], None, 500, 280, 40, '282828', 3, fallback=fallback)});" data-library_art="${row0['library_art']}">
        % elif stat_id == 'top_users':
        <div id="stats-background-${stat_id}" class="dashboard-stats-background" data-blurhash="${page('pms_image_proxy', row0['user_thumb'] or 'interfaces/default/images/gravatar-default.png', None, 100, 100, 40, '282828', 0, fallback='user')}">
        % elif stat_id == 'top_platforms':
        <div id="stats-background-${stat_id}" class="dashboard-stats-background platform-${row0['platform_name']}-rgba no-image">
        % else:
        <div id="stats-background-${stat_id}" class="dashboard-stats-background flat">
        % endif
            % if stat_id in ('top_movies', 'popular_movies', 'top_tv', 'popular_tv', 'top_music', 'popular_music', 'last_watched'):
            <div class="dashboard-stats-poster-container hidden-xs">
                % if stat_id in ('top_music', 'popular_music'):
                <div id="stats-thumb-${stat_id}-bg" class="dashboard-stats-poster" style="background-image: url(${page('pms_image_proxy', row0['thumb'], row0['rating_key'], 300, 300, 60, '282828', 3, fallback='cover')});"></div>
                % endif
                <%
                    height, fallback = ('450', 'poster')
                    if stat_id in ('top_music', 'popular_music'):
                        height, fallback = ('300', 'cover')
                    elif row0['live']:
                        height, fallback = ('450', 'poster-live')

                    href = '#'
                    if row0['rating_key']:
                        if row0['live']:
                            href = page('info', row0['rating_key'], row0['guid'], history=True, live=row0['live'])
                        else:
                            href = page('info', row0['rating_key'], history=True)
                %>
                <a id="stats-thumb-url-${stat_id}" href="${href}" title="${row0['title']}">
                    <div id="stats-thumb-${stat_id}" class="dashboard-stats-${fallback.split('-')[0]}" style="background-image: url(${page('pms_image_proxy', row0['thumb'], row0['grandparent_rating_key'] or row0['rating_key'], 300, height, fallback=fallback)});"></div>
                </a>
            </div>
            % elif stat_id == 'top_libraries':
                % if row0['library_thumb'].startswith('http'):
                <div id="stats-thumb-${stat_id}" class="dashboard-stats-flat hidden-xs" style="background-image: url(${page('pms_image_proxy', row0['library_thumb'], None, 100, 100, fallback='cover')});"></div>
                % else:
                <div id="stats-thumb-${stat_id}" class="dashboard-stats-flat svg-icon library-${row0['section_type']} hidden-xs"></div>
                % endif
            % elif stat_id == 'top_users':
            <% user_href = page('user', row0['user_id']) if row0['user_id'] else '#' %>
            <a id="stats-thumb-url-${stat_id}" href="${user_href}" title="${row0['user']}" class="hidden-xs">
                <div id="stats-thumb-${stat_id}" class="dashboard-stats-circle" style="background-image: url(${page('pms_image_proxy', row0['user_thumb'] or 'interfaces/default/images/gravatar-default.png', None, 100, 100, fallback='user')})"></div>
            </a>
            % elif stat_id == 'top_platforms':
            <div id="stats-thumb-${stat_id}" class="dashboard-stats-flat svg-icon platform-${row0['platform_name']} transparent hidden-xs"></div>
            % elif stat_id == 'most_concurrent':
            <div id="stats-thumb-${stat_id}" class="dashboard-stats-flat svg-icon stats-${stat_id} transparent hidden-xs"></div>
            % endif
            <div class="dashboard-stats-info-container">
                <div id="stats-title-${stat_id}" class="dashboard-stats-info-title">
                    <h4>${top_stat['stat_title']}</h4>
                    % if stat_id.startswith('top') and top_stat.get('stat_type') == 'total_plays':
                    <span class="dashboard-stats-stats-units">plays</span>
                    % elif stat_id.startswith('top') and top_stat.get('stat_type') == 'total_duration':
                    <span class="dashboard-stats-stats-units">hh:mm</span>
                    % elif stat_id.startswith('popular'):
                    <span class="dashboard-stats-stats-units">users</span>
                    % elif stat_id == 'last_watched':
                    <span class="dashboard-stats-stats-units" id="last-watched-header-info" title="${row0['user']}">${row0['friendly_name']}</span>
                    % elif stat_id == 'most_concurrent':
                    <span class="dashboard-stats-stats-units" id="most-concurrent-header-info">streams</span>
                    % endif
                </div>
                <div class="dashboard-stats-info-scroller scrollbar-macosx">
                    <div class="dashboard-stats-info scoller-content">
                        <ul class="list-unstyled dashboard-stats-info-list">
                            % for row in top_stat['rows']:
                            <li class="dashboard-stats-info-item ${'expanded' if loop.index == 0 else ''}" data-stat_id="${stat_id}"
                                data-rating_key="${row.get('rating_key')}" data-grandparent_rating_key="${row.get('grandparent_rating_key')}" data-guid="${row.get('guid')}" data-title="${row.get('title')}"
                                data-art="${row.get('art')}" data-thumb="${row.get('thumb')}" data-platform="${row.get('platform_name')}" data-library-type="${row.get('section_type')}"
                                data-user_id="${row.get('user_id')}" data-user="${row.get('user')}" data-friendly_name="${row.get('friendly_name')}" data-user_thumb="${row.get('user_thumb')}"
                                data-last_watch="${row.get('last_watch')}" data-started="${row.get('started')}" data-live="${row.get('live')}"
                                data-library_art="${row.get('library_art', '')}" data-library_thumb="${row.get('library_thumb', '')}">
                                <div class="sub-list">${loop.index + 1}</div>
                                <div class="sub-value">
                                    % if stat_id in ('top_movies', 'popular_movies', 'top_tv', 'popular_tv', 'top_music', 'popular_music', 'last_watched'):
                                    <%
                                        href = '#'
                                        if row['rating_key']:
                                            if row['live']:
                                                href = page('info', row['rating_key'], row['guid'], history=True, live=row['live'])
                                            else:
                                                href = page('info', row['rating_key'], history=True)
                                    %>
                                    <a href="${href}" title="${row['title']}">
                                        ${row['title']}
                                    </a>
                                    % elif stat_id == 'top_libraries':
                                    <% library_href = page('library', row['section_id']) %>
                                    <a href="${library_href}" title="${row['section_name']}">
                                        ${row['section_name']}
                                    </a>
                                    % elif stat_id == 'top_users':
                                    <% user_href = page('user', row['user_id']) if row['user_id'] else '#' %>
                                    <a href="${user_href}" title="${row['user']}">
                                        ${row['friendly_name']}
                                    </a>
                                    % elif stat_id == 'top_platforms':
                                    ${row['platform']}
                                    % elif stat_id == 'most_concurrent':
                                    <a href="graphs#concurrent-graph" title="${row['title']}">
                                        ${row['title']}
                                    </a>
                                    % endif
                                </div>
                                <div class="sub-count">
                                    % if stat_id.startswith('top') and top_stat.get('stat_type') == 'total_plays':
                                    ${row['total_plays']}
                                    % elif stat_id.startswith('top') and top_stat.get('stat_type') == 'total_duration':
                                    ${row['total_duration'] | hd}
                                    % elif stat_id.startswith('popular'):
                                    ${row['users_watched']}
                                    % elif stat_id == 'most_concurrent':
                                    ${row['count']}
                                    % endif
                                </div>
                            </li>
                            % endfor
                        </ul>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
% endif
% endfor
% else:
<div class="text-muted">No stats to show for the selected period.</div><br>
% endif