namespace Netina.AdminPanel.PWA.Pages;

public class HomeViewModel : BaseViewModel<HomeDashboardDto>
{

    private readonly ISnackbar _snackbar;
    private readonly IUserUtility _userUtility;
    private readonly IRestWrapper _restWrapper;
    private readonly IDialogService _dialogService;
    private readonly NavigationManager _navigationManager;

    public HomeViewModel(ISnackbar snackbar, IUserUtility userUtility, IRestWrapper restWrapper, IDialogService dialogService,NavigationManager navigationManager) : base(userUtility)
    {
        _snackbar = snackbar;
        _userUtility = userUtility;
        _restWrapper = restWrapper;
        _dialogService = dialogService;
        _navigationManager = navigationManager;
    }

    public override async Task InitializeAsync()
    {
        try
        {
            IsProcessing = true;

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

            var dto = await _restWrapper.DashboardApiRest.GetHomeDashboardAsync(token);
            PageDto = dto;
            var changeLog = await _restWrapper.UserRestApi.GetChangeLogAsync(token);
            if (changeLog.IsNewVersion)
            {
                DialogOptions maxWidth = new DialogOptions() { MaxWidth = MaxWidth.Medium, FullWidth = true, NoHeader = true, DisableBackdropClick = true };
                var parameters = new DialogParameters<ChangeLogDialogBox> { { x => x.AdminChangeLog,changeLog } };
                await _dialogService.ShowAsync<ChangeLogDialogBox>("", parameters, maxWidth);
            }

        }
        catch (ApiException ex)
        {
            var exe = await ex.GetContentAsAsync<ApiResult>();
            if (ex.StatusCode == HttpStatusCode.Unauthorized)
            {
                await _userUtility.LogoutAsync();
                _navigationManager.NavigateTo("login", true, true);
            }
            _snackbar.Add(exe != null ? exe.Message : ex.Content, Severity.Error);
        }
        catch (Exception e)
        {
            _snackbar.Add(e.Message.Contains("Failed to fetch") ? "اینترنت خود را بررسی نمایید" : e.Message, Severity.Error);
        }
        finally
        {

            IsProcessing = false;
        }
        await base.InitializeAsync();
    }


    public async Task AddBlogClicked()
    {
        DialogOptions maxWidth = new DialogOptions() { MaxWidth = MaxWidth.Medium, FullWidth = true, DisableBackdropClick = true };
        await _dialogService.ShowAsync<BlogActionDialogBox>("افزودن بلاگ جدید", maxWidth);
    }
    public async Task AddProductClicked()
    {
        DialogOptions maxWidth = new DialogOptions() { MaxWidth = MaxWidth.Large, FullWidth = true, DisableBackdropClick = true };
        var dialogResult = await _dialogService.ShowAsync<ProductActionDialogBox>("افزودن محصول جدید", maxWidth);
        var result = await dialogResult.Result;
        if (!result.Canceled && result.Data is bool and true)
        {
            await InitializeAsync();
        }
    }

    public async Task AddBrandClicked()
    {
        DialogOptions maxWidth = new DialogOptions() { MaxWidth = MaxWidth.Medium, FullWidth = true, DisableBackdropClick = true };
        await _dialogService.ShowAsync<BrandActionDialogBox>("افزودن برند جدید", maxWidth);
    }
}