@namespace Oqtane.Modules.Admin.RecycleBin
@inherits ModuleBase
@inject NavigationManager NavigationManager
@inject IPageModuleService PageModuleService
@inject IModuleService ModuleService
@inject IPageService PageService
@inject IStringLocalizer<Index> Localizer

<TabStrip>
    <TabPanel Name="Pages" ResourceKey="Pages">
        @if (_pages == null)
        {
            <br />
            <p>@Localizer["No Deleted Pages"]</p>
        }
        else
        {
            <Pager Items="@_pages">
                <Header>
                    <th style="width: 1px;">&nbsp;</th>
                    <th style="width: 1px;">&nbsp;</th>
                    <th>@Localizer["Name"]</th>
                    <th>@Localizer["Deleted By"]</th>
                    <th>@Localizer["Deleted On"]</th>
                </Header>
                <Row>
                    <td><button @onclick="@(() => RestorePage(context))" class="btn btn-info" title="Restore">Restore</button></td>
                    <td><ActionDialog Header="Delete Page" Message="@Localizer["Are You Sure You Wish To Permanently Delete The {0} Page?", context.Name]" Action="Delete" Security="SecurityAccessLevel.Admin" Class="btn btn-danger" OnClick="@(async () => await DeletePage(context))" ResourceKey="DeletePage" /></td>
                    <td>@context.Name</td>
                    <td>@context.DeletedBy</td>
                    <td>@context.DeletedOn</td>
                </Row>
            </Pager>
            @if (_pages.Any())
            {
                <div style="text-align:right;">
                    <ActionDialog Header="Delete All Pages" Message="Are You Sure You Wish To Permanently Delete All Pages?" Action="Delete All Pages" Security="SecurityAccessLevel.Admin" Class="btn btn-danger" OnClick="@(async () => await DeleteAllPages())" ResourceKey="DeleteAllPages" />
                </div>
            }
        }
    </TabPanel>
    <TabPanel Name="Modules" ResourceKey="Modules">
        @if (_modules == null)
        {
            <br />
            <p>@Localizer["No Deleted Modules"]</p>
        }
        else
        {
            <Pager Items="@_modules">
                <Header>
                    <th style="width: 1px;">&nbsp;</th>
                    <th style="width: 1px;">&nbsp;</th>
                    <th>@Localizer["Page"]</th>
                    <th>@Localizer["Module"]</th>
                    <th>@Localizer["Deleted By"]</th>
                    <th>@Localizer["Deleted On"]</th>
                </Header>
                <Row>
                    <td><button @onclick="@(() => RestoreModule(context))" class="btn btn-info" title="Restore">@Localizer["Restore"]</button></td>
                    <td><ActionDialog Header="Delete Module" Message="@Localizer["Are You Sure You Wish To Permanently Delete The {0} Module?", context.Title]" Action="Delete" Security="SecurityAccessLevel.Admin" Class="btn btn-danger" OnClick="@(async () => await DeleteModule(context))" ResourceKey="DeleteModule" /></td>
                    <td>@PageState.Pages.Find(item => item.PageId == context.PageId).Name</td>
                    <td>@context.Title</td>
                    <td>@context.DeletedBy</td>
                    <td>@context.DeletedOn</td>
                </Row>
            </Pager>
            @if (_modules.Any())
            {
                <div style="text-align:right;">
                    <ActionDialog Header="Delete All Modules" Message="Are You Sure You Wish To Permanently Delete All Modules?" Action="Delete All Modules" Security="SecurityAccessLevel.Admin" Class="btn btn-danger" OnClick="@(async () => await DeleteAllModules())" ResourceKey="DeleteAllModules" />
                </div>
            }

        }
    </TabPanel>
</TabStrip>

@code {
    private List<Page> _pages;
    private List<Module> _modules;

    public override SecurityAccessLevel SecurityAccessLevel => SecurityAccessLevel.Admin;

    protected override async Task OnInitializedAsync()
    {
        try
        {
            await Load();
        }
        catch (Exception ex)
        {
            await logger.LogError(ex, "Error Loading Deleted Pages Or Modules {Error}", ex.Message);
            AddModuleMessage(Localizer["Error Loading Deleted Pages Or Modules"], MessageType.Error);
        }
    }

    private async Task Load()
    {
        _pages = await PageService.GetPagesAsync(PageState.Site.SiteId);
        _pages = _pages.Where(item => item.IsDeleted).ToList();

        _modules = await ModuleService.GetModulesAsync(PageState.Site.SiteId);
        _modules = _modules.Where(item => item.IsDeleted).ToList();
    }

    private async Task RestorePage(Page page)
    {
        try
        {
            page.IsDeleted = false;
            await PageService.UpdatePageAsync(page);
            await logger.LogInformation("Page Restored {Page}", page);
            await Load();
            StateHasChanged();
            NavigationManager.NavigateTo(NavigateUrl());
        }
        catch (Exception ex)
        {
            await logger.LogError(ex, "Error Restoring Deleted Page {Page} {Error}", page, ex.Message);
            AddModuleMessage(Localizer["Error Restoring Deleted Page"], MessageType.Error);
        }
    }

    private async Task DeletePage(Page page)
    {
        try
        {
            await PageService.DeletePageAsync(page.PageId);
            await logger.LogInformation("Page Permanently Deleted {Page}", page);
            await Load();
            StateHasChanged();
            NavigationManager.NavigateTo(NavigateUrl());
        }
        catch (Exception ex)
        {
            await logger.LogError(ex, "Error Permanently Deleting Page {Page} {Error}", page, ex.Message);
            AddModuleMessage(ex.Message, MessageType.Error);
        }
    }

    private async Task DeleteAllPages()
    {
        try
        {
            foreach (Page page in _pages)
            {
                await PageService.DeletePageAsync(page.PageId);
                await logger.LogInformation("Page Permanently Deleted {Page}", page);
            }

            await logger.LogInformation("Pages Permanently Deleted");
            await Load();
            StateHasChanged();
            NavigationManager.NavigateTo(NavigateUrl());
        }
        catch (Exception ex)
        {
            await logger.LogError(ex, "Error Permanently Deleting Pages {Error}", ex.Message);
            AddModuleMessage(ex.Message, MessageType.Error);
        }
    }

    private async Task RestoreModule(Module module)
    {
        try
        {
            var pagemodule = await PageModuleService.GetPageModuleAsync(module.PageModuleId);
            pagemodule.IsDeleted = false;
            await PageModuleService.UpdatePageModuleAsync(pagemodule);
            await logger.LogInformation("Module Restored {Module}", module);
            await Load();
            StateHasChanged();
        }
        catch (Exception ex)
        {
            await logger.LogError(ex, "Error Restoring Deleted Module {Module} {Error}", module, ex.Message);
            AddModuleMessage(Localizer["Error Restoring Deleted Module"], MessageType.Error);
        }
    }

    private async Task DeleteModule(Module module)
    {
        try
        {
            await PageModuleService.DeletePageModuleAsync(module.PageModuleId);
            // check if there are any remaining module instances in the site
            _modules = await ModuleService.GetModulesAsync(PageState.Site.SiteId);

            if (!_modules.Exists(item => item.ModuleId == module.ModuleId))
            {
                await ModuleService.DeleteModuleAsync(module.ModuleId);
            }

            await logger.LogInformation("Module Permanently Deleted {Module}", module);
            await Load();
            StateHasChanged();
        }
        catch (Exception ex)
        {
            await logger.LogError(ex, "Error Permanently Deleting Module {Module} {Error}", module, ex.Message);
            AddModuleMessage(Localizer["Error Permanently Deleting Module"], MessageType.Error);
        }
    }

    private async Task DeleteAllModules()
    {
        try
        {
            foreach (Module module in _modules)
            {
                await PageModuleService.DeletePageModuleAsync(module.PageModuleId);
                // check if there are any remaining module instances in the site
                _modules = await ModuleService.GetModulesAsync(PageState.Site.SiteId);

                if (!_modules.Exists(item => item.ModuleId == module.ModuleId))
                {
                    await ModuleService.DeleteModuleAsync(module.ModuleId);
                }
            }

            await logger.LogInformation("Modules Permanently Deleted");
            await Load();
            StateHasChanged();
        }
        catch (Exception ex)
        {
            await logger.LogError(ex, "Error Permanently Deleting Modules {Error}", ex.Message);
            AddModuleMessage(Localizer["Error Permanently Deleting Modules"], MessageType.Error);
        }
    }
}
