#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import sys
import traceback
from Reader import *

# Copyright by JK 2018
# License: MIT
# https://www.reportlab.com/docs/reportlab-userguide.pdf

if len(sys.argv) > 1:
  event = sys.argv[1]
else:
  event = "Use ARG1 as event"

if len(sys.argv) > 2:
  attendeesFile = sys.argv[2]
else:
  attendeesFile = "attendees.txt"

print("This script generates a template for the Avery L4785 form")

data = loadData(attendeesFile)

doc = PDFDoc("badges-A4", 21, 29.7)
c = 0
printerCorrection = 0.2

for p in data:
  try:
    # check for image
    if c % 10 == 0:
      offset_x = 1.97 - printerCorrection
    elif c % 10 == 5:
      offset_x = 8 + 1.97 + 1.5 - printerCorrection

    if c % 5 == 0:
      offset_y = 1.35
    else:
      offset_y += 5.5

    img = doc.subImage(offset_x, offset_y , 8, 5)
    img.addImg("../assets/background2", 0, 0, 0.7)

    img.addImg("../assets/reading-logo", 0.1, 0.8, 0.1)
    img.addKeyVal("Department of", 0.37, 0.87, size=0.035, color=(0,0.2,0), font="Helvetica")
    img.addKeyVal("Computer Science", 0.37, 0.81, size=0.05, color=(0,0.2,0), font="Helvetica")
    #img.addImg("logo", 0.7, 0.8, 0.1)

    #img.drawRectBorder(0, 0, 1, 1)
    size = 0.2
    if len(p[0]) > 12:
      size = 0.2 * 12 / len(p[0])

    img.addKeyValCenter(p[0], 0.5, 0.5, size=size, font="Helvetica")
    img.addKeyValCenter(p[1], 0.5, 0.37, size=0.07, font="Helvetica-Oblique")

    img.addKeyValCenter(event, 0.5, 0.2, size=0.07, font="Helvetica-Oblique")

    if c % 10 == 9:
      doc.newpage()
    c+=1
  except:
    print("Unexpected error when processing %s" %(p))
    traceback.print_exc()

doc.save()

print("In printing dialog set the defaults for scaling but use the manual feed!")
