{% import '@CorePluginsAdmin/macros.twig' as pluginsMacro %}

{% block content %}

    <div class="pluginDetails">
        {% if errorMessage %}
            {{ errorMessage }}
        {% elseif plugin %}

            {% set latestVersion = plugin.versions[plugin.versions|length - 1] %}

            <div class="header">
                <div class="intro" style="width:75%;float:left;">
                    <h2>{{ plugin.name }}</h2>
                    <p class="description">
                        {% if plugin.featured %}
                            {{ pluginsMacro.featuredIcon('left') }}
                        {% endif %}
                        {{ plugin.description }}
                    </p>
                </div>
                <div class="width:25%;float:left;">

                    {% if isSuperUser %}
                        {% if plugin.canBeUpdated and 0 == plugin.missingRequirements|length %}
                            <a class="install update"
                               href="{{ linkTo({'action':'updatePlugin', 'pluginName': plugin.name, 'nonce': updateNonce}) }}"
                                    >{{ 'CoreUpdater_UpdateTitle'|translate }}</a>
                        {% elseif plugin.isInstalled %}
                        {% elseif 0 < plugin.missingRequirements|length %}
                        {% else %}
                            <a href="{{ linkTo({'action': 'installPlugin', 'pluginName': plugin.name, 'nonce': installNonce}) }}"
                               class="install">{{ 'CorePluginsAdmin_ActionInstall'|translate }}</a>
                        {% endif %}
                    {% endif %}
                </div>
            </div>

            <div class="content">
                <div style="width:75%;float:left;">

                    <div id="pluginDetailsTabs">
                        <ul>
                            <li><a href="#tabs-description">{{ 'General_Description'|translate }}</a></li>
                            {% if latestVersion.readmeHtml.faq %}
                                <li><a href="#tabs-faq">{{ 'General_Faq'|translate }}</a></li>
                            {% endif %}
                            <li><a href="#tabs-changelog">{{ 'CorePluginsAdmin_Changelog'|translate }}</a></li>
                            {% if plugin.screenshots|length %}
                                <li><a href="#tabs-screenshots">{{ 'CorePluginsAdmin_Screenshots'|translate }}</a></li>
                            {% endif %}
                            {% if latestVersion.readmeHtml.support %}
                                <li><a href="#tabs-support">{{ 'CorePluginsAdmin_Support'|translate }}</a></li>
                            {% endif %}
                        </ul>

                        <div id="tabs-description">
                            {{ pluginsMacro.missingRequirementsPleaseUpdateNotice(plugin) }}
                            {{ latestVersion.readmeHtml.description|raw }}
                        </div>

                        {% if latestVersion.readmeHtml.faq %}
                            <div id="tabs-faq">
                                {{ latestVersion.readmeHtml.faq|raw }}
                            </div>
                        {% endif %}

                        <div id="tabs-changelog">
                            {{ pluginsMacro.missingRequirementsPleaseUpdateNotice(plugin) }}
                            {% if plugin.canBeUpdated %}
                                <p class="updateAvailableNotice">{{ 'CorePluginsAdmin_PluginUpdateAvailable'|translate(plugin.currentVersion, plugin.latestVersion) }}
                                    {% if plugin.repositoryChangelogUrl %}<a target="_blank" href="{{ plugin.repositoryChangelogUrl }}">{{ 'CorePluginsAdmin_ViewRepositoryChangelog'|translate }}</a>{% endif %}
                                </p>
                            {% endif %}

                            {% if latestVersion.readmeHtml.changelog %}
                                {{ latestVersion.readmeHtml.changelog|raw }}
                            {% endif %}

                            <h3>{{ 'CorePluginsAdmin_History'|translate }}</h3>

                            <ul>
                                {% for version in plugin.versions|reverse %}
                                    <li>
                                        {% set versionName %}
                                            <strong>
                                                {% if version.repositoryChangelogUrl %}
                                                    <a target="_blank" title="{{ 'CorePluginsAdmin_Changelog'|translate }}" href="{{ version.repositoryChangelogUrl }}">{{ version.name }}</a>
                                                {% else %}
                                                    {{ version.name }}
                                                {% endif %}
                                            </strong>
                                        {% endset %}
                                        {{ 'CorePluginsAdmin_PluginVersionInfo'|translate(versionName, version.release)|raw }}
                                        </li>
                                {% endfor %}
                            </ul>
                        </div>

                        {% if plugin.screenshots|length %}
                            <div id="tabs-screenshots">
                                <div class="thumbnails">
                                    {% for screenshot in plugin.screenshots %}
                                        <div class="thumbnail">
                                            <a href="{{ screenshot }}" target="_blank"><img src="{{ screenshot }}?w=400" width="400" alt=""></a>
                                            <p>
                                                {{ screenshot|split('/')|last|replace({'_': ' ', '.png': '', '.jpg': '', '.jpeg': ''}) }}
                                            </p>
                                        </div>
                                    {% endfor %}
                                </div>
                            </div>
                        {% endif %}

                        {% if latestVersion.readmeHtml.support %}
                            <div id="tabs-support">

                                {{ latestVersion.readmeHtml.support|raw }}

                            </div>
                        {% endif %}
                    </div>

                </div>
                <div class="metadata" style="width:25%;float:left;">
                    <p><br /></p>
                    <dl>
                        <dt>{{ 'CorePluginsAdmin_Version'|translate }}</dt>
                        <dd>{{ plugin.latestVersion }}</dd>
                        <dt>{{ 'CorePluginsAdmin_PluginKeywords'|translate }}</dt>
                        <dd>{{ plugin.keywords|join(', ') }}</dd>
                        <dt>{{ 'CorePluginsAdmin_LastUpdated'|translate }}</dt>
                        <dd>{{ plugin.lastUpdated }}</dd>
                        <dt>{{ 'General_Downloads'|translate }}</dt>
                        <dd title="{{ 'CorePluginsAdmin_NumDownloadsLatestVersion'|translate(latestVersion.numDownloads|number_format) }}">{{ plugin.numDownloads }}</dd>
                        <dt>{{ 'CorePluginsAdmin_Developer'|translate }}</dt>
                        <dd>{{ pluginsMacro.pluginDeveloper(plugin.owner) }}</dd>
                        <dt>{{ 'CorePluginsAdmin_Authors'|translate }}</dt>
                        <dd>{% for author in plugin.authors if author.name %}

                                {% spaceless %}
                                    {% if author.homepage %}
                                        <a target="_blank" href="{{ author.homepage }}">{{ author.name }}</a>
                                    {% elseif author.email %}
                                        <a href="mailto:{{ author.email|escape('url') }}">{{ author.name }}</a>
                                    {% else %}
                                        {{ author.name }}
                                    {% endif %}

                                    {% if loop.index < plugin.authors|length %}
                                        ,
                                    {% endif %}
                                {% endspaceless %}

                            {% endfor %}
                        </dd>
                        <dt>{{ 'CorePluginsAdmin_Websites'|translate }}</dt>
                        <dd>
                            {% if plugin.homepage %}
                                <a target="_blank" href="{{ plugin.homepage }}">{{ 'CorePluginsAdmin_PluginWebsite'|translate }}</a>,
                            {% endif %}
                            <a target="_blank" href="{{ plugin.repositoryUrl }}">GitHub</a></dd>
                        {% if plugin.activity %}
                            <dt>{{ 'CorePluginsAdmin_Activity'|translate }}</dt>
                            <dd>
                                {{ plugin.activity.numCommits }} commits

                                {% if plugin.activity.numContributors > 1 %}
                                    {{ 'CorePluginsAdmin_ByXDevelopers'|translate(plugin.activity.numContributors) }}
                                {% endif %}
                                {% if plugin.activity.lastCommitDate %}
                                    {{ 'CorePluginsAdmin_LastCommitTime'|translate(plugin.activity.lastCommitDate) }}
                                {% endif %}</dd>
                        {% endif %}
                    </dl>
                    <br />
                </div>
            </div>
            <script type="text/javascript">
                $(function() {

                    var active = 0;
                    {% if activeTab %}
                        var $activeTab = $('#tabs-{{ activeTab|e('js') }}');
                        if ($activeTab) {
                            active = $activeTab.index() - 1;
                        }
                    {% endif %}

                    $( "#pluginDetailsTabs" ).tabs({active: active >= 0 ? active : 0});

                    $('.pluginDetails a').each(function (index, a) {
                        var link = $(a).attr('href');

                        if (link && 0 === link.indexOf('http')) {
                            $(a).attr('target', '_blank');
                        }
                    });
                });
            </script>
        {% endif %}
    </div>

{% endblock %}
