using Microsoft.AspNetCore.Components;
using FrontOffice.Main.Utilities;

namespace FrontOffice.Main.Pages.Gateway;

public partial class StoreChooser : IDisposable
{
    [Inject] private CartService CartService { get; set; } = default!;
    [Inject] private DiscountCartService DiscountCartService { get; set; } = default!;

    private int _cartCount;
    private int _discountCartCount;

    protected override async Task OnInitializedAsync()
    {
        await CartService.EnsureInitializedAsync();
        await DiscountCartService.EnsureInitializedAsync();

        _cartCount = CartService.Count;
        _discountCartCount = DiscountCartService.Count;

        CartService.OnChange += OnCartChanged;
        DiscountCartService.OnChange += OnDiscountCartChanged;
    }

    private void OnCartChanged()
    {
        _cartCount = CartService.Count;
        InvokeAsync(StateHasChanged);
    }

    private void OnDiscountCartChanged()
    {
        _discountCartCount = DiscountCartService.Count;
        InvokeAsync(StateHasChanged);
    }

    public void Dispose()
    {
        CartService.OnChange -= OnCartChanged;
        DiscountCartService.OnChange -= OnDiscountCartChanged;
    }
}
