using Mapster;
using NetinaShop.Domain.Entities.Warehouses;

namespace NetinaShop.AdminPanel.PWA.Dialogs;

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


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

    public ShippingActionDialogBoxViewModel(ISnackbar snackbar,
        IRestWrapper restWrapper,
        IUserUtility userUtility,
        IDialogService dialogService,
        MudDialogInstance mudDialog,
        ShippingSDto shipping)
    {
        _snackbar = snackbar;
        _restWrapper = restWrapper;
        _userUtility = userUtility;
        _dialogService = dialogService;
        _mudDialog = mudDialog;
        PageDto = shipping;
        IsEditing = true;
    }
    public bool IsEditing = false;



    public void Cancel() => _mudDialog.Cancel();

    public async Task SubmitEditAsync()
    {
        try
        {
            IsProcessing = true;
            if (PageDto.Id == default)
                throw new Exception("Id is null !");

            var token = await _userUtility.GetBearerTokenAsync();
            if (token == null)
                throw new Exception("Token is null");

            if (PageDto.Name.IsNullOrEmpty())
                throw new Exception("نام را وارد کنید");

            if (PageDto.WarehouseName.IsNullOrEmpty())
                throw new Exception("نام انبار را وارد کنید");

            var request = PageDto.Adapt<UpdateShippingCommand>();
            await _restWrapper.CrudApiRest<Shipping, Guid>(Address.ShippingController).Update<UpdateShippingCommand>(request, token);
            _snackbar.Add($"ویرایش {PageDto.Name} با موفقیت انجام شد", Severity.Success);
            _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 SubmitCreateAsync()
    {
        try
        {
            IsProcessing = true;

            var token = await _userUtility.GetBearerTokenAsync();
            if (token == null)
                throw new Exception("Token is null");

            if (PageDto.Name.IsNullOrEmpty())
                throw new Exception("نام را وارد کنید");

            if (PageDto.WarehouseName.IsNullOrEmpty())
                throw new Exception("نام انبار را وارد کنید");
            var request = PageDto.Adapt<CreateShippingCommand>();
            await _restWrapper.CrudApiRest<Shipping, Guid>(Address.ShippingController).Create<CreateShippingCommand>(request, token);
            _snackbar.Add($"ساخت {PageDto.Name} با موفقیت انجام شد", Severity.Success);
            _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;
        }
    }
}