from typing import List, Tuple
from pygame import Rect, image
from pygame_gui.core.interfaces import IUIManagerInterface, IContainerLikeInterface
from pygame_gui.elements import UIDropDownMenu
from pymasep.interface.ui_input import UIInput


class UIChoice(UIInput):
    """
    UI Input to choose among list of choices

    :param relative_rect: Position and size of the choice
    :param manager: Manager (from pygame_gui)
    :param container: Container of the UIChoice (form pygame_gui)
    :param label_text: Text of the label
    :param waited_type: Expected type of the value chosen
    :param error_ressource: Image displayed when an error occurs
    :param choices: list of choice (1st element is displayed, 2nd is the value)
    """

    def __init__(self, relative_rect: Rect,
                 manager: IUIManagerInterface,
                 container: IContainerLikeInterface,
                 label_text: str,
                 waited_type: type,
                 error_ressource: image,
                 choices: List[Tuple[str, str]]):
        super().__init__(relative_rect, manager, container=container, label_text=label_text, waited_type=waited_type,
                         error_ressource=error_ressource)

        font_info = self.ui_manager.get_theme().get_font_info(self.object_ids)
        self.text_entry_pos = Rect((self.label_pos[0] + self.label_pos[2] + 10, 0),
                                   (self.get_size()[0] * 0.5, int(font_info['size']) * 3))
        self.choices = choices
        options_list = [c[0] for c in self.choices]
        self.ddmenu = UIDropDownMenu(relative_rect=self.text_entry_pos,
                                     options_list=options_list,
                                     starting_option=options_list[0],
                                     manager=self.ui_manager,
                                     container=self)

    def set_text(self, text: str):
        """
        Set the text to display.

        :param text: Parameter is not used. First choice at initialization
        """
        self.ddmenu.selected_option = self.choices[0][0]

    def get_text(self) -> str:
        """
        Get the value of selected element

        :return: the 2nd element of the chosen choice
        """
        return [t[1] for t in self.choices if t[0] == self.ddmenu.selected_option][0]
