using NetinaShop.Domain.Dtos.ScraperDtos.Response;

namespace NetinaShop.AdminPanel.PWA.Dialogs;

public class DigikalaProductActionDialogBoxViewModel : BaseViewModel<ObservableCollection<ScraperProductDto>>
{
    private readonly ISnackbar _snackbar;
    private readonly IRestWrapper _restWrapper;
    private readonly IUserUtility _userUtility;
    private readonly IDialogService _dialogService;
    private readonly MudDialogInstance _mudDialog;


    public string Search = string.Empty;

    public DigikalaProductActionDialogBoxViewModel(ISnackbar snackbar,
        IRestWrapper restWrapper,
        IUserUtility userUtility,
        IDialogService dialogService,
        MudDialogInstance mudDialog)
    {
        _snackbar = snackbar;
        _restWrapper = restWrapper;
        _userUtility = userUtility;
        _dialogService = dialogService;
        _mudDialog = mudDialog;
    }
    public void Cancel() => _mudDialog.Cancel();

    public async Task AddProductToShopAsync(string productId)
    {
        var options = new DialogOptions { CloseOnEscapeKey = true };
        var parameters = new DialogParameters<QuestionDialog>();
        parameters.Add(x => x.ContentText, "آیا از افزودن محصول اطمینان دارید ?");
        var dialogReference = await _dialogService.ShowAsync<QuestionDialog>("افزودن محصول از دیجیکالا", parameters, options);
        var result = await dialogReference.Result;
        if (!result.Canceled)
        {

            try
            {
                var token = await _userUtility.GetBearerTokenAsync();
                if (token == null)
                    throw new Exception("Token is null");
                IsProcessing = true;
                await _restWrapper.ScraperRestApi.AddDigiProductToShopAsync(productId,Search, token);
                _snackbar.Add("کالا افزوده شد", Severity.Success);
            }
            catch (ApiException ex)
            {
                var exe = await ex.GetContentAsAsync<ApiResult>();
                _snackbar.Add(exe != null ? exe.Message : ex.Content, Severity.Error);
            }
            catch (Exception e)
            {
                _snackbar.Add(e.Message, Severity.Error);
            }
            finally
            {

                IsProcessing = false;
            }
        }
    }

    public async Task SearchChanged(string search)
    {
        if (search.IsNullOrEmpty() && !Search.IsNullOrEmpty())
            await InitializeAsync();
        Search = search;
    }
    public async Task SearchAsync()
    {
        try
        {
            var token = await _userUtility.GetBearerTokenAsync();
            if (token == null)
                throw new Exception("Token is null");
            if (Search.IsNullOrEmpty())
                throw new AppException("اسم کالا برای جست جو وارد نشده است");
            IsProcessing = true;
            var dto = await _restWrapper.ScraperRestApi.SearchDigiProductsAsync(Search, token);
            PageDto.Clear();
            dto.ForEach(d => PageDto.Add(d));
        }
        catch (ApiException ex)
        {
            var exe = await ex.GetContentAsAsync<ApiResult>();
            _snackbar.Add(exe != null ? exe.Message : ex.Content, Severity.Error);
        }
        catch (Exception e)
        {
            _snackbar.Add(e.Message, Severity.Error);
        }
        finally
        {

            IsProcessing = false;
        }
    }
}