@namespace Oqtane.Modules.Admin.Themes
@inherits ModuleBase
@inject NavigationManager NavigationManager
@inject IFileService FileService
@inject IThemeService ThemeService
@inject IPackageService PackageService

@if (_packages != null)
{
    <TabStrip>
        @if (_packages.Count > 0)
        {
            <TabPanel Name="Download">
                <ModuleMessage Type="MessageType.Info" Message="Download one or more themes from the list below. Once you are ready click Install to complete the installation."></ModuleMessage>
                <Pager Items="@_packages">
                    <Header>
                        <th>Name</th>
                        <th>Version</th>
                        <th style="width: 1px;"></th>
                    </Header>
                    <Row>
                        <td>@context.Name</td>
                        <td>@context.Version</td>
                        <td>
                            <button type="button" class="btn btn-primary" @onclick=@(async () => await DownloadTheme(context.PackageId, context.Version))>Download</button>
                        </td>
                    </Row>
                </Pager>
            </TabPanel>
        }
        <TabPanel Name="Upload">
            <table class="table table-borderless">
                <tr>
                    <td>
                        <Label HelpText="Upload one or more theme packages. Once they are uploaded click Install to complete the installation.">Theme: </Label>
                    </td>
                    <td>
                        <FileManager Filter="nupkg" ShowFiles="false" Folder="Themes" UploadMultiple="@true" />
                    </td>
                </tr>
            </table>
        </TabPanel>
    </TabStrip>

    <button type="button" class="btn btn-success" @onclick="InstallThemes">Install</button>
    <NavLink class="btn btn-secondary" href="@NavigateUrl()">Cancel</NavLink>
}

@code {
    private List<Package> _packages;

    public override SecurityAccessLevel SecurityAccessLevel => SecurityAccessLevel.Host;

    protected override async Task OnInitializedAsync()
    {
        try
        {
            var themes = await ThemeService.GetThemesAsync();
            _packages = await PackageService.GetPackagesAsync("theme");

            foreach (Package package in _packages.ToArray())
            {
                if (themes.Exists(item => Utilities.GetTypeName(item.ThemeName) == package.PackageId))
                {
                    _packages.Remove(package);
                }
            }
        }
        catch (Exception ex)
        {
            await logger.LogError(ex, "Error Loading Packages {Error}", ex.Message);
            AddModuleMessage("Error Loading Packages", MessageType.Error);
        }
    }

    private async Task InstallThemes()
    {
        try
        {
            ShowProgressIndicator();
            var interop = new Interop(JSRuntime);
            await interop.RedirectBrowser(NavigateUrl(), 10);
            await ThemeService.InstallThemesAsync();
        }
        catch (Exception ex)
        {
            await logger.LogError(ex, "Error Installating Theme");
        }
    }

    private async Task DownloadTheme(string packageid, string version)
    {
        try
        {
            await PackageService.DownloadPackageAsync(packageid, version, "Themes");
            await logger.LogInformation("Theme {ThemeName} {Version} Downloaded Successfully", packageid, version);
            AddModuleMessage("Themes Downloaded Successfully. Click Install To Complete Installation.", MessageType.Success);
            StateHasChanged();
        }
        catch (Exception ex)
        {
            await logger.LogError(ex, "Error Downloading Module {ThemeName} {Version}", packageid, version);
            AddModuleMessage("Error Downloading Theme", MessageType.Error);
        }
    }
}
