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


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

    CSS = """

    Grid {
        grid-size: 5 20;
        grid-rows: 2 2 2 2 2 3 2 2 2 2 2 6 2 2 6 2 2 2 2 6;
        grid-columns: 25 25 25 25 225;
        height: 100%;
        width: 100%;
        padding: 1;
    }

    Input {
        border: none;
    }
    Button {
        border: none;
    }
    SelectionList {
        border: none;
        min-height: 6;
    }
    Select {
        border: none;
    }

    DataTable {
        min-height: 6;
    }

    DataTable .datatable--cursor {
        background: darkorange;
        color: black;
    }

    DataTable .datatable--header {
        background: steelblue;
        color: white;
    }

    #toplabel {
        height: 1;
    }

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

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

    def __init__(self, key=None, value=None):
        super().__init__()
        self.__key = key
        self.__value = value


    def on_mount(self):

        if self.__key is not None:
            self.query_one("#key_input", Input).value = str(self.__key)

        if self.__value is not None:
            self.query_one("#value_input", Input).value = str(self.__value)


    def compose(self):

        yield Header()

        with Grid():

            # 8
            yield Static("Key")
            yield Input(id="key_input", classes="four")

            yield Static("Value")
            yield Input(id="value_input", classes="four")

            # 17
            yield Static(" ", classes="five")

            # 18
            yield Button("Save", id="save_button")
            yield Button("Cancel", id="cancel_button")

            # 19
            yield Static(" ", classes="five")

            # 20
            yield Static(" ", classes="five", id="messagestatic")

        yield Footer(id="footer")


    def getTagFromInput(self):

        tagKey = self.query_one("#key_input", Input).value
        tagValue = self.query_one("#value_input", Input).value

        return (tagKey, tagValue)


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

        # Check if the button pressed is the one we are interested in
        if event.button.id == "save_button":
            self.dismiss(self.getTagFromInput())

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