from rio import Column, Component, event

from src.ezgg_lan_manager import ConfigurationService, NewsService
from src.ezgg_lan_manager.components.NewsPost import NewsPost
from src.ezgg_lan_manager.types.News import News


class NewsPage(Component):
    news_posts: list[News] = []

    @event.on_populate
    async def on_populate(self) -> None:
        await self.session.set_title(f"{self.session[ConfigurationService].get_lan_info().name} - Neuigkeiten")
        self.news_posts = (await self.session[NewsService].get_news())[:8]

    def build(self) -> Component:
        posts = [NewsPost(
            title=news.title,
            subtitle=news.subtitle,
            text=news.content,
            date=news.news_date.strftime("%d.%m.%Y"),
            author=news.author.user_name
        ) for news in self.news_posts]
        return Column(
                *posts,
                align_y=0,
            )
