import json
from xml.etree.ElementTree import Element

from typing import List


def has_electronic_payment(
        order   # type: Element
):
    # type: (...) -> bool

    if not order:
        return False

    tenders = order.findall("TenderHistory/Tender")
    for tender in tenders:
        if _get_electronic_type_id(tender) not in (0, 9):
            return True

    return False


def get_electronic_tenders(
        order   # type: Element
):
    # type: (...) -> List

    tenders = order.findall("TenderHistory/Tender")
    ret_tenders = []
    for tender in tenders:
        electronic_type = _get_electronic_type_id(tender)
        if electronic_type in (1, 2):
            ret_tenders.append({
                "id": int(tender.get("tenderId")),
                "type": int(tender.get("tenderType")),
                "amount": tender.get("tenderAmount"),
                "electronicType": electronic_type,
                "detail": json.loads(tender.get("tenderDetail", "{}"))
            })

    return ret_tenders


def _get_electronic_type_id(
        tender   # type: Element
):
    # type: (...) -> int

    return int(tender.get("electronicType") or tender.get("eletronicType"))
