import ui

class MyApp(ui.View):
    def __init__(self):
        screen_width, screen_height = ui.get_screen_size()
        view_width = min(screen_width, screen_height)
        view_height = max(screen_width, screen_height)
        
        self.frame = (0, 0, view_width, view_height)
        self.name = 'Crib'
        self.flex = 'WH'
        self.bg_color = 'black'  # Set background color to black
        self.button1_pressed = None
        
        self.page1()

    def create_buttons(self, buttons_text, rows, cols, action):
        button_width = self.width / cols
        button_height = self.height / rows
        
        for i, text in enumerate(buttons_text):
            row = i // cols
            col = i % cols
            btn = ui.Button(title=text)
            btn.action = lambda sender=btn: action(sender.title)
            btn.frame = (col * button_width, row * button_height, button_width, button_height)
            btn.tint_color = 'white'  # Set button text color to white
            btn.border_width = 1  # Add a 1-point border
            btn.border_color = 'white'  # Set border color to white
            btn.alignment = ui.ALIGN_CENTER  # Center text
            btn.font = ('<system>', 18)
            self.add_subview(btn)

    def page1(self, sender=None):
        self.clear_subviews()

        buttons_text = ["5C", "7C", "7H", "9D", "7D", "R"]
        self.create_buttons(buttons_text, 3, 2, self.on_button_press)

    def page2(self, sender=None):
        self.clear_subviews()

        buttons_text = ["0", "1", "3", "5", "6", "8"]
        self.create_buttons(buttons_text, 3, 2, self.on_button_press)

    def results_page(self, button1, button2):
        self.clear_subviews()

        result_label = ui.Label()
        result_text = self.get_result(button1, button2)
        result_label.text = result_text.replace('. ', '.\n')  # Separate In Hand and In Head lines
        result_label.frame = (10, (self.height - 100) / 2, self.width - 20, self.height - 100)
        result_label.number_of_lines = 0
        result_label.font = ('<system-bold>', 20)
        result_label.text_color = 'white'  # Set text color to white
        result_label.alignment = ui.ALIGN_CENTER  # Center text
        self.add_subview(result_label)

    def get_result(self, button1, button2):
        results = {
            ("5C", "0"): "Empty",
            ("5C", "1"): "Empty",
            ("5C", "3"): "In Hand: 6C.\nIn Head: AS 2C",
            ("5C", "5"): "Empty",
            ("5C", "6"): "Empty",
            ("5C", "8"): "In Hand: 6C.\nIn Head: 10C",
            ("7C", "0"): "In Hand: 5C.\nIn Head: AH 2H",
            ("7C", "1"): "In Hand: 5C.\nIn Head: AS 2S",
            ("7C", "3"): "In Hand: 5C.\nIn Head: 4C",
            ("7C", "5"): "In Hand: 5C.\nIn Head: 6H 10H",
            ("7C", "6"): "In Hand: 5C.\nIn Head: 6S 10S",
            ("7C", "8"): "In Hand: 5C.\nIn Head: 9C JC KC",
            ("7H", "0"): "In Hand: 7C.\nIn Head: 4H 5H",
            ("7H", "1"): "In Hand: 7C.\nIn Head: 4S 5S",
            ("7H", "3"): "In Hand: 7C.\nIn Head: 3C",
            ("7H", "5"): "In Hand: 7C.\nIn Head: 9H JH KH",
            ("7H", "6"): "In Hand: 7C.\nIn Head: 9S JS KS",
            ("7H", "8"): "In Hand: 7C.\nIn Head: 8C QC",
            ("9D", "0"): "In Hand: 7H.\nIn Head: 3S",
            ("9D", "1"): "In Hand: 7H.\nIn Head: AD 2D 6D",
            ("9D", "3"): "In Hand: 7H.\nIn Head: 3H",
            ("9D", "5"): "In Hand: 7H.\nIn Head: 8S QS",
            ("9D", "6"): "In Hand: 7H.\nIn Head: 10D",
            ("9D", "8"): "In Hand: 7H.\nIn Head: 8H QH",
            ("7D", "0"): "Empty",
            ("7D", "1"): "Empty",
            ("7D", "3"): "In Hand: 9D.\nIn Head: 4D 5D",
            ("7D", "5"): "Empty",
            ("7D", "6"): "Empty",
            ("7D", "8"): "In Hand: 9D.\nIn Head: JD KD",
            ("R", "0"): "Empty",
            ("R", "1"): "Empty",
            ("R", "3"): "In Hand: 7D.\nIn Head: 3D",
            ("R", "5"): "Empty",
            ("R", "6"): "Empty",
            ("R", "8"): "In Hand: 7D.\nIn Head: 8D QD"
        }
        return results.get((button1, button2), "No specific result")

    def on_button_press(self, button_text):
        if button_text in ["5C", "7C", "7H", "9D", "7D", "R"]:
            self.button1_pressed = button_text
            self.page2()
        elif button_text in ["0", "1", "3", "5", "6", "8"]:
            self.results_page(button1=self.button1_pressed, button2=button_text)

    def clear_subviews(self):
        for subview in list(self.subviews):
            self.remove_subview(subview)

if __name__ == '__main__':
    app = MyApp()
    app.present(style='sheet', animated=False)