# encoding: utf-8
from datetime import datetime

from report import Generator
from reportsdata.tefcancellationreport.dto import (
    TefCancellationGeneratorDto,
    TefCancellationHeaderDto,
    TefCancellationBodyDto
)
from tefcancellationreport import TefCancellationReport
from tefcancellationreport.repository import OrderRepository
from utils.report_formatters import remove_accents


class TefCancellationReportGenerator(Generator):

    def __init__(
        self,
        order_repository,   # type: OrderRepository
        initial_date,       # type: str
        end_date            # type: str
    ):
        # type: (...) -> None

        self.order_repository = order_repository
        self.initial_date = initial_date
        self.end_date = end_date

    def generate_data(
        self
    ):
        # type: (...) -> TefCancellationGeneratorDto

        tef_cancellation_report = TefCancellationReport(order_repository=self.order_repository)
        tenders_by_date = tef_cancellation_report.get_tef_tenders_by_date(
            initial_date=self.initial_date,
            end_date=self.end_date
        )
        card_brands = tef_cancellation_report.get_card_brands()
        header = TefCancellationHeaderDto(
            title=remove_accents("$TEF_CANCELLATION_REPORT_38_CHARS").upper(),
            start_date=self.initial_date,
            end_date=self.end_date
        )
        body = TefCancellationBodyDto(
            tenders_by_date=tenders_by_date,
            card_brands=card_brands
        )

        return TefCancellationGeneratorDto(
            header=header,
            body=body
        )
