@implements IDisposable

<AuthorizeView>
    <Authorized>
        <li class="nav-item">
            <a href="#" class="icons">
                <i class="ti-search" aria-hidden="true"></i>
            </a>
        </li>

        <li class="nav-item">
            <a href="/SdvShop/Shop/ShoppingCart" class="icons">
                <i class="ti-shopping-cart">@this.ShoppingCartState.ShoppingCart.Count</i>
            </a>
        </li>
        <li class="nav-item">
            <a href="/Profile/@context.User.Identity.Name" class="icons">
                <i class="ti-user" aria-hidden="true"></i>
            </a>
        </li>
        <li class="nav-item">
            <a href="/SdvShop/Shop/FavoriteProducts" class="icons">
                <i class="ti-heart" aria-hidden="true">@this.FavoriteProductsState.FavoriteProducts.Count</i>
            </a>
        </li>
        <li class="nav-item">
            <a href="/SdvShop/Shop/TrackOrder" class="icons">
                <i class="fas fa-truck"></i>
            </a>
        </li>
    </Authorized>
    <NotAuthorized>
        <li class="nav-item">
            <a href="#" class="icons">
                <i class="ti-search" aria-hidden="true"></i>
            </a>
        </li>

        <li class="nav-item">
            <a href="/SdvShop/Shop/ShoppingCart" class="icons">
                <i class="ti-shopping-cart">@this.ShoppingCartState.ShoppingCart.Count</i>
            </a>
        </li>
        <li class="nav-item">
            <a href="/SdvShop/Shop/FavoriteProducts" class="icons">
                <i class="ti-heart" aria-hidden="true">@this.FavoriteProductsState.FavoriteProducts.Count</i>
            </a>
        </li>
        <li class="nav-item">
            <a href="/SdvShop/Shop/TrackOrder" class="icons">
                <i class="fas fa-truck"></i>
            </a>
        </li>
    </NotAuthorized>
</AuthorizeView>

@code {
    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        if (firstRender)
        {
            var shoppingCartItems = await this.SessionStorage.GetItemAsync<Dictionary<string, int>>("CodeShop");
            if (shoppingCartItems?.Count == null)
            {
                await this.SessionStorage.SetItemAsync("CodeShop", new Dictionary<string, int>());
            }
            else
            {
                shoppingCartItems = await this.ShoppingCartState.UpdateSession(shoppingCartItems);
                await this.SessionStorage.SetItemAsync("CodeShop", shoppingCartItems);
            }

            var favoriteProducts = await this.LocalStorage.GetItemAsync<List<string>>("CodeShopFavProducts");
            if (favoriteProducts?.Count == null)
            {
                await this.LocalStorage.SetItemAsync("CodeShopFavProducts", new List<string>());
            }
            else
            {
                favoriteProducts = await this.FavoriteProductsState.UpdateSession(favoriteProducts);
                await this.LocalStorage.SetItemAsync("CodeShopFavProducts", favoriteProducts);
            }
        }
    }

    protected override void OnInitialized()
    {
        this.ShoppingCartState.OnChange += this.StateHasChanged;
        this.FavoriteProductsState.OnChange += this.StateHasChanged;
    }

    public void Dispose()
    {
        this.ShoppingCartState.OnChange -= this.StateHasChanged;
        this.FavoriteProductsState.OnChange -= this.StateHasChanged;
    }
}