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

import utils
import actions.util
from actions.login.get_user_info import get_user_info
from model.customexception import GettingUserInfoException, CanceledDialogException

from sysactions import (
    action,
    get_model,
    show_messagebox,
    StopAction,
    AuthenticationFailed
)

from login_user import sign_in_quick_user
from signin_user import sign_in_table_user
from actions.pos import pos_must_be_blocked
from typing import Optional


@action
def common_login(
        pos_id,         # type: str
        user_id=None,   # type: Optional[str]
        password=None   # type: Optional[str]
):
    # type: (...) -> bool

    model = get_model(pos_id)
    _login_validations(model, pos_id, user_id)

    try:
        user_info = get_user_info(pos_id, user_id, password, model)
    except CanceledDialogException as _:
        raise StopAction()
    except (AuthenticationFailed, GettingUserInfoException) as _:
        show_messagebox(pos_id, "$INVALID_USERNAME_OR_PASSWORD", "$INFORMATION")
        raise StopAction()

    working_mode = model.find("WorkingMode").get("usrCtrlType")
    if working_mode == "QSR":
        return sign_in_quick_user(pos_id, user_info)
    else:
        return sign_in_table_user(pos_id, user_info)


def _login_validations(model, pos_id, user_id):
    if pos_must_be_blocked(pos_id):
        show_messagebox(pos_id, message="$POS_BLOCKED", icon="error")
        raise StopAction()

    if not utils.check_current_day(pos_id):
        raise StopAction()

    if actions.util.is_user_already_logged(pos_id, user_id):
        raise StopAction()
