# -*- coding: utf-8 -*-

from helper import translate_pos_label
from typing import (
    Optional,
    Tuple,
)

from xml.etree.ElementTree import Element

import sysactions
from mw_helper import (
    show_message_dialog,
    show_filtered_list_box_dialog,
)

from actions.login.common_close_user import set_authorizer

from actions.login.signout_user import sign_out_user
from actions.util.list_users import get_opened_users
from custom_sysactions import user_authorized_to_execute_action


@sysactions.action
def close_operators_manager_menu(
    pos_id,                         # type: str
    current_operator_id=None,       # type: Optional[str]
):
    # type: (...) -> bool

    model = sysactions.get_model(pos_id)
    manager_id = sysactions.get_custom(
        model=model,
        key='Last Manager ID',
    )

    logged_out_user = execute_sign_out_user(
        pos_id=pos_id,
        current_operator_id=current_operator_id,
        auth_data=manager_id,
    )

    set_authorizer(
        pos_id=pos_id,
        authorizer=manager_id,
        is_quick_service=False,
    )

    return logged_out_user


@sysactions.action
@user_authorized_to_execute_action
def close_operators(
    pos_id,                  # type: str
    current_operator_id,     # type: str
    auth_data=None,          # type: Optional[str]
):
    # type: (...) -> bool

    return execute_sign_out_user(
        pos_id=pos_id,
        current_operator_id=current_operator_id,
        auth_data=auth_data,
    )


def execute_sign_out_user(
    pos_id,                 # type: str
    current_operator_id,    # type: str
    auth_data=None,         # type: Optional[str]
):
    # type: (...) -> bool

    model = sysactions.get_model(posid=pos_id)
    sysactions.check_current_order(
        posid=pos_id,
        model=model,
        need_order=False,
    )

    pos_to_close_user, user = select_user_to_close(pos_id=pos_id)
    if user is None:
        return False

    return sign_out_user(
        pos_id=pos_id,
        user_id=user.get("id"),
        pos_to_close_user=pos_to_close_user,
        current_operator_id=current_operator_id,
        auth_data=auth_data,
    )


def select_user_to_close(
    pos_id,     # type: str
):
    # type: (...) -> Tuple[Optional[int], Optional[Element]]

    pos_user_list = get_opened_users()

    if len(pos_user_list) == 0:
        show_message_dialog(pos_id, message="$THERE_ARE_NO_OPENED_USERS")
        return None, None

    model = sysactions.get_model(posid=pos_id)
    pos = translate_pos_label(model=model)

    parsed_users_options = []
    for pos_user in pos_user_list:
        prefix = "{}{:02d}"
        user = pos_user[1]
        working_mode = user.get("userControlType")
        if working_mode == "TS":
            prefix = "TS"
        else:
            prefix = prefix.format(pos, pos_user[0])

        parsed_users_options.append("{} - {} - {}".format(prefix, user.get('id'), user.get('name')))

    index = show_filtered_list_box_dialog(
        pos_id=pos_id,
        options="|".join(parsed_users_options),
        title="$LOGOUT",
        message="",
        mask="NOFILTER",
    )
    if index is None:
        return None, None

    pos_to_close_user = pos_user_list[index][0]
    user = pos_user_list[index][1]
    return pos_to_close_user, user
