from typing import (
    Dict,
    Any,
)

from pos_model import OrderTender

from models import (
    NfeTender,
    MaxNfceFields,
)
from utils import get_t_pag


class TPagService(object):

    def __init__(
        self,            # type: TPagService
        tenders_type,    # type: Dict[str, Any]
    ):
        # type: (...) -> None

        self.tenders_type = tenders_type
        self.static_pix_qty = 0
        self.dynamic_pix_qty = 0

    def get_t_pag(
        self,      # type: TPagService
        tender,    # type: OrderTender
    ):
        # type: (...) -> int

        t_pag = get_t_pag(
            tender=tender,
            tenders_type=self.tenders_type,
        )
        if t_pag in [NfeTender.pix_estatico, NfeTender.pagamento_instantaneo_pix]:
            return self._get_new_t_pag_if_maximum_reached(t_pag=t_pag)

        return t_pag

    def _get_new_t_pag_if_maximum_reached(
        self,    # type: TPagService
        t_pag,   # type: int
    ):
        # type: (...) -> int

        if t_pag == NfeTender.pix_estatico and not self.static_pix_qty:
            self.static_pix_qty = 1
        elif t_pag == NfeTender.pagamento_instantaneo_pix:
            self.dynamic_pix_qty += 1

        if self._sum() > MaxNfceFields.max_obs_cont:
            return NfeTender.outros

        return t_pag

    def _sum(
        self,    # type: TPagService
    ):
        # type: (...) -> int

        return self.static_pix_qty + (self.dynamic_pix_qty * 2)
