namespace NetinaShop.AdminPanel.PWA.Dialogs;

public class ProductCategoryActionDialogBoxViewModel:BaseViewModel
{
    private readonly ISnackbar _snackbar;
    private readonly IRestWrapper _restWrapper;
    private readonly IUserUtility _userUtility;
    private readonly MudDialogInstance _mudDialog;

    public ProductCategoryActionDialogBoxViewModel(ISnackbar snackbar, IRestWrapper restWrapper, IUserUtility userUtility, MudDialogInstance mudDialog)
    {
        _snackbar = snackbar;
        _restWrapper = restWrapper;
        _userUtility = userUtility;
        _mudDialog = mudDialog;
    }

    public ProductCategoryActionDialogBoxViewModel(ISnackbar snackbar, IRestWrapper restWrapper, IUserUtility userUtility, MudDialogInstance mudDialog,ProductCategorySDto category)
    {
        _snackbar = snackbar;
        _restWrapper = restWrapper;
        _userUtility = userUtility;
        _mudDialog = mudDialog;
        Category = category;
    }
    public void Cancel() => _mudDialog.Cancel();

    public string Name = string.Empty;
    public string Description = string.Empty;
    public bool IsMain;
    public bool IsEditing = false;

    private ProductCategorySDto? _category = null;
    [Parameter]
    public ProductCategorySDto? Category
    {
        get => _category;
        set
        {
            _category = value;
            if (_category != null)
            {
                Name = _category.Name;
                Description = _category.Description;
                IsEditing = true;
            }
        }
    }

    public async Task SubmitCreateAsync()
    {
        try
        {
            if (Name.IsNullOrEmpty())
                throw new AppException("لطفا نام دسته را وارد کنید");
            IsProcessing = true;
            var token = await _userUtility.GetBearerTokenAsync();
            var request = new CreateProductCategoryCommand(Name, Description, IsMain, SelectedCategory?.Id ?? default, new List<StorageFileSDto>());
            await _restWrapper.CrudApiRest<ProductCategory, Guid>(Address.ProductCategoryController).Create<CreateProductCategoryCommand>(request, token);
            _mudDialog.Close(DialogResult.Ok(true));
        }
        catch (ApiException ex)
        {
            var exe = await ex.GetContentAsAsync<ApiResult>();
            if (exe != null)
                _snackbar.Add(exe.Message, Severity.Error);
            _snackbar.Add(ex.Content, Severity.Error);
            _mudDialog.Cancel();
        }
        catch (Exception e)
        {
            _snackbar.Add(e.Message, Severity.Error);
            _mudDialog.Cancel();
        }
        finally
        {

            IsProcessing = false;
        }
    }

    public async Task SubmitEditAsync()
    {
        try
        {
            if (Category == null)
                throw new AppException("دسته به درستی ارسال نشده است");
            if (Name.IsNullOrEmpty())
                throw new AppException("لطفا نام دسته را وارد کنید");
            IsProcessing = true;
            await Task.Delay(1000);
            var token = await _userUtility.GetBearerTokenAsync();
            var request = new UpdateProductCategoryCommand(Category.Id, Name, Description, IsMain, SelectedCategory?.Id ?? default, new List<StorageFileSDto>());
            await _restWrapper.CrudApiRest<ProductCategory, Guid>(Address.ProductCategoryController).Update<UpdateProductCategoryCommand>(request, token);
            _mudDialog.Close(DialogResult.Ok(true));
        }
        catch (ApiException ex)
        {
            var exe = await ex.GetContentAsAsync<ApiResult>();
            if (exe != null)
                _snackbar.Add(exe.Message, Severity.Error);
            _snackbar.Add(ex.Content, Severity.Error);
            _mudDialog.Cancel();
        }
        catch (Exception e)
        {
            _snackbar.Add(e.Message, Severity.Error);
            _mudDialog.Cancel();
        }
        finally
        {

            IsProcessing = false;
        }
    }

    private List<ProductCategorySDto> _productCategories = new List<ProductCategorySDto>();
    public ProductCategorySDto? SelectedCategory;
    public async Task<IEnumerable<ProductCategorySDto>> SearchCity(string city)
    {
        try
        {
            if (_productCategories.Count == 0)
            {
                _productCategories = await _restWrapper.ProductCategoryRestApi.ReadAll();
            }
            if (city.IsNullOrEmpty())
                return _productCategories;
            return _productCategories.Where(c => c.Name.Contains(city));
        }
        catch (ApiException ex)
        {
            var exe = await ex.GetContentAsAsync<ApiResult>();
            if (exe != null)
                _snackbar.Add(exe.Message, Severity.Error);
            _snackbar.Add(ex.Content, Severity.Error);
            return _productCategories;
        }
        catch (Exception e)
        {
            _snackbar.Add(e.Message, Severity.Error);
            return _productCategories;
        }
    }
}