from datetime import datetime
from functools import partial
from typing import Optional, Callable

from rio import Component, Rectangle, Text, TextStyle, Column, Row, TextInput, DateInput, MultiLineTextInput, IconButton, Color, Button, ThemeContextSwitcher


class NewsPost(Component):
    title: str = ""
    text: str = ""
    date: str = ""
    subtitle: str = ""
    author: str = ""

    def build(self) -> Component:
        return Rectangle(
            content=Column(
                Row(
                    Text(
                        self.title,
                        grow_x=True,
                        margin=2,
                        margin_bottom=0,
                        fill=self.session.theme.background_color,
                        style=TextStyle(
                            font_size=1.3
                        ),
                        overflow="ellipsize"
                    ),
                    Text(
                        self.date,
                        margin=2,
                        align_x=1,
                        fill=self.session.theme.background_color,
                        style=TextStyle(
                            font_size=0.6
                        ),
                        overflow="wrap"
                    )
                ),
                Text(
                    self.subtitle,
                    grow_x=True,
                    margin=2,
                    margin_top=0,
                    margin_bottom=0,
                    fill=self.session.theme.background_color,
                    style=TextStyle(
                        font_size=0.8
                    ),
                    overflow="ellipsize"
                ),
                Text(
                    self.text,
                    margin=2,
                    fill=self.session.theme.background_color,
                    overflow="wrap"
                ),
                Text(
                    f"Geschrieben von {self.author}",
                    align_x=0,
                    grow_x=True,
                    margin=2,
                    margin_top=0,
                    margin_bottom=1,
                    fill=self.session.theme.background_color,
                    style=TextStyle(
                        font_size=0.5,
                        italic=True
                    ),
                    overflow="nowrap"
                )
            ),
            fill=self.session.theme.primary_color,
            margin_left=1,
            margin_right=1,
            margin_top=2,
            margin_bottom=1,
            shadow_radius=0.5,
            shadow_color=self.session.theme.hud_color,
            shadow_offset_y=0,
            corner_radius=0.2
        )


class EditableNewsPost(NewsPost):
    news_id: int = -1
    save_cb: Callable = lambda _: None
    delete_cb: Callable = lambda _: None

    def set_prop(self, prop, value) -> None:
        self.__setattr__(prop, value)

    def build(self) -> Component:
        return ThemeContextSwitcher(
            content=Rectangle(
                content=Column(
                    Row(
                        TextInput(
                            text=self.title,
                            label="Titel",
                            style="rounded",
                            min_width=15,
                            on_change=lambda e: self.set_prop("title", e.text)
                        ),
                        DateInput(
                            value=datetime.strptime(self.date, "%d.%m.%Y"),
                            style="rounded",
                            on_change=lambda e: self.set_prop("date", e.value.strftime("%d.%m.%Y"))
                        )
                    ),
                    TextInput(
                        text=self.subtitle,
                        label="Untertitel",
                        style="rounded",
                        grow_x=True,
                        on_change=lambda e: self.set_prop("subtitle", e.text)
                    ),
                    MultiLineTextInput(
                        text=self.text,
                        label="Text",
                        style="rounded",
                        grow_x=True,
                        min_height=12,
                        on_change=lambda e: self.set_prop("text", e.text)
                    ),
                    Row(
                        TextInput(
                            text=self.author,
                            label="Autor",
                            style="rounded",
                            grow_x=True,
                            on_change=lambda e: self.set_prop("author", e.text)
                        ),
                        Rectangle(content=Button(icon="material/delete", style="major", color="danger", shape="rectangle", on_press=partial(self.delete_cb, self.news_id)), fill=Color.from_hex("0b7372")),
                        Rectangle(content=Button(icon="material/save", style="major", color="success", shape="rectangle", on_press=partial(self.save_cb, self)), fill=Color.from_hex("0b7372"))
                    )
                ),
                fill=self.session.theme.primary_color,
                margin_left=1,
                margin_right=1,
                margin_top=2,
                margin_bottom=1,
                shadow_radius=0.2,
                shadow_color=self.session.theme.background_color,
                shadow_offset_y=0,
                corner_radius=0.2
            ),
            color="primary"
        )
