from typing import List

from basereport import TableReport, ReportColumnDefinition, AlignTypes
from report import Part
from report.command import AlignCommand, RepeatCommand, FontCommand
from ._ReportBuilder import ReportBuilder
from ..dto import TableReportHeaderDto


class TableReportHeader(ReportBuilder):
    def __init__(self, table_report_header_dto, pre_account_with_discount):
        # type: (TableReportHeaderDto, bool) -> None
        self.table_report_header_dto = table_report_header_dto
        self.pre_account_with_discount = pre_account_with_discount

    def generate(self):
        # type: () -> List[Part]

        report_header = []
        if not self.pre_account_with_discount:
            report_header.extend(
                    [
                        Part("$REPORT_ITEMS_CONFERENCE", [AlignCommand(AlignCommand.Alignment.center)]),
                        a_new_line(),
                        a_new_line()
                    ]
            )

        return self._format_report_header(self.pre_account_with_discount, report_header)

    def _format_report_header(self, show_order_id, report_header):
        report_header.extend([
            Part(commands=[FontCommand(FontCommand.Font.A)]),
            Part("$TABLE {}".format(self.table_report_header_dto.table_name[:20]),
                 [AlignCommand(AlignCommand.Alignment.center)]),
            a_new_line()
        ])

        if show_order_id:
            order_id_part = Part(
                    "$SHORT_REFERENCE {}".format(self.table_report_header_dto.order_id[:20]),
                    [AlignCommand(AlignCommand.Alignment.center)]
            )
            report_header.extend([order_id_part, a_new_line()])

        report_header.extend([
            Part("$OPERATOR_LABEL {}".format(self.table_report_header_dto.user_name[:20]),
                 [AlignCommand(AlignCommand.Alignment.center)]),
            a_new_line(),
            Part("$SEATS_NUMBER {}".format(self.table_report_header_dto.service_seats),
                 [AlignCommand(AlignCommand.Alignment.center)]),
            a_new_line(),
            a_new_line(),
            TableReport(lines=[["$START_HOUR", self._start_hour()]], column_definitions=a_time_column_definition()),
            TableReport(
                    lines=[["$TOTALED_HOUR", self._totaled_hour()]],
                    column_definitions=a_time_column_definition()
            ),
            a_new_line(),
            Part(commands=[RepeatCommand(38, "=")]),
            a_new_line(),

        ])
        return report_header

    def _start_hour(self):
        return "#DATETIME({})".format(self.table_report_header_dto.start_date.isoformat())

    def _totaled_hour(self):
        return "#DATETIME({})".format(self.table_report_header_dto.totaled_date.isoformat())


def a_new_line(commands=None):
    return Part("\n", commands=commands)


def a_time_column_definition():
    return [
        ReportColumnDefinition(width=13,
                               fill_with_char=" ",
                               before_text="",
                               align=AlignTypes.LEFT),
        ReportColumnDefinition(width=25,
                               fill_with_char="",
                               before_text="",
                               align=AlignTypes.RIGHT)

    ]
