from application.model import Configurations
from application.service import BenefitService
from application.service import OrderService


class OnOrderTotalInteractor(object):
    def __init__(self, configs, benefit_service, order_service):
        # type: (Configurations, BenefitService, OrderService) -> None

        self.configs = configs
        self.logger = configs.logger
        self.benefit_service = benefit_service
        self.order_service = order_service

    def execute(self, pos_id):
        # type: (int) -> None

        order_picture = self.benefit_service.apply_automatic_benefits_on_order(pos_id, on_total=True)
        if not order_picture:
            order_picture = self.order_service.get_order_picture(pos_id)

        if order_picture.get("state") == "TOTALED":
            product_groups = self.benefit_service.product_groups
            order_picture = self.benefit_service.validate_benefits(pos_id, order_picture, product_groups)
            order_picture = self.benefit_service.remove_invalid_benefits(pos_id, order_picture)
            self.benefit_service.apply_pending_benefits(pos_id, order_picture)
            self.benefit_service.apply_manual_pending_benefits(pos_id, order_picture)
