import click

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

from ffx.track_descriptor import TrackDescriptor


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

    CSS = """

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

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

    .two {
        column-span: 2;
    }
    .three {
        column-span: 3;
    }
    .four {
        column-span: 4;
    }

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

    def __init__(self, trackDescriptor : TrackDescriptor):
        super().__init__()

        if type(trackDescriptor) is not TrackDescriptor:
            raise click.ClickException('TrackDeleteScreen.init(): trackDescriptor is required to be of type TrackDescriptor')

        self.__trackDescriptor = trackDescriptor


    def on_mount(self):

        self.query_one("#subindexlabel", Static).update(str(self.__trackDescriptor.getSubIndex()))
        self.query_one("#patternlabel", Static).update(str(self.__trackDescriptor.getPatternId()))
        self.query_one("#languagelabel", Static).update(str(self.__trackDescriptor.getLanguage().label()))
        self.query_one("#titlelabel", Static).update(str(str(self.__trackDescriptor.getTitle())))


    def compose(self):

        yield Header()

        with Grid():
            
            #1
            yield Static(f"Are you sure to delete the following {self.__trackDescriptor.getType().label()} track?", id="toplabel", classes="four")

            #2
            yield Static("sub index")
            yield Static(" ", id="subindexlabel", classes="three")

            #3
            yield Static("from pattern")
            yield Static(" ", id="patternlabel", classes="three")

            #4
            yield Static(" ", classes="four")

            #5
            yield Static("Language")
            yield Static(" ", id="languagelabel", classes="three")

            #6
            yield Static("Title")
            yield Static(" ", id="titlelabel", classes="three")

            #7
            yield Static(" ", classes="four")

            #8
            yield Static(" ", classes="four")

            #9
            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":
            self.dismiss(self.__trackDescriptor)

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