{% import '@Marketplace/macros.twig' as marketplaceMacro %}

{% block content %}

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

            {% set isBundle = plugin.isBundle is defined and plugin.isBundle %}

            {% if plugin.versions is not empty and plugin.versions[plugin.versions|length - 1] %}
                {% set latestVersion = plugin.versions[plugin.versions|length - 1] %}
            {% else %}
                {% set latestVersion = '' %}
            {% endif %}

            <div class="row">
                <div class="col s12 m9">
                    <h2>{{ plugin.displayName }}</h2>
                    <p class="description">
                        {% if plugin.featured %}
                            {{ marketplaceMacro.featuredIcon('left') }}
                        {% endif %}
                        {{ plugin.description }}
                    </p>
                    <div class="contentDetails">
                        <div id="pluginDetailsTabs" class="row">
                            <div class="col s12">
                                <ul class="tabs">
                                    <li class="tab col s3"><a href="#tabs-description">{{ 'General_Description'|translate }}</a></li>

                                    {% if latestVersion.readmeHtml.faq %}
                                        <li class="tab col s3"><a href="#tabs-faq">{{ 'General_Faq'|translate }}</a></li>
                                    {% endif %}

                                    {% if latestVersion.readmeHtml.documentation %}
                                        <li class="tab col s3"><a href="#tabs-documentation">{{ 'General_Documentation'|translate }}</a></li>
                                    {% endif %}

                                    {% if plugin.screenshots|length %}
                                        <li class="tab col s3"><a href="#tabs-screenshots">{{ 'Marketplace_Screenshots'|translate }}</a></li>
                                    {% endif %}

                                    {% if plugin.shop is defined and plugin.shop and plugin.shop.reviews and plugin.shop.reviews.embedUrl is defined and plugin.shop.reviews.embedUrl %}
                                        <li class="tab col s3"><a href="#tabs-reviews">{{ 'Marketplace_Reviews'|translate }}</a></li>
                                    {% endif %}
                                </ul>
                            </div>

                            <div id="tabs-description" class="tab-content col s12">
                                {% if isSuperUser and (plugin.isDownloadable or plugin.isInstalled) %}
                                    {{ marketplaceMacro.missingRequirementsPleaseUpdateNotice(plugin) }}

                                    {% if isMultiServerEnvironment %}
                                        <div class="alert alert-warning">{{ 'Marketplace_MultiServerEnvironmentWarning'|translate }}</div>
                                    {% elseif not isAutoUpdateEnabled %}
                                        <div class="alert alert-warning">{{ 'Marketplace_AutoUpdateDisabledWarning'|translate("'[General]enable_auto_update=1'", "'config/config.ini.php'") }}</div>
                                    {% endif %}
                                {% endif %}

                                {% if hasSomeAdminAccess and plugin.isMissingLicense is defined and plugin.isMissingLicense %}
                                    <div class="alert alert-danger">{{ 'Marketplace_PluginLicenseMissingDescription'|translate }}</div>
                                {% elseif hasSomeAdminAccess and plugin.hasExceededLicense is defined and plugin.hasExceededLicense %}
                                    <div class="alert alert-warning">{{ 'Marketplace_PluginLicenseExceededDescription'|translate }}</div>
                                {% endif %}

                                <div ng-non-bindable>{{ latestVersion.readmeHtml.description|raw }}</div>
                            </div>

                            {% if latestVersion.readmeHtml.faq %}
                                <div id="tabs-faq" class="tab-content col s12" ng-non-bindable>
                                    {{ latestVersion.readmeHtml.faq|raw }}
                                </div>
                            {% endif %}

                            {% if latestVersion.readmeHtml.documentation %}
                                <div id="tabs-documentation" class="tab-content col s12" ng-non-bindable>
                                    {{ latestVersion.readmeHtml.documentation|raw }}
                                </div>
                            {% endif %}

                            {% if plugin.screenshots|length %}
                                <div id="tabs-screenshots" class="tab-content col s12">
                                    <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 plugin.shop is defined and plugin.shop and plugin.shop.reviews and plugin.shop.reviews.embedUrl is defined and plugin.shop.reviews.embedUrl %}
                                <div id="tabs-reviews" class="tab-content col s12">
                                    <iframe class="reviewIframe"
                                            style="{% if plugin.shop.reviews.height %}height:{{ plugin.shop.reviews.height }}px;{% endif %}"
                                            id="{{ plugin.shop.reviews.embedUrl|md5 }}"
                                            src="{{ plugin.shop.reviews.embedUrl|raw }}"></iframe>
                                </div>
                            {% endif %}
                        </div>
                    </div>
                </div>
                <div class="col s12 m3">
                    <div class="metadata">
                        <div class="actionButton">
                            {% if not plugin.isDownloadable or not isSuperUser %}
                                {% if hasSomeAdminAccess and plugin.hasExceededLicense is defined and plugin.hasExceededLicense and plugin.consumer and plugin.consumer.loginUrl is defined and plugin.consumer.loginUrl %}
                                    <a class="install update"
                                       target="_blank"
                                       rel="noreferrer noopener"
                                       href="{{ plugin.consumer.loginUrl|default('')|e('html_attr') }}"
                                    >{{ 'Marketplace_UpgradeSubscription'|translate }}</a>
                                {% endif %}
                            {% elseif isSuperUser %}
                                {% if not isAutoUpdatePossible %}
                                    <a onclick="$(this).css('display', 'none')" href="{{ linkTo({'action': 'download', 'pluginName': plugin.name, 'nonce': (plugin.name|nonce)}) }}"
                                       class="download">{{ 'General_Download'|translate }}</a>
                                {% elseif plugin.canBeUpdated and 0 == plugin.missingRequirements|length %}
                                    <a class="install update"
                                       href="{{ linkTo({'module': 'Marketplace', 'action':'updatePlugin', 'pluginName': plugin.name, 'nonce': updateNonce}) }}"
                                    >{{ 'CoreUpdater_UpdateTitle'|translate }}</a>
                                {% elseif not plugin.isInstalled and 0 == plugin.missingRequirements|length %}
                                    <a href="{{ linkTo({'module': 'Marketplace', 'action': 'installPlugin', 'pluginName': plugin.name, 'nonce': installNonce}) }}"
                                       class="install">{{ 'Marketplace_ActionInstall'|translate }}</a>
                                {% endif %}
                            {% endif %}
                        </div>

                        {% if plugin.specialOffer is defined and plugin.specialOffer %}<p style="color: green;">{{ plugin.specialOffer }}<br /><br /></p>{% endif %}

                        <dl>
                            {% if not isBundle %}
                                <dt>{{ 'CorePluginsAdmin_Version'|translate }}</dt>
                                <dd>{{ plugin.latestVersion }}</dd>
                            {% endif %}

                            <dt>{{ 'Marketplace_PluginKeywords'|translate }}</dt>
                            <dd>{{ plugin.keywords|join(', ') }}</dd>

                            {% if plugin.lastUpdated and not isBundle %}
                                <dt>{{ 'Marketplace_LastUpdated'|translate }}</dt>
                                <dd>{{ plugin.lastUpdated }}</dd>
                            {% endif %}
                            {% if plugin.numDownloads %}
                                <dt>{{ 'General_Downloads'|translate }}</dt>
                                <dd title="{{ 'Marketplace_NumDownloadsLatestVersion'|translate(latestVersion.numDownloads|number_format) }}">{{ plugin.numDownloads }}</dd>
                            {% endif %}

                            {% if not isBundle %}
                                <dt>{{ 'Marketplace_Developer'|translate }}</dt>
                                <dd>{{ marketplaceMacro.pluginDeveloper(plugin.owner) }}</dd>
                                {% if latestVersion and latestVersion.license is defined and latestVersion.license and latestVersion.license.name is defined and latestVersion.license.name %}
                                    <dt>{{ 'Marketplace_License'|translate }}</dt>
                                    <dd>
                                        {% if latestVersion.license.url is defined and latestVersion.license.url %}
                                            <a rel="noreferrer noopener"
                                               href="{{ latestVersion.license.url }}"
                                               target="_blank">{{ latestVersion.license.name }}</a>
                                        {% else %}
                                            {{ latestVersion.license.name }}
                                        {% endif %}
                                    </dd>
                                {% endif %}
                                <dt>{{ 'Marketplace_Authors'|translate }}</dt>
                                <dd>{% for author in plugin.authors|filter(author => author.name) %}
                                        {% apply spaceless %}
                                            {% if author.homepage %}
                                                <a target="_blank" rel="noreferrer noopener" 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 %}
                                        {% endapply %}
                                    {% endfor %}
                                </dd>
                            {% endif %}
                            <dt>{{ 'CorePluginsAdmin_Websites'|translate }}</dt>
                            <dd>
                                {% if plugin.homepage %}
                                    <a target="_blank" rel="noreferrer noopener" href="{{ plugin.homepage }}">{{ 'Marketplace_PluginWebsite'|translate }}</a>
                                {%- endif -%}

                                {%- if plugin.changelog is defined and plugin.changelog and plugin.changelog.url is defined and plugin.changelog.url -%}
                                    {%- if plugin.homepage %}, {% endif %}
                                    <a target="_blank" rel="noreferrer noopener" href="{{ plugin.changelog.url }}">{{ 'CorePluginsAdmin_Changelog'|translate }}</a>
                                {%- endif -%}

                                {%- if plugin.repositoryUrl %}, <a target="_blank" href="{{ plugin.repositoryUrl }}">GitHub</a>{% endif %}
                            </dd>

                            {% if plugin.activity and plugin.activity.numCommits %}
                                <dt>{{ 'CorePluginsAdmin_Activity'|translate }}</dt>
                                <dd>
                                    {{ plugin.activity.numCommits }} commits

                                    {% if plugin.activity.numContributors > 1 %}
                                        {{ 'Marketplace_ByXDevelopers'|translate(plugin.activity.numContributors) }}
                                    {% endif %}
                                    {% if plugin.activity.lastCommitDate %}
                                        {{ 'Marketplace_LastCommitTime'|translate(plugin.activity.lastCommitDate) }}
                                    {% endif %}</dd>
                            {% endif %}
                        </dl>

                        {% if plugin.support is not empty %}
                            {% for entry in plugin.support %}
                                {% if entry.name and entry.value %}
                                    <dt>{{ entry.name }}</dt>
                                    <dd>{{ entry.value }}</dd>
                                {% endif %}
                            {% endfor %}
                        {% endif %}

                        <br />
                    </div>
                </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 select').material_select();

                    $('#pluginDetailsTabs .tabs').tabs();
                    $('#pluginDetailsTabs .tabs').tabs('select', '#tabs-' + (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>

            {% if plugin.shop is defined and plugin.shop and plugin.shop.reviews and plugin.shop.reviews.embedUrl is defined and plugin.shop.reviews.embedUrl %}
                <script type="text/javascript">
                    $(function() {
                        var $iFrames = $('.pluginDetails iframe.reviewIframe');
                        for (var i = 0; i < $iFrames.length; i++) {
                            iFrameResize({checkOrigin: ['{{ plugin.shop.reviews.embedUrl|domainOnly }}']}, $iFrames[i]);
                        }
                    });
                </script>
            {% endif %}

            <script type="text/javascript">
                $(function() {
                    var $variationPicker = $('.pluginDetails .variationPicker select');
                    if ($variationPicker.val()) {
                        $('.addToCartLink').attr('href', $variationPicker.val());
                    }
                    $variationPicker.on('change', function () {
                        $('.addToCartLink').attr('href', $variationPicker.val())
                    });

                    if ($variationPicker.length) {
                        $variationPicker.material_select();
                    }
                });
            </script>
        {% endif %}
    </div>


{% endblock %}
