@namespace Oqtane.Modules.Admin.RecycleBin
@inherits ModuleBase
@inject NavigationManager NavigationManager
@inject IPageModuleService PageModuleService
@inject IModuleService ModuleService
@inject IPageService PageService

<TabStrip>
    <TabPanel Name="Pages">
        @if (_pages == null)
        {
            <br />
            <p>No Deleted Pages</p>
        }
        else
        {
            <Pager Items="@_pages">
                <Header>
                    <th style="width: 1px;">&nbsp;</th>
                    <th style="width: 1px;">&nbsp;</th>
                    <th>Name</th>
                    <th>Deleted By</th>
                    <th>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="@("Are You Sure You Wish To Permanently Delete The " + context.Name + " Page?")" Action="Delete" Security="SecurityAccessLevel.Admin" Class="btn btn-danger" OnClick="@(async () => await DeletePage(context))" /></td>
                    <td>@context.Name</td>
                    <td>@context.DeletedBy</td>
                    <td>@context.DeletedOn</td>
                </Row>
            </Pager>
        }
    </TabPanel>
    <TabPanel Name="Modules">
        @if (_modules == null)
        {
            <br />
            <p>No Deleted Modules</p>
        }
        else
        {
            <Pager Items="@_modules">
                <Header>
                    <th style="width: 1px;">&nbsp;</th>
                    <th style="width: 1px;">&nbsp;</th>
                    <th>Page</th>
                    <th>Module</th>
                    <th>Deleted By</th>
                    <th>Deleted On</th>
                </Header>
                <Row>
                    <td><button @onclick="@(() => RestoreModule(context))" class="btn btn-info" title="Restore">Restore</button></td>
                    <td><ActionDialog Header="Delete Module" Message="@("Are You Sure You Wish To Permanently Delete The " + context.Title + " Module?")" Action="Delete" Security="SecurityAccessLevel.Admin" Class="btn btn-danger" OnClick="@(async () => await DeleteModule(context))" /></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>
        }
    </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("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("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 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("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("Error Permanently Deleting Module", MessageType.Error);
        }
    }
}
