using BackOffice.Services.Content;
using BackOffice.Pages.Content.Components;
using Microsoft.AspNetCore.Components;
using MudBlazor;

namespace BackOffice.Pages.Content;

public partial class PageSettingsManagementPage
{
    private List<PageSettingsSummaryDto> _pages = new();
    private bool _loading = true;

    protected override async Task OnInitializedAsync()
    {
        await LoadPagesAsync();
    }

    private async Task LoadPagesAsync()
    {
        _loading = true;
        try
        {
            _pages = await PageSettingsService.GetAllAsync();
        }
        catch (Exception ex)
        {
            Snackbar.Add($"خطا در دریافت صفحات: {ex.Message}", Severity.Error);
        }
        finally
        {
            _loading = false;
        }
    }

    private async Task EditPageSettings(PageSettingsSummaryDto summary)
    {
        var details = await PageSettingsService.GetByKeyAsync(summary.PageKey);
        if (details == null)
        {
            Snackbar.Add("خطا در دریافت تنظیمات صفحه", Severity.Error);
            return;
        }

        var dto = new SavePageSettingsDto
        {
            PageKey = details.PageKey,
            Title = details.Title,
            MetaDescription = details.MetaDescription,
            HeroTitle = details.HeroTitle,
            HeroSubtitle = details.HeroSubtitle,
            IsActive = details.IsActive,
            SettingsJson = details.SettingsJson
        };

        var parameters = new DialogParameters<PageSettingsEditDialog>
        {
            { nameof(PageSettingsEditDialog.Model), dto },
            { nameof(PageSettingsEditDialog.ExistingHeroImagePath), details.HeroImagePath },
            { nameof(PageSettingsEditDialog.PageTitle), GetPageDisplayName(summary.PageKey) }
        };

        var dialog = await DialogService.ShowAsync<PageSettingsEditDialog>(
            $"ویرایش تنظیمات «{GetPageDisplayName(summary.PageKey)}»", parameters,
            new DialogOptions { CloseButton = true, MaxWidth = MaxWidth.Medium, FullWidth = true });

        var result = await dialog.Result;
        if (result is { Canceled: false })
        {
            Snackbar.Add("تنظیمات صفحه با موفقیت ذخیره شد", Severity.Success);
            await LoadPagesAsync();
        }
    }

    private async Task ManageImages(PageSettingsSummaryDto summary)
    {
        var details = await PageSettingsService.GetByKeyAsync(summary.PageKey);
        if (details == null)
        {
            Snackbar.Add("خطا در دریافت اطلاعات صفحه", Severity.Error);
            return;
        }

        var parameters = new DialogParameters<PageImagesDialog>
        {
            { nameof(PageImagesDialog.PageId), details.Id },
            { nameof(PageImagesDialog.PageKey), details.PageKey },
            { nameof(PageImagesDialog.PageTitle), GetPageDisplayName(summary.PageKey) },
            { nameof(PageImagesDialog.Images), details.Images }
        };

        var dialog = await DialogService.ShowAsync<PageImagesDialog>(
            $"مدیریت تصاویر «{GetPageDisplayName(summary.PageKey)}»", parameters,
            new DialogOptions { CloseButton = true, MaxWidth = MaxWidth.Large, FullWidth = true });

        var result = await dialog.Result;
        if (result is { Canceled: false })
        {
            await LoadPagesAsync();
        }
    }

    private static string GetPageIcon(string pageKey) => pageKey switch
    {
        "landing" => Icons.Material.Filled.Home,
        "about" => Icons.Material.Filled.Info,
        "contact" => Icons.Material.Filled.ContactMail,
        "licenses" => Icons.Material.Filled.VerifiedUser,
        _ => Icons.Material.Filled.Article
    };

    private static string GetPageDisplayName(string pageKey) => pageKey switch
    {
        "landing" => "صفحه اصلی",
        "about" => "درباره ما",
        "contact" => "تماس با ما",
        "licenses" => "مجوزها",
        _ => pageKey
    };
}
