from mw_helper import show_message_dialog
from sysactions import (
    get_model,
    translate_message,
    show_any_dialog,
)
from typing import List


def get_custom_table_seats(
    pos_id,              # type: str
    service_seats,       # type: str
    seats_options,       # type: List[str]
):
    # type: (...) -> List[str]

    model = get_model(posid=pos_id)
    title = translate_message(
        model=model,
        message_key="SELECT_SEATS",
    )
    while True:
        response = show_any_dialog(
            posid=pos_id,
            dlgtype="multipleSelectListBox",
            message="",
            title=title.replace("{0}", service_seats),
            infotext="|".join(seats_options),
            buttons="$OK|$CANCEL",
            icon="",
            timeout=600000,
            mask="",
            defvalue="",
            minvalue="",
            maxvalue="",
            contents=None,
            asynch=False,
        )
        if response is None:
            return []

        custom_seats = sorted([seats_options[int(item)] for item in response.split(",")], key=int)
        if len(custom_seats) == int(service_seats):
            return custom_seats

        max_selected_seats_msg = len(custom_seats) > int(service_seats)
        message_title = "$MAX_SELECTED_SEATS" if max_selected_seats_msg else "$MIN_SELECTED_SEATS"

        show_message_dialog(
            pos_id=pos_id,
            title="$INFORMATION",
            message=message_title,
        )