from helper import translate_pos_label
from sysactions import (
    action,
    get_model,
    get_poslist,
    show_text_preview,
    translate_message,
    get_podtype,
)

from actions.pos.close_day import close_day

@action
def all_pos_status(
    pos_id,  # type: str
):
    # type: (...) -> bool

    while True:
        data = _generate_all_pos_data()
        result = show_text_preview(
            posid=pos_id,
            texts=data,
            title='$SELECT_ITEM',
            buttons='$OK|$CLOSE_POS',
            defvalue='',
            onthefly=False,
            timeout=120000,
        )
        if result is not None and len(result) >= 1:
            button_clicked = result[0]
            if button_clicked == '1':
                if len(result) >= 2:
                    pos_id_to_close = result[1]
                    close_day(pos_id_to_close, pos_id)
                else:
                    return False
            else:
                return True


def _generate_all_pos_data():
    data = []
    for pos_id_to_list in sorted(get_poslist()):
        model = get_model(pos_id_to_list)

        pod_type = get_podtype(model)
        if pos_id_to_list > 0 and pod_type not in ("TT", "DL"):
            operator_label = translate_message(
                model=model,
                message_key="OPERATOR",
            )
            name_label = translate_message(
                model=model,
                message_key="NAME",
            )
            status = model.find("PosState").get("state")
            if status == "UNDEFINED":
                status = "CLOSED"

            status_message = translate_message(
                model=model,
                message_key=status,
            )

            business_period = model.find("PosState").get("period")
            if business_period != "0":
                business_period = business_period[6:8] + "/" + business_period[4:6] + "/" + business_period[0:4]
            else:
                business_period = ""

            operators = ""
            for operator in model.find("Operators"):
                operator_id = operator.get("id")
                operator_name = operator.get("name")
                operator_status = translate_message(
                    model=model,
                    message_key=operator.get("state"),
                )
                operators += OPERATOR_INFO.format(
                    operator_label,
                    operator_id,
                    name_label,
                    operator_name,
                    operator_status,
                )
            message = POS_STATUS_INFO.format(
                translate_message(
                    model=model,
                    message_key=status,
                ),
                translate_message(
                    model=model,
                    message_key="BUSINESS_PERIOD",
                ),
                business_period,
                operators,
            )

            pos = translate_pos_label(model=model)
            status_title = "{}{} ({}) - {} - {}".format(pos, pos_id_to_list, pod_type, status_message, business_period)
            data.append([pos_id_to_list, status_title, message, ])
    return data


OPERATOR_INFO = """
    {}:
        ID: {}
        {}: {}
        Status: {}

"""

POS_STATUS_INFO = """
Status: {}
{}: {}
{}
"""

