@inherits RefreshablePageBase
@page "/"
@using RBLFeederCommon.Enums
@using RBLFeederCommon.Models.RssFeed
@using RBLNews.Shared.Services

@inject IFeedDataService feedDataService
@inject AppLifecycleService appLifecycleService

@if (FeedDataService.Feeds == null)
{
    <div class="d-flex justify-content-center">
        Lade Feeds ...<br/><br/>
        <Spinner Type="SpinnerType.Grow" Color="SpinnerColor.Primary" Size="SpinnerSize.Large" />
    </div>
}
else
{
    <br/>
    <div id="feeds-page" class="row">
        <div class="container">
            @foreach (FeedGroupVM feedGrp in this.FeedDataService.Feeds.FeedGroups)
            {
                <h4><Icon Name="IconName.Calendar2Event" /> @feedGrp.PublishDate.ToLocalTime().ToString("dd.MM.yyyy")</h4>
                @foreach (FeedVM feed in feedGrp.Feeds)
                {
                    <div class="col-xs-12 col-sm-12 col-md-8 col-lg-4">
                        <Card>
                            <CardBody>
                                <CardTitle>@feed.Title</CardTitle>
                                <CardText>@feed.Description</CardText>
                            </CardBody>
                            <ul class="list-group list-group-flush">
                                <li class="list-group-item">
                                    @GetRssSourceName((RssFeedSources)@feed.Source) | @feed.PubDate?.ToLocalTime()
                                </li>
                            </ul>
                            <CardFooter>
                                <div class="row">
                                    <Button Color="ButtonColor.Primary" Class="btn-sm align-self-end ml-auto" To="@feed.Link" Type="ButtonType.Link"><Icon Name="IconName.Link" /> Öffnen</Button>
                                </div>
                            </CardFooter>
                        </Card>
                        <br />
                    </div>

                }
            }
        </div>
    </div>
}
@code {
    
    [Inject]
    private IFeedDataService FeedDataService { get; set; }

    [Inject]
    private AppLifecycleService AppLifecycleService { get; set; }

    protected override void OnInitialized()
    {
        AppLifecycleService.OnActivated = LoadFeeds;

        LoadFeeds();
    }

    private async void LoadFeeds()
    {
        await FeedDataService.LoadFeeds();
        StateHasChanged();
    }

    private string GetRssSourceName(RssFeedSources source)
    {
        switch (source)
        {
            case RssFeedSources.RbLive:
            case RssFeedSources.NitterRbLive:
                return "RBLive!";
            case RssFeedSources.NitterFabrizioRomano:
                return "Fabrizio Romano";
            case RssFeedSources.Lvz:
                return "LVZ";
            case RssFeedSources.Kicker:
                return "Kicker";
            case RssFeedSources.Bild:
                return "BILD";
            case RssFeedSources.Transfermarkt:
                return "Transfermarkt";
            default:
                return "?";
        }
    }
}

