using CMSMicroservice.Protobuf.Protos.Category;
using Microsoft.AspNetCore.Components;
using MudBlazor;

namespace BackOffice.Pages.Category;

public partial class CreateOrUpdateCategoryDialog
{
    [CascadingParameter] private IMudDialogInstance MudDialog { get; set; } = default!;
    [Inject] public CategoryContract.CategoryContractClient CategoryContract { get; set; } = default!;

    [Parameter] public CreateNewCategoryRequest? Model { get; set; }
    [Parameter] public UpdateCategoryRequest? EditModel { get; set; }

    private bool IsEdit => EditModel != null;

    private MudForm? _form;
    private bool _isValid;
    private bool _isSaving;

    private string Name { get; set; } = string.Empty;
    private string Title { get; set; } = string.Empty;
    private string? Description { get; set; }
    private string? ImagePath { get; set; }
    private long? ParentId { get; set; }
    private bool IsActive { get; set; } = true;
    private int SortOrder { get; set; }

    protected override void OnInitialized()
    {
        if (IsEdit && EditModel != null)
        {
            Name = EditModel.Name;
            Title = EditModel.Title;
            Description = EditModel.Description;
            ImagePath = EditModel.ImagePath;
            ParentId = EditModel.ParentId;
            IsActive = EditModel.IsActive;
            SortOrder = EditModel.SortOrder;
        }
        else if (Model != null)
        {
            Name = Model.Name;
            Title = Model.Title;
            Description = Model.Description;
            ImagePath = Model.ImagePath;
            ParentId = Model.ParentId;
            IsActive = Model.IsActive;
            SortOrder = Model.SortOrder;
        }
    }

    private async Task Save()
    {
        if (_form != null)
        {
            await _form.Validate();
            if (!_isValid) return;
        }

        _isSaving = true;
        StateHasChanged();

        try
        {
            if (IsEdit && EditModel != null)
            {
                var request = new UpdateCategoryRequest
                {
                    Id = EditModel.Id,
                    Name = Name,
                    Title = Title,
                    Description = Description,
                    ImagePath = ImagePath,
                    ParentId = ParentId,
                    IsActive = IsActive,
                    SortOrder = SortOrder
                };

                await CategoryContract.UpdateCategoryAsync(request);
            }
            else
            {
                var request = new CreateNewCategoryRequest
                {
                    Name = Name,
                    Title = Title,
                    Description = Description,
                    ImagePath = ImagePath,
                    ParentId = ParentId,
                    IsActive = IsActive,
                    SortOrder = SortOrder
                };

                await CategoryContract.CreateNewCategoryAsync(request);
            }

            MudDialog.Close(DialogResult.Ok(true));
        }
        catch (Exception ex)
        {
            Snackbar.Add($"خطا: {ex.Message}", Severity.Error);
        }
        finally
        {
            _isSaving = false;
            StateHasChanged();
        }
    }

    private Task OnParentChanged(long? parentId)
    {
        ParentId = parentId;
        return Task.CompletedTask;
    }

    private void Cancel()
    {
        MudDialog.Cancel();
    }
}
