from application.model import (
    OperationDescription,
    BenefitAppliers,
)
from application.model.configuration import Configurations
from application.model.repository.benefit_applier import DefaultBenefitApplierRepository
from application.repository.pos import DBRepository
from typing import Optional, Dict


class UnlockBenefitInteractor(object):

    def __init__(self, configs, db_repository, benefit_appliers_repositories):
        # type: (Configurations, DBRepository, Dict[BenefitAppliers: DefaultBenefitApplierRepository]) -> None

        self.configs = configs
        self.logger = self.configs.logger
        self.db_repository = db_repository
        self.benefit_appliers_repositories = benefit_appliers_repositories

    def unlock_benefit(self, benefit_id, originator_pos_id, benefit_applier, retry_quantity=0):
        # type: (str, int, BenefitAppliers, Optional[int]) -> None

        success = False
        try:
            message = "Unlocking benefit. BenefitId: {}; OriginatorPosId: {}; BenefitApplier: {}; Try: {}"
            self.logger.info(message.format(benefit_id, originator_pos_id, benefit_applier, retry_quantity))

            if benefit_applier not in [None, 'None', '']:
                self.benefit_appliers_repositories[benefit_applier].unlock_benefit(
                    benefit_id=benefit_id,
                    pos_id=originator_pos_id,
                )
            success = True
        except Exception as _:
            self.logger.exception("Error unlocking benefit: {}".format(benefit_id))
        finally:
            self.db_repository.manage_operation_status(success,
                                                       benefit_id,
                                                       originator_pos_id,
                                                       OperationDescription.UNLOCKED,
                                                       benefit_applier,
                                                       retry_quantity)
