@model Disco.Web.Areas.Config.Models.Plugins.InstallModel
@using Disco.Services.Plugins;
@{
    Authorization.Require(Claims.Config.Plugin.Install);

    var canInstallLocal = Authorization.Has(Claims.Config.Plugin.InstallLocal);

    ViewBag.Title = Html.ToBreadcrumb("Configuration", MVC.Config.Config.Index(), "Plugins", MVC.Config.Plugins.Index(), "Install Plugin");
}
<div id="pluginLibrary">
    <h4 id="pluginLibraryHeading">The plugin library [<a href="https://discoict.com.au/">https://discoict.com.au</a>] was last updated @CommonHelpers.FriendlyDate((Model.Library.ManifestDate > DateTime.Now ? DateTime.Now : Model.Library.ManifestDate))</h4>
    @if (Model.Library.Plugins.Count == 0)
    {
        <div class="form" style="width: 450px; padding: 100px 0;">
            <h2>No Plugins are Available</h2>
        </div>
    }
    else
    {
        var pluginGroups = Model.AvailablePlugins;

        <div id="pluginLibraryGroups">
            @foreach (var pluginGroup in pluginGroups)
            {
                <div class="form">
                    <h2>@pluginGroup.Item1</h2>
                    <table>
                        @foreach (var plugin in pluginGroup.Item2)
                        {
                            var installedPlugin = Plugins.PluginInstalled(plugin.Item1.Id) ? Plugins.GetPlugin(plugin.Item1.Id) : null;
                            <tr>
                                <td>
                                    <div class="pluginItem@(installedPlugin != null ? " pluginInstalled" : string.Empty)">
                                        <h2 class="pluginName">
                                            <i class="fa fa-cogs"></i>@plugin.Item1.Name
                                            @if (installedPlugin == null)
                                            {
                                                <a class="pluginInstallLink button" href="@(Url.Action(MVC.API.Plugin.Install(plugin.Item1.Id)))">Install</a>
                                            }
                                            else
                                            {
                                                if (Version.Parse(plugin.Item2.Version) > installedPlugin.Version)
                                                {
                                                    <a class="pluginUpdateLink button alert" href="@(Url.Action(MVC.API.Plugin.Update(plugin.Item1.Id)))">Update</a>
                                                }
                                                else
                                                {
                                                    <a class="button" href="@(Url.Action(MVC.Config.Plugins.Configure(plugin.Item1.Id)))">Configure</a>
                                                }
                                            }
                                        </h2>
                                        <div class="pluginItemBlurb">@(new HtmlString(plugin.Item1.Description))</div>
                                        <div class="pageMenuBlurb">
                                            <span class="pluginId">@plugin.Item1.Id</span> | <span class="pluginVersion">v@(plugin.Item2.Version)</span> | @plugin.Item1.Author | <a href="@plugin.Item1.InformationUrl" title="More Information" target="_blank"><i class="fa fa-external-link"></i></a>
                                        </div>
                                    </div>
                                </td>
                            </tr>

                        }
                    </table>
                </div>
            }
        </div>

    }
</div>
<div id="dialogInstallPlugin" title="Install this Plugin?" class="dialog">
    <h2 id="dialogInstallPluginName"></h2>
    <h4 id="dialogInstallPluginDetails"></h4>

    <div class="info-box">
        <p class="fa-p">
            <i class="fa fa-exclamation-triangle"></i><strong>Warning:</strong> All plugins run with the same level of network privileges as the Disco ICT Web App.<br />
            <strong>Only Install plugins from a trusted source.</strong>
        </p>
    </div>
    @using (Html.BeginForm(MVC.API.Plugin.Install()))
    {
        @Html.AntiForgeryToken()
    }
</div>
@if (canInstallLocal)
{
    <div id="dialogUploadPlugin" title="Install Plugin Package" class="dialog">
        <div style="padding-bottom: 10px;">
            @using (Html.BeginForm(MVC.API.Plugin.InstallLocal(), FormMethod.Post, new { enctype = "multipart/form-data" }))
            {
                @Html.AntiForgeryToken()
                <label for="pluginFile">Plugin Package: </label>
                <input id="pluginFile" name="Plugin" type="file" accept=".discoPlugin" />
            }
        </div>
        <div class="info-box error">
            <p class="fa-p">
                <i class="fa fa-exclamation-triangle"></i><strong>Warning:</strong> All plugins run with the same level of network privileges as the Disco ICT Web App.<br />
                <strong>Only install plugins from a trusted source.</strong>
            </p>
        </div>
    </div>
}
<script>
    $(function () {
        var $selectedPlugin;
        var $selectedPluginUrl;

        $('#pluginLibraryHeading').appendTo('#layout_PageHeading');

        // Install
        var $dialogInstall = $('#dialogInstallPlugin').dialog({
            resizable: false,
            modal: true,
            width: 350,
            autoOpen: false,
            buttons: {
                "Install": function () {
                    if ($selectedPlugin == null || !$selectedPluginUrl) {
                        $(this).dialog("close");
                        return;
                    }
                    $(this).dialog("disable");
                    $(this)
                        .find('form')
                        .attr('action', $selectedPluginUrl)
                        .submit();
                },
                Cancel: function () {
                    $selectedPlugin = null;
                    $(this).dialog("close");
                }
            }
        });
        $('#pluginLibraryGroups').find('a.pluginInstallLink').click(function () {
            $this = $(this);

            $selectedPlugin = $this.closest('.pluginItem');
            $selectedPluginUrl = $this.attr('href');

            $('#dialogInstallPluginName').text($selectedPlugin.find('.pluginName').text());
            $('#dialogInstallPluginDetails').text($selectedPlugin.find('.pluginId').text() + ' | ' + $selectedPlugin.find('.pluginVersion').text());

            $dialogInstall.dialog('option', 'title', 'Install this Plugin?');
            $dialogInstall.dialog('open');

            return false;
        });
        $('#pluginLibraryGroups').find('a.pluginUpdateLink').click(function () {
            $this = $(this);

            $selectedPlugin = $this.closest('.pluginItem');
            $selectedPluginUrl = $this.attr('href');

            $('#dialogInstallPluginName').text($selectedPlugin.find('.pluginName').text());
            $('#dialogInstallPluginDetails').text($selectedPlugin.find('.pluginId').text() + ' | ' + $selectedPlugin.find('.pluginVersion').text());

            $dialogInstall.dialog('option', 'title', 'Update this Plugin?');
            $dialogInstall.dialog('open');

            return false;
        });

        @if (canInstallLocal)
        {<text>
        // Upload
        var $dialogUpload = $('#dialogUploadPlugin').dialog({
            resizable: false,
            modal: true,
            width: 350,
            autoOpen: false,
            buttons: {
                "Upload & Install": function () {
                    var pluginFile = $('#pluginFile');
                    if (pluginFile.val()) {
                        pluginFile.closest('form').submit();
                        $(this).dialog('disable');
                    } else {
                        alert('Choose a Plugin Package to Upload');
                    }
                },
                Cancel: function () {
                    $(this).dialog("close");
                }
            }
        });
        $('#buttonUpload').click(function () {
            $dialogUpload.dialog('open');
            return false;
        });
        </text>}
    });
</script>
<div class="actionBar">
    @Html.ActionLinkButton("Update Plugin Library", MVC.API.Plugin.UpdateLibraryManifest())
    @if (canInstallLocal)
    {
        @Html.ActionLinkButton("Install Plugin Package", MVC.API.Plugin.InstallLocal(), "buttonUpload")
    }
</div>
