{#
/**
 * Copyright (C) 2020 Xibo Signage Ltd
 *
 * Xibo - Digital Signage - http://www.xibo.org.uk
 *
 * This file is part of Xibo.
 *
 * Xibo is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * Xibo is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with Xibo.  If not, see <http://www.gnu.org/licenses/>.
 */
#}
{% extends "authed.twig" %}
{% import "inline.twig" as inline %}

{% block pageContent %}
    {% include "theme-dashboard-message.twig" ignore missing %}
    <div class="row">
        <div class="col-md-6 col-12">
            <div class="row">
                <div class="col-6">
                    <div class="widget">
                        <div class="widget-body p-3 p-xl-2">
                            <div class="widget-icon green pull-left">
                                <i class="fa fa-users"></i>
                            </div>
                            <div class="widget-content pull-left">
                                <div class="title">{{ library.countOf }}</div>
                                <div class="comment">{% trans "Library Count" %}</div>
                            </div>
                            <div class="clearfix"></div>
                        </div>
                    </div>
                </div>
                <div class="col-6">
                    <div class="widget">
                        <div class="widget-body p-3 p-xl-2">
                            <div class="widget-icon red pull-left">
                                <i class="fa fa-tasks"></i>
                            </div>
                            <div class="widget-content pull-left">
                                <div class="title">{{ library.size }}</div>
                                <div class="comment">{% trans "Library Size" %}</div>
                            </div>
                            <div class="clearfix"></div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="row">
                <div class="col-12">
                    <div class="widget">
                        <div class="widget-title">
                            <i class="fa fa-tasks"></i>
                            {% trans "Number of media items" %}
                            <div class="clearfix"></div>
                        </div>
                        <div class="widget-body medium no-padding">
                            <canvas id="libraryCountChart" style="clear:both;" width="350" height="220"></canvas>
                        </div>
                    </div>
                </div>
            </div>
            <div class="row">
                <div class="col-12">
                    <div class="widget">
                        <div class="widget-title">
                            <i class="fa fa-tasks"></i>
                            {% trans "Size of media items" %} - {{ library.typesSuffix }}
                            <div class="clearfix"></div>
                        </div>
                        <div class="widget-body medium no-padding">
                            <canvas id="librarySizeChart" style="clear:both;" width="350" height="220"></canvas>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-md-6 col-12">
            {% if settings.SETTING_LIBRARY_TIDY_ENABLED == 1 and currentUser.featureEnabled("library.modify") %}
                <div class="row">
                    <div class="col-12">
                        <div class="widget">
                            <div class="widget-body p-3 p-xl-2 widget-button XiboFormButton" href="{{ url_for("library.tidy.form") }}">
                                <div class="widget-icon red pull-left">
                                    <i class="fa fa-trash"></i>
                                </div>
                                <div class="widget-content pull-left">
                                    <div class="title">
                                        {% trans "Tidy library" %}
                                    </div>
                                    <div class="comment">{% trans "Run through the library and remove unused and unnecessary files" %}</div>
                                </div>
                                <div class="clearfix"></div>
                            </div>
                        </div>
                    </div>
                </div>
            {% endif %}
            <div class="row">
                <div class="col-12">
                    <div class="widget">
                        <div class="widget-title">
                            <i class="fa fa-video"></i>
                            {% trans "Unused media" %}
                            <div class="clearfix"></div>
                        </div>
                        <div class="widget-body medium no-padding">
                            <div class="table">
                                <table id="datatable-unused-media" class="table">
                                    <thead>
                                    <tr>
                                        <th>{% trans "ID" %}</th>
                                        <th>{% trans "Name" %}</th>
                                        <th>{% trans "Type" %}</th>
                                        <th>{% trans "Size" %}</th>
                                    </tr>
                                    </thead>
                                    <tbody>

                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="row">
                <div class="col-12">
                    <div class="widget">
                        <div class="widget-title">
                            <i class="fa fa-video"></i>
                            {% trans "Unreleased media" %}
                            <div class="clearfix"></div>
                        </div>
                        <div class="widget-body medium no-padding">
                            <div class="table">
                                <table id="datatable-unreleased-media" class="table">
                                    <thead>
                                    <tr>
                                        <th>{% trans "ID" %}</th>
                                        <th>{% trans "Name" %}</th>
                                        <th>{% trans "Filename" %}</th>
                                        <th>{% trans "Reason" %}</th>
                                        <th>{% trans "Widget cache?" %}</th>
                                    </tr>
                                    </thead>
                                    <tbody>

                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
{% endblock %}

{% block javaScript %}
    <script type="text/javascript" nonce="{{ cspNonce }}">
      const stringToColour = function(str) {
        let hash = 0;
        str.split('').forEach(char => {
          hash = char.charCodeAt(0) + ((hash << 5) - hash);
        });
        let colour = '#';
        for (let i = 0; i < 3; i++) {
          const value = (hash >> (i * 8)) & 0xff
          colour += value.toString(16).padStart(2, '0');
        }
        return colour;
      }

      $(document).ready(function() {
        // Library chart
        // -------------
        const libraryData = {{ library.types|json_encode|raw }};
        let dataCount = new Array();
        let dataSize = new Array();
        let colours = new Array();
        let labels = new Array();
        $.each(libraryData, function (i, e) {
          dataSize.push(Math.round(e.size / Math.pow(1024, {{ library.typesBase }}), 2));
          dataCount.push(e.count);
          labels.push(e.title);
          colours.push(stringToColour(e.title));
        });
        const librarySizeChart = new Chart($("#librarySizeChart"), {
          type: 'pie',
          data: {
            datasets: [{
              data: dataSize,
              backgroundColor: colours
            }],
            labels: labels
          },
          options: {
            maintainAspectRatio: false
          }
        });
        const libraryCountChart = new Chart($("#libraryCountChart"), {
          type: 'pie',
          data: {
            datasets: [{
              data: dataCount,
              backgroundColor: colours
            }],
            labels: labels
          },
          options: {
            maintainAspectRatio: false
          }
        });

        // Tables
        let language = dataTablesLanguage;

        // Unused media
        // ----------------
        language.emptyTable = '{{ "No unused media in the Library"|trans }}';
        const dataTableUnusedMedia = $('#datatable-unused-media').DataTable({
          language: language,
          serverSide: true,
          stateSave: true,
          responsive: true,
          stateDuration: 0,
          stateLoadCallback: dataTableStateLoadCallback,
          stateSaveCallback: dataTableStateSaveCallback,
          filter: false,
          searchDelay: 3000,
          order: [[3, 'desc']],
          ajax: {
            url: '{{ url_for("library.search") }}',
            data: function (dataToSend) {
              dataToSend.unusedOnly = 1;
              return dataToSend;
            },
          },
          lengthChange: false,
          columns: [
            {data: 'mediaId'},
            {data: 'name'},
            {data: 'mediaType'},
            {
              data: 'fileSize',
              render: function (data, type, row) {
                if (type !== 'display') {
                  return data;
                }

                return row.fileSizeFormatted;
              }
            },
          ]
        });
        dataTableUnusedMedia.on('processing.dt', dataTableProcessing);

        // Unreleased media
        // ----------------
        language.emptyTable = '{{ "No unreleased media in the Library"|trans }}';
        const dataTableUnreleasedMedia = $('#datatable-unreleased-media').DataTable({
          language: language,
          serverSide: true,
          stateSave: true,
          responsive: true,
          stateDuration: 0,
          stateLoadCallback: dataTableStateLoadCallback,
          stateSaveCallback: dataTableStateSaveCallback,
          filter: false,
          searchDelay: 3000,
          order: [[1, 'asc']],
          ajax: {
            url: '{{ url_for("library.search") }}',
            data: function (dataToSend) {
              dataToSend.unreleasedOnly = 1;
              return dataToSend;
            },
          },
          lengthChange: false,
          columns: [
            {data: 'mediaId'},
            {data: 'name'},
            {data: 'fileName'},
            {data: 'releasedDescription'},
            {
              data: 'mediaType',
              data: function(data, type) {
                const icon = data === 'module' ? 'tick' : 'times';
                return '<span class="fa ' + icon + '"></span>';
              },
            },
          ]
        });
        dataTableUnreleasedMedia.on('processing.dt', dataTableProcessing);
      });
    </script>
{% endblock %}

