using BackOffice.Services.Blog;
using BackOffice.Pages.Blog.Components;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Web;
using MudBlazor;

namespace BackOffice.Pages.Blog;

public partial class BlogCategoryManagementPage
{
    private MudDataGrid<BlogCategoryItemDto> _grid = default!;

    private async Task<GridData<BlogCategoryItemDto>> LoadServerData(GridState<BlogCategoryItemDto> state)
    {
        var filter = new BlogCategoryFilterDto
        {
            PageNumber = state.Page + 1,
            PageSize = state.PageSize
        };

        var result = await BlogCategoryService.GetAllAsync(filter);

        return new GridData<BlogCategoryItemDto>
        {
            Items = result.Items,
            TotalItems = result.TotalCount
        };
    }

    private async Task ReloadAsync()
    {
        if (_grid != null)
        {
            await _grid.ReloadServerData();
        }
    }

    private async Task CreateCategory()
    {
        var parameters = new DialogParameters<BlogCategoryEditDialog>
        {
            { nameof(BlogCategoryEditDialog.Model), new BlogCategoryEditDto() },
            { nameof(BlogCategoryEditDialog.IsEditMode), false }
        };

        var dialog = await DialogService.ShowAsync<BlogCategoryEditDialog>("ایجاد دسته‌بندی جدید", parameters,
            new DialogOptions { CloseButton = true, MaxWidth = MaxWidth.Small, FullWidth = true });

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

    private async Task EditCategory(BlogCategoryItemDto item)
    {
        var dto = new BlogCategoryEditDto
        {
            Title = item.Title,
            Slug = item.Slug,
            Description = item.Description,
            IconName = item.IconName,
            SortOrder = item.SortOrder,
            IsActive = item.IsActive
        };

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

        var dialog = await DialogService.ShowAsync<BlogCategoryEditDialog>("ویرایش دسته‌بندی", parameters,
            new DialogOptions { CloseButton = true, MaxWidth = MaxWidth.Small, FullWidth = true });

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

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

        if (confirmed == true)
        {
            await BlogCategoryService.DeleteAsync(item.Id);
            Snackbar.Add("دسته‌بندی حذف شد", Severity.Success);
            await ReloadAsync();
        }
    }
}
