@using WebSharpRSS.Models
@using Serilog
@using ToolQit.Extensions

@inject IDialogService _dialogService

<MudStack Spacing="2" Class="ma-2">
    @if (Items != null)
    {
        foreach (var feedItemData in Items)
        {
            <MudItem @onclick="@(() => Callback(feedItemData))">
                <MudPaper Height="250" Width="300" Class="px-2">
                    <div style="justify-self: start;" class="d-flex align-center">
                        @if (feedItemData.SyndicationParent.Category != null && !feedItemData.SyndicationParent.Category.Icon.IsNullEmptyWhiteSpace())
                        {
                            <MudTooltip Text="@feedItemData.SyndicationParent.Category.Name">
                                <MudIcon Icon="@feedItemData.SyndicationParent.Category.Icon" Class="mr-1" Style="@($"color:{feedItemData.SyndicationParent.Category.HexColor}")"/>
                            </MudTooltip>
                        }
                        @if (feedItemData.SyndicationParent.ImageUrl != null)
                        {
                            <MudTooltip Text="@feedItemData.SyndicationParent.Title">
                                <MudImage Src="@feedItemData.SyndicationParent.ImageUrl" ObjectFit="ObjectFit.Contain" Width="20" Height="20"/>
                            </MudTooltip>
                        }
                        <div class="d-inline ma-2 align-center" style="font-size: 20px; overflow: hidden; display: -webkit-box; -webkit-line-clamp: 1; line-clamp: 1; -webkit-box-orient: vertical;">
                            @if (feedItemData.Title != null)
                            {
                                @((MarkupString)feedItemData.Title)
                            }
                        </div>
                        <MudText Typo="Typo.subtitle2" Color="Color.Secondary"> - @feedItemData.SyndicationParent.Title</MudText>
                    </div>
                    <div style="overflow: hidden; display: -webkit-box; -webkit-line-clamp: 2; line-clamp:2; -webkit-box-orient: vertical;">
                        @if (feedItemData.Description != null && !feedItemData.Description.IsNullEmptyWhiteSpace())
                        {
                            @((MarkupString)feedItemData.Description)
                        }
                        else
                        {
                            <MudText Color="Color.Dark" Typo="Typo.body2">No description found!</MudText>
                        }
                    </div>
                    <MudText Typo="Typo.overline">@feedItemData.PublishingDate.ToString()</MudText>
                </MudPaper>
            </MudItem>
        }
    }
    else
    {
        <MudAlert Severity="Severity.Info" Variant="Variant.Filled">No items to load!</MudAlert>
    }
</MudStack>

@code {
    [Parameter]
    public HashSet<SyndicationItemData>? Items { get; set; }
    DialogOptions _dialogOptions = new DialogOptions() { FullWidth = true, MaxWidth = MaxWidth.ExtraLarge, NoHeader = false, CloseButton = true, CloseOnEscapeKey = true };
    
    private void Callback(SyndicationItemData syndicationItem)
    {
        var parameters = new DialogParameters();
        parameters.Add("Data", syndicationItem);
        _dialogService.Show<ReadDialog>("Custom", parameters, _dialogOptions);
        Log.Verbose("Item: {ItemId} clicked", syndicationItem.Link);
    }

}