from xml.etree import cElementTree as eTree

from application.interactor import BurnBenefitInteractor
from application.model import BenefitAppliers
from application.model.configuration import Configurations
from application.model.repository.benefit_applier import DefaultBenefitApplierRepository
from application.repository.pos import DBRepository
from application.repository.pos.utils import find_order_benefits_id
from typing import Dict


class OrderModifiedPaidInteractor(object):
    def __init__(
        self,                               # type: OrderModifiedPaidInteractor
        burn_benefit_interactor,            # type: BurnBenefitInteractor
        configs,                            # type: Configurations
        db_repository,                      # type: DBRepository
        benefit_appliers_repositories,      # type: Dict[BenefitAppliers: DefaultBenefitApplierRepository]
    ):
        # type: (...) -> None

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

    def burn_paid_benefit(
        self,                   # type: OrderModifiedPaidInteractor
        current_pos_id,         # type: int
        originator_pos_id,      # type: int
        order_id,               # type: int
        order_picture,          # type: eTree
    ):
        # type: (...) -> None

        order_benefits_id = find_order_benefits_id(self.logger, order_picture)
        if not order_benefits_id:
            return

        message = "Benefits founded to burn: {}; Current/Originator PosId: {}/{}; OrderId: {}"
        self.logger.info(message.format(order_benefits_id, current_pos_id, originator_pos_id, order_id))

        for benefit_id in order_benefits_id:
            benefit_applier = self.db_repository.get_benefit_applier_by_db(benefit_id)
            if benefit_applier not in [None, 'None', '']:
                self.burn_benefit_interactor.burn_benefit(
                    benefit_id,
                    originator_pos_id,
                    benefit_applier,
                    order_picture
                )
