import click

from textual.screen import Screen
from textual.widgets import Header, Footer, Static, Button
from textual.containers import Grid

from .shifted_season_controller import ShiftedSeasonController

from ffx.model.shifted_season import ShiftedSeason


# Screen[dict[int, str, int]]
class ShiftedSeasonDeleteScreen(Screen):

    CSS = """

    Grid {
        grid-size: 2;
        grid-rows: 2 auto;
        grid-columns: 30 330;
        height: 100%;
        width: 100%;
        padding: 1;
    }

    Input {
        border: none;
    }
    Button {
        border: none;
    }
    #toplabel {
        height: 1;
    }

    .two {
        column-span: 2;
    }

    .box {
        height: 100%;
        border: solid green;
    }
    """

    def __init__(self, showId = None, patternId = None, shiftedSeasonId = None):
        super().__init__()

        self.context = self.app.getContext()
        self.Session = self.context['database']['session'] # convenience

        self.__ssc = ShiftedSeasonController(context = self.context)

        self._showId = showId
        self._patternId = patternId
        self.__shiftedSeasonId = shiftedSeasonId


    def on_mount(self):

        shiftedSeason: ShiftedSeason = self.__ssc.getShiftedSeason(self.__shiftedSeasonId)

        ownerLabel = (
            f"pattern #{self._patternId}"
            if self._patternId is not None
            else f"show #{self._showId}"
        )
        self.query_one("#static_owner", Static).update(ownerLabel)
        self.query_one("#static_original_season", Static).update(str(shiftedSeason.getOriginalSeason()))
        self.query_one("#static_first_episode", Static).update(str(shiftedSeason.getFirstEpisode()))
        self.query_one("#static_last_episode", Static).update(str(shiftedSeason.getLastEpisode()))
        self.query_one("#static_season_offset", Static).update(str(shiftedSeason.getSeasonOffset()))
        self.query_one("#static_episode_offset", Static).update(str(shiftedSeason.getEpisodeOffset()))


    def compose(self):

        yield Header()

        with Grid():

            yield Static("Are you sure to delete the following shifted season?", id="toplabel", classes="two")

            yield Static(" ", classes="two")

            yield Static("from")
            yield Static(" ", id="static_owner")

            yield Static(" ", classes="two")

            yield Static("Source season")
            yield Static(" ", id="static_original_season")

            yield Static("First episode")
            yield Static(" ", id="static_first_episode")

            yield Static("Last episode")
            yield Static(" ", id="static_last_episode")

            yield Static("Season offset")
            yield Static(" ", id="static_season_offset")

            yield Static("Episode offset")
            yield Static(" ", id="static_episode_offset")

            yield Static(" ", classes="two")

            yield Button("Delete", id="delete_button")
            yield Button("Cancel", id="cancel_button")

        yield Footer()


   # Event handler for button press
    def on_button_pressed(self, event: Button.Pressed) -> None:

        if event.button.id == "delete_button":

            if self.__shiftedSeasonId is None:
                raise click.ClickException('ShiftedSeasonDeleteScreen.on_button_pressed(): shifted season id is undefined')

            if self.__ssc.deleteShiftedSeason(self.__shiftedSeasonId):
                self.dismiss(self.__shiftedSeasonId)

            else:
                #TODO: Meldung
                self.app.pop_screen()

        if event.button.id == "cancel_button":
            self.app.pop_screen()
