from typing import (
    Dict,
    Any,
)

from helper import remove_accents
from pos_model import (
    TenderType,
    OrderTender,
)

from models import (
    PosTenderToNfeTender,
    NfeTender,
)


def is_tef_tender(
    tender,    # type: OrderTender
):
    # type: (...) -> bool

    tef_tenders = [
        TenderType.credit,
        TenderType.debit,
        TenderType.voucher,
        TenderType.gift_card,
        TenderType.digital_wallet,
    ]

    return tender.type in tef_tenders


def get_t_pag(
    tender,          # type: OrderTender
    tenders_type,    # type: Dict[str, Any]
):
    # type: (...) -> int

    if is_tef_tender(tender=tender):
        return int(tender.t_pag or 99)

    tender_description = str(remove_accents(tender.descr)).upper()
    if tender_description in tenders_type:
        tender_type_data_base = tenders_type[tender_description]
        t_pag = tender_type_data_base.get("tPag", None)
        if _is_not_blank(value=t_pag):
            return int(t_pag)

    tender_type = PosTenderToNfeTender.convert(pos_tender=tender.type)
    if tender_type not in [NfeTender.dinheiro, NfeTender.cash_back]:
        return NfeTender.outros

    return tender_type


def _is_not_blank(
    value   # type: str
):
    # type: (...) -> bool

    if value and value.strip():
        return True

    return False
