import csv
import time
from datetime import datetime

from PyQt5.QtWidgets import QApplication

from src.components.os_label_printer import *
from src.lib.helpers import ConfigReader

SYSTEM_ID = "test-linux"
CSV_PATH="tmp/ferrari_labels5.csv"
TEMPLATE="ferrari_flag_qr_only.prn"
config = ConfigReader(system_id=SYSTEM_ID)
printer=Os_Label_Printer(config=config,name="label_printer")


# timenow = datetime.now()
app = QApplication(sys.argv)

with open(CSV_PATH, "r", encoding="utf-8-sig") as f:
    reader = csv.DictReader(f)
    for row in reader:
        START_SN = 1
        STOP_SN = int(row["quantity"])
        PN = row["part_number"]
        print(f"PART NUMBER # {PN}")
        input("Press Enter to continue...")
        for sn in range(START_SN,STOP_SN+2):
            timenow = datetime.now()
            print(f"PRINTING LABEL # {sn}")
            context = {
                # RECIPE DATA
                "PART": PN,
                # SERIAL DEFINITION
                "SN": str(sn),
                "SN4": f"{sn:0>4}",
                "SN5": f"{sn:0>5}",
                "SN6": f"{sn:0>6}",
                # TIME DEFINITION
                "DATETIME": timenow.strftime("%d/%m/%Y %H:%M:%S"),
                "DATE": timenow.strftime("%d/%m/%Y"),
                "TIME": timenow.strftime("%H:%M:%S"),
                "YYYY": timenow.strftime("%Y"),
                "YY": timenow.strftime("%y"),
                "MO": timenow.strftime("%m"),
                "DD": timenow.strftime("%d"),
                "HH": timenow.strftime("%H"),
                "MI": timenow.strftime("%M"),
                "SS": timenow.strftime("%S"),
                "JJJ": timenow.strftime("%j"),
            }
            printer.print_label(TEMPLATE,context)

            time.sleep(1)