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

import sysactions

from bustoken import TK_FISCALWRAPPER_CANCEL_ORDER
from msgbus import FM_PARAM, TK_SYS_ACK
from mw_helper import show_filtered_list_box_dialog
from typing import List

from .. import mb_context


def void_order(pos_id, last_paid_order="0", order_id='', abandon=0, void_reason=9, pos_ot=None):
    model = sysactions.get_model(pos_id)
    if pos_ot is None:
        pos_ot = sysactions.get_posot(model)

    order_id = int(order_id) if order_id != "" else ""
    void_order_id = order_id
    if (type(void_reason) == int) or not void_reason:
        void_reason = get_void_reason_id(pos_id, void_reason)
    if not order_id:
        order = sysactions.get_current_order(model)
        order_id = order.get('orderId')

    pos_ot.setOrderCustomProperties(void_reason, str(order_id))
    pos_ot.voidOrder(int(pos_id), int(last_paid_order), void_order_id, abandon)

    return True


def void_fiscal_order(pos_id, order_id):
    data = "|".join([str(pos_id), str(order_id)])
    msg = mb_context.MB_EasySendMessage("FiscalWrapper", TK_FISCALWRAPPER_CANCEL_ORDER, format=FM_PARAM, data=data)
    if msg.token != TK_SYS_ACK:
        sysactions.show_messagebox(pos_id, message="$ERROR_CANCEL_FISCAL_ORDER|{}".format(msg.data))
        raise sysactions.StopAction(msg.data)


def get_void_reason_id(pos_id, selected_index=None):

    void_reason_ids = range(1, 13)
    void_reason_options = get_void_reason_options(pos_id)
    void_reason_to_show = void_reason_options[0:4]

    if not selected_index:
        message = "$SELECT_REASON"
        selected_index = show_filtered_list_box_dialog(pos_id, void_reason_to_show, message, mask="NOFILTER")

    if selected_index is None:
        return None

    void_reason_id = void_reason_ids[int(selected_index)]
    void_reason_description = void_reason_options[int(selected_index)]
    void_reason_dict = {'VOID_REASON_ID': void_reason_id, 'VOID_REASON_DESCR': void_reason_description}

    return void_reason_dict


def get_void_reason_options(
    pos_id  # type: str
):
    # type: (...) -> List[str]

    void_reason_options = ["CHANGED_YOUR_MIND",
                           "DUPLICATE",
                           "WRONG_SALE",
                           "CANCELLATION",
                           "COUPON_CANCELED",
                           "PAYMENT_CANCELED",
                           "SAVED_ORDER_CANCELED",
                           "ERROR_CREATING_SALE",
                           "ERROR_PROCESSING_PAYMENT_SELECTION",
                           "CANCELLATION_VIA_SYSTEM",
                           "SYNC_ERROR",
                           "MANAGER_AUTHORIZATION_CANCELLATION"]

    return _translate_array(pos_id, void_reason_options)


def _translate_array(pos_id, messages):
    # type: (str, List[str]) -> List[str]

    model = sysactions.get_model(pos_id)
    result = []
    for index, message in enumerate(messages, start=1):
        result.append("{} - {}".format(index, sysactions.translate_message(model, message)))

    return result
