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

namespace BackOffice.Pages.Content.Components;

public partial class SitePageSectionsDialog
{
    [CascadingParameter] IMudDialogInstance DialogInstance { get; set; } = default!;
    [Inject] public ISitePageService SitePageService { get; set; } = default!;

    [Parameter] public long PageId { get; set; }
    [Parameter] public string PageTitle { get; set; } = string.Empty;
    [Parameter] public List<SitePageSectionItemDto> Sections { get; set; } = new();

    private bool _changed;

    private async Task AddSection()
    {
        var dto = new SitePageSectionEditDto
        {
            SitePageId = PageId,
            SortOrder = Sections.Count + 1
        };

        var parameters = new DialogParameters<SitePageSectionEditDialog>
        {
            { nameof(SitePageSectionEditDialog.Model), dto },
            { nameof(SitePageSectionEditDialog.IsEditMode), false }
        };

        var dialog = await DialogService.ShowAsync<SitePageSectionEditDialog>("افزودن بخش جدید", parameters,
            new DialogOptions { CloseButton = true, MaxWidth = MaxWidth.Medium, FullWidth = true });

        var result = await dialog.Result;
        if (!result.Canceled)
        {
            Snackbar.Add("بخش با موفقیت ایجاد شد", Severity.Success);
            _changed = true;
            await RefreshSections();
        }
    }

    private async Task EditSection(SitePageSectionItemDto item)
    {
        var dto = new SitePageSectionEditDto
        {
            SitePageId = PageId,
            SectionKey = item.SectionKey,
            Title = item.Title,
            Subtitle = item.Subtitle,
            HtmlContent = item.HtmlContent,
            IconName = item.IconName,
            ImagePath = item.ImagePath,
            ImageThumbnailPath = item.ImageThumbnailPath,
            SortOrder = item.SortOrder,
            IsActive = item.IsActive,
            ExtraData = item.ExtraData
        };

        var parameters = new DialogParameters<SitePageSectionEditDialog>
        {
            { nameof(SitePageSectionEditDialog.Model), dto },
            { nameof(SitePageSectionEditDialog.SectionId), item.Id },
            { nameof(SitePageSectionEditDialog.IsEditMode), true }
        };

        var dialog = await DialogService.ShowAsync<SitePageSectionEditDialog>("ویرایش بخش", parameters,
            new DialogOptions { CloseButton = true, MaxWidth = MaxWidth.Medium, FullWidth = true });

        var result = await dialog.Result;
        if (!result.Canceled)
        {
            Snackbar.Add("بخش با موفقیت به‌روزرسانی شد", Severity.Success);
            _changed = true;
            await RefreshSections();
        }
    }

    private async Task DeleteSection(SitePageSectionItemDto item)
    {
        var confirmed = await DialogService.ShowMessageBox(
            "حذف بخش",
            $"آیا از حذف بخش «{item.Title}» مطمئن هستید؟",
            yesText: "حذف",
            cancelText: "لغو");

        if (confirmed == true)
        {
            await SitePageService.DeleteSectionAsync(item.Id);
            Snackbar.Add("بخش حذف شد", Severity.Success);
            _changed = true;
            await RefreshSections();
        }
    }

    private async Task RefreshSections()
    {
        var details = await SitePageService.GetByIdAsync(PageId);
        if (details != null)
        {
            Sections = details.Sections;
            StateHasChanged();
        }
    }

    private void Close()
    {
        if (_changed)
            DialogInstance.Close(DialogResult.Ok(true));
        else
            DialogInstance.Cancel();
    }
}
