from commons.report import Generator
from salechallengereport import SaleChallengeReport
from dto import SaleChallengeHeaderDto, SaleChallengeGeneratorDto


class SaleChallengeGenerator(Generator):
    def __init__(self, order_repository, product_repository, sale_challenge_repository, sale_challenge_id, operator_id):
        self.order_repository = order_repository
        self.product_repository = product_repository
        self.sale_challenge_repository = sale_challenge_repository
        self.sale_challenge_id = sale_challenge_id
        self.operator_id = operator_id

    def generate_data(self):
        sale_challenge_report = SaleChallengeReport(self.sale_challenge_repository, self.product_repository,
                                                    self.order_repository, self.operator_id)
        operator_name = sale_challenge_report.get_operator_name()
        sale_challenge = sale_challenge_report.get_sale_challenge(self.sale_challenge_id)
        sale_items = sale_challenge_report.get_sales_items(sale_challenge)
        header = SaleChallengeHeaderDto(sale_challenge, operator_name)
        columns = 3 if operator_name else 2
        return SaleChallengeGeneratorDto(header, sale_items, columns)
