import json
import logging

from typing import Optional
from xml.etree import cElementTree as eTree

from mw_helper import show_qr_code
from sysactions import show_messagebox

from utilfunctions import (
    get_custom_property_value,
    parse_qr_tag,
)

show_welcome_message = {}
show_final_message = {}


class LoyaltyInteractor(object):
    def __init__(
        self,
        pos_id,      # type: str
    ):
        # type: (...) -> None

        self.pos_id = pos_id
        self.logger = logging.getLogger("PosActions")

    def set_show_customer_welcome_message(
        self,
        show,    # type: bool
    ):
        # type: (...) -> None

        global show_welcome_message

        if self.pos_id not in show_welcome_message:
            show_welcome_message[self.pos_id] = show

    def get_show_customer_welcome_message(
        self,
    ):
        # type: (...) -> bool

        global show_welcome_message

        if self.pos_id in show_welcome_message:
            return show_welcome_message[self.pos_id]

        return False

    def set_show_customer_final_message(
        self,
        show,    # type: bool
    ):
        # type: (...) -> None

        global show_final_message

        if self.pos_id not in show_final_message:
            show_final_message[self.pos_id] = show

    def show_loyalty_customer_final_message(
        self,
        order_picture=None,      # type: Optional[eTree.Element]
    ):
        # type: (...) -> None

        self.logger.debug("showing customer final message")

        try:
            loyalty_customer_info = get_custom_property_value(
                pos_id=self.pos_id,
                custom_property="LOYALTY_CUSTOMER_INFO",
                order_picture=order_picture,
            )
            loyalty_customer_data = json.loads(loyalty_customer_info or "{}")
        except Exception:  # noqa
            self.exception("error getting/parsing loyalty customer info")
            loyalty_customer_data = {}

        customer_info = loyalty_customer_data.get("customerInfo", {})
        additional_info = customer_info.get("additionalInfo", {})
        end_sale_message = additional_info.get("endSaleMessage", None)

        if end_sale_message and self._show_final_message():
            title = "$LOYALTY_FINAL_MESSAGE_CUSTOMER_HEADER"
            message, qr_data = parse_qr_tag(text=end_sale_message)
            if qr_data:
                show_qr_code(
                    pos_id=self.pos_id,
                    code_data=qr_data,
                    buttons="$OK",
                    title=title,
                    info_text=message,
                )
            else:
                show_messagebox(
                    posid=self.pos_id,
                    message=message,
                    title=title,
                )

    def _show_final_message(
        self,
    ):
        # type: (...) -> bool

        global show_final_message

        if self.pos_id in show_final_message:
            return show_final_message[self.pos_id]

        return False
