from escposformatter import DefaultEscPosFormatter
from htmlformatter import HtmlFormatter
from report import ReportParser
from sysactions import generate_report, print_text, show_print_preview, show_messagebox

from enum import Enum

from utilfunctions import (
    get_device_service,
    get_sector_name,
)


class PrinterType(Enum):
    esc_pos = "ESC_POS"
    apos = "APOS"


def print_report(
    pos_id,
    report_name,
    preview,
    order,
    *args
):
    report_data = generate_report(report_name, pos_id, *args)
    report = ReportParser().parse(report_data)

    sector_name = get_sector_name(order=order) if order else ""
    current_printer = get_device_service(
        pos_id=pos_id,
        device_type="printer",
        sector=sector_name,
    )

    if report is not None:
        if preview:
            print_preview_report = HtmlFormatter().format(report)
            if show_print_preview(pos_id, print_preview_report.encode("utf-8")) in (1, None):
                return False

        text = DefaultEscPosFormatter().format(report)
        print_text(
            posid=pos_id,
            model=None,
            text=text,
            force_printer=current_printer,
        )
    elif report is None:
        show_messagebox(pos_id, "$NO_REPORT_DATA", "$ERROR")
    else:
        return print_text(
            posid=pos_id,
            model=None,
            text=report_data,
            preview=preview,
            force_printer=current_printer,
        )
