@model CategoryNavigationModel.CategoryLineModel

@functions{
    public bool BreadCrumbContainsCurrentCategoryId(CategorySimpleModel category)
    {
        if (Model.CurrentCategoryId == 0)
            return false;

        if (category.Id == Model.CurrentCategoryId)
            return true;

        foreach (var subCategory in category.SubCategories)
        {
            if (BreadCrumbContainsCurrentCategoryId(subCategory))
            {
                return true;
            }
        }

        return false;
    }
}
@{
    var active = Model.Category.Id == Model.CurrentCategoryId || Model.Category.SubCategories.Count(BreadCrumbContainsCurrentCategoryId) > 0;
    var last = Model.Category.Id == Model.CurrentCategoryId;
    var liClass = active ? "active" : "inactive";
    if (last)
    {
        liClass += " last";
    }
}
<li class="@liClass">
    <a href="@Url.RouteUrl("Category", new { SeName = Model.Category.SeName })">@Model.Category.Name
        @if (Model.Category.NumberOfProducts.HasValue)
        {
            <text> </text>@T("Categories.TotalProducts", Model.Category.NumberOfProducts.Value)
        }
    </a>
    @{
        if (Model.Category.Id == Model.CurrentCategoryId ||
            Model.Category.SubCategories.Count(BreadCrumbContainsCurrentCategoryId) > 0)
        {
            if (Model.Category.SubCategories.Count > 0)
            {
                <ul class="sublist">
                    @foreach (var subCategory in Model.Category.SubCategories)
                    {
                        var categoryLineModel = new CategoryNavigationModel.CategoryLineModel
                        {
                            CurrentCategoryId = Model.CurrentCategoryId,
                            Category = subCategory
                        };
                        @await Html.PartialAsync("_CategoryLine.Navigation", categoryLineModel)
                    }
                </ul>
            }
        }
    }
</li>
