from string import upper

from sysactions import (
    action,
    get_model,
    get_poslist,
    get_podtype,
    get_custom,
    set_custom,
    show_listbox,
    translate_message,
)

from xml.etree import cElementTree as eTree

from typing import List


@action
def open_close_totem(
    pos_id,  # type: int
):
    # type: (...) -> None

    totem_id_list = _get_totem_list()
    options = _get_options(pos_id, totem_id_list)
    selected_index = show_listbox(
        posid=pos_id,
        options=options,
        message="$OPEN_CLOSE_TOTEM",
    )
    if selected_index is None:
        return

    index = int(selected_index)
    totem_info = [x for x in totem_id_list if x["index"] == index][0]
    set_custom(
        posid=totem_info["totem_id"],
        key='TOTEM_WAS_CLOSED',
        value=not totem_info["status"],
    )


def _get_options(
    pos_id,         # int
    totem_id_list,  # type: List
):
    # type: (...) -> List

    model = get_model(posid=pos_id)
    options = []
    status_translation_dict = _get_status_translation_dict(model=model)
    for c in totem_id_list:
        if c["status"]:
            status = "({})".format(status_translation_dict["CLOSED"])
        else:
            status = "({})".format(status_translation_dict["OPENED"])
        options.append(status_translation_dict["TOTEM_ID_STATUS"].format(c["totem_id"], status))

    return options


def _get_totem_list():
    # type: (...) -> List

    totem_id_list = []
    index_list = 0
    for pos_list_id in get_poslist():
        totem_info = _get_totem_info(
            index_list=index_list,
            pos_list_id=pos_list_id,
        )
        if totem_info is not None:
            totem_id_list.append(totem_info)
            index_list += 1

    return totem_id_list


def _get_status_translation_dict(
    model,  # type: eTree.XML
):
    # type: (...) -> dict

    status_translation_dict = {
        "CLOSED": translate_message(
            model=model,
            message_key="CLOSED",
        ),
        "OPENED": translate_message(
            model=model,
            message_key="OPENED",
        ),
        "TOTEM_ID_STATUS": translate_message(
            model=model,
            message_key="TOTEM_ID_STATUS",
        ),
    }
    return status_translation_dict


def _get_totem_info(
    index_list,  # type: int
    pos_list_id,  # type: int
):
    # type: (...) -> dict

    model = get_model(posid=pos_list_id)
    pod_type = get_podtype(model=model)
    if pod_type == "TT":
        totem_was_closed = get_custom(
            model=model,
            key='TOTEM_WAS_CLOSED',
        )
        status = False
        if totem_was_closed is None:
            status = False
        else:
            if upper(totem_was_closed) == "TRUE":
                status = True
            elif upper(totem_was_closed) == "FALSE":
                status = False
        return {
            "index": index_list,
            "status": status,
            "totem_id": pos_list_id,
        }
