from logging import Logger

from model.config import Config
from model.electronic_message import ElectronicMessage
from model.error_type import PosValidationErrorsType
from model.exception import ValidationError
from model.repository import PosRepositoryModel
from interactor.route.electronic_payment.send_responses.dto import SendResponsesRequest
from validation_util import validate_api_key, check_if_business_day_is_opened, \
    check_if_operator_is_logged, check_if_order_is_totaled
from typing import List, Any


class SendResponsesInteractor(object):
    def __init__(self, logger, config, pos_repository, fiscal_service, electronic_messages):
        # type: (Logger, Config, PosRepositoryModel, Any, List[ElectronicMessage]) -> None

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

    def execute(self, request):
        # type: (SendResponsesRequest) -> None

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

        model = self.pos_repository.get_model(pos_id)

        check_if_business_day_is_opened(self.pos_repository, model)
        check_if_operator_is_logged(self.pos_repository, model)
        check_if_order_is_totaled(self.pos_repository, model)

        self.fiscal_service.send_electronic_payment_message_response(model, request)
