from logging import Logger

from interactor.route.electronic_payment.cancel_current_transaction.dto import CancelCurrentTransactionRequest
from model.config import Config
from model.error_type import PosValidationErrorsType
from model.exception import ValidationError
from model.repository import PosRepositoryModel
from services import FiscalService
from validation_util import validate_api_key


class CancelCurrentTransactionInteractor(object):
    def __init__(
        self,               # type: CancelCurrentTransactionInteractor
        logger,             # type: Logger
        config,             # type: Config
        fiscal_service,     # type: FiscalService
        pos_repository,     # type: PosRepositoryModel
    ):
        # type: (...) -> None

        self.logger = logger
        self.config = config
        self.fiscal_service = fiscal_service
        self.pos_repository = pos_repository

    def execute(
        self,       # type: CancelCurrentTransactionInteractor
        request,    # type: CancelCurrentTransactionRequest
    ):
        # type: (...) -> None

        pos_id = validate_api_key(
            request=request,
            secure_api_key_list=self.config.api_key_mapping,
        )
        if not pos_id:
            raise ValidationError(PosValidationErrorsType.PosIdNotFound)

        model = self.pos_repository.get_model(pos_id=pos_id)

        self.fiscal_service.cancel_electronic_payment(
            model=model,
            request=request,
        )
