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

@inject IFeedDataService feedDataService
@inject AppLifecycleService appLifecycleService

@if (FeedGroups == null)
{
	<div class="d-flex justify-content-center">
		<Spinner Type="SpinnerType.Grow" Color="SpinnerColor.Primary" Size="SpinnerSize.Large"/>
	</div>
}
else
{
	<div id="feeds-page">
		<div class="container-fluid">
			<section id="filter">
				<div class="row">
					@foreach (IGrouping<int, int> sGrouping in SourceGroupings)
					{
						<div class="col col-lg-1">
							<SwitchWithBadge
								Source="(RssFeedSources)sGrouping.Key"
								OnFilterChanged="OnFilterChanged"
								Text="@GetRssSourceName((RssFeedSources)sGrouping.Key)"
								Count="@sGrouping.Count()">
							</SwitchWithBadge>
						</div>
					}
				</div>
			</section>
			<section id="feeds-list">
				@foreach (FeedGroupVM feedGrp in FeedGroups)
				{
					<h2>
						<Icon Name="IconName.Calendar2Event"/> @feedGrp.PublishDate.ToLocalTime().ToString("dd.MM.yyyy")
					</h2>
					@foreach (FeedVM feed in feedGrp.Feeds)
					{
						<div class="col-xs-12 col-sm-12 col-md-9 col-lg-7 col-xl-5">
							<Card>
								<CardBody>
									<CardTitle>@feed.Title</CardTitle>
									<CardText>@feed.Description</CardText>
									<div class="row">
										<Button Outline="true" Color="ButtonColor.Primary" Class="btn-sm align-self-end ml-auto" To="@feed.Link" Type="ButtonType.Link"><Icon Name="IconName.Link"/> Öffnen</Button>
									</div>
								</CardBody>
								@* <ul class="list-group list-group-flush"> *@
								@* 	<li class="list-group-item"> *@
								@* 		<div class="row"> *@
								@* 			<Button Outline="true" Color="ButtonColor.Primary" Class="btn-sm align-self-end ml-auto" To="@feed.Link" Type="ButtonType.Link"><Icon Name="IconName.Link"/> Öffnen</Button> *@
								@* 		</div> *@
								@* 	</li> *@
								@* </ul> *@
								<CardFooter>

									@GetRssSourceName((RssFeedSources)@feed.Source) | @feed.PubDate?.ToLocalTime().ToString("dd.MM.yyyy HH:mm")

								</CardFooter>
							</Card>
							<br/>
						</div>
					}
				}
			</section>
		</div>
	</div>
}

@code {

	[Inject] private IFeedDataService FeedDataService { get; set; }

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

	private List<FeedGroupVM>? FeedGroups { get; set; }

	private IEnumerable<IGrouping<int, int>> SourceGroupings { get; set; } = new List<IGrouping<int, int>>();

	private List<RssFeedSources> activeFeedSources = new List<RssFeedSources>();


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

		LoadFeeds();
	}


	private void SetFilter()
	{
		this.activeFeedSources.Clear();

		IEnumerable<int> enumerable = this.SourceGroupings.Select(grp => grp.Key).Distinct();

		foreach (int i in enumerable)
		{
			this.activeFeedSources.Add((RssFeedSources)i);
		}
	}


	private async void LoadFeeds()
	{
		// Copy feed list to keep original list
		await FeedDataService.LoadFeeds();
		FeedGroups = [.. FeedDataService.Feeds.FeedGroups];

		List<FeedGroupVM> listTmp = [.. FeedDataService.Feeds.FeedGroups];

		SourceGroupings = listTmp.SelectMany(fg => fg.Feeds.Select(f => f.Source)).GroupBy(sources => sources);

		SetFilter();

		this.FilterFeeds();

		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 "?";
		}
	}


	private void OnFilterChanged(FilterFeedEventArg eventArg)
	{
		if (eventArg.Value && !this.activeFeedSources.Contains(eventArg.Source))
		{
			activeFeedSources.Add(eventArg.Source);
		}
		else if (this.activeFeedSources.Contains(eventArg.Source))
		{
			activeFeedSources.Remove(eventArg.Source);
		}

		this.FilterFeeds();

		StateHasChanged();
	}


	private void FilterFeeds()
	{
		this.FeedGroups = new List<FeedGroupVM>();
		foreach (FeedGroupVM fg in this.FeedDataService.Feeds.FeedGroups)
		{
			List<FeedVM> feeds = fg.Feeds.Where(f => this.activeFeedSources.Contains((RssFeedSources)f.Source)).ToList();
			if (feeds.Any())
			{
				this.FeedGroups.Add(
					new FeedGroupVM
					{
						Feeds = feeds,
						PublishDate = fg.PublishDate
					});
			}
		}
	}


}