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

namespace BackOffice.Pages.Tag;

public partial class TagManagementPage
{
    // TagService is injected in the .razor file

    private MudDataGrid<TagListItemDto> _grid = default!;
    private string? _search;
    private bool? _isActive;

    private async Task<GridData<TagListItemDto>> LoadServerData(GridState<TagListItemDto> state)
    {
        var filter = new TagFilterDto
        {
            PageNumber = state.Page + 1,
            PageSize = state.PageSize,
            SearchTerm = _search,
            IsActive = _isActive
        };

        var result = await TagService.GetTagsAsync(filter);

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

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

    private void OnSearchKeyDown(KeyboardEventArgs args)
    {
        if (args.Key == "Enter")
        {
            _ = ReloadAsync();
        }
    }

    private async Task CreateTag()
    {
        var parameters = new DialogParameters<TagEditDialog>
        {
            { nameof(TagEditDialog.Model), new TagEditDto() },
            { nameof(TagEditDialog.IsEditMode), false }
        };

        var dialog = await DialogService.ShowAsync<TagEditDialog>("ایجاد تگ جدید", 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 EditTag(TagListItemDto item)
    {
        var dto = new TagEditDto
        {
            Name = item.Name,
            Title = item.Title,
            Description = item.Description,
            IsActive = item.IsActive,
            SortOrder = item.SortOrder
        };

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

        var dialog = await DialogService.ShowAsync<TagEditDialog>("ویرایش تگ", 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 DeleteTag(TagListItemDto item)
    {
        var confirmed = await DialogService.ShowMessageBox(
            "حذف تگ",
            $"آیا از حذف تگ «{item.Title}» مطمئن هستید؟",
            yesText: "حذف",
            cancelText: "لغو");

        if (confirmed == true)
        {
            await TagService.DeleteAsync(item.Id);
            Snackbar.Add("تگ حذف شد", Severity.Success);
            await ReloadAsync();
        }
    }
}

