import { PDFDocument, rgb, StandardFonts } from 'pdf-lib'
import fontkit from '@pdf-lib/fontkit'

export const generateUrkunde = async (data, schule, name, unterschrift, vorlage = false, typ = 'sim') => {
  const pdfDoc = await PDFDocument.create()

  pdfDoc.registerFontkit(fontkit)
  const Helvetica = await pdfDoc.embedFont(StandardFonts.Helvetica)
  const HelveticaBold = await pdfDoc.embedFont(StandardFonts.HelveticaBold)
  const Droid = await pdfDoc.embedFont(DroidSans)

  const pngImage = unterschrift ? await pdfDoc.embedPng(unterschrift) : undefined

  for (let i = 0; i < data.length; i++) {
    let v

    if (vorlage) {
      if (typ === 'sim') {
        const vorlageDoc = await PDFDocument.load(urkundeSIM)
        const [tmp] = await pdfDoc.copyPages(vorlageDoc, [0])
        v = tmp
      } else if (typ === 'lktf23') {
        const vorlageDoc = await PDFDocument.load(urkundeLKTF23)
        const [tmp] = await pdfDoc.copyPages(vorlageDoc, [0])
        v = tmp
      }
    }

    pdfDoc.addPage(v)
  }

  data.forEach((d, i) => {
    const page = pdfDoc.getPage(i)

    const { width } = page.getSize()

    let textWidth = 0

    if (typ === 'lktf23') {
      const title = 'Wettbewerb 4-IN-motion'
      textWidth = Helvetica.widthOfTextAtSize(title, 24)
      page.drawText(title, {
        x: 0.5 * (width - textWidth),
        y: 520,
        size: 24,
        font: Helvetica,
        color: rgb(0.347, 0.355, 0.375)
      })
    }

    textWidth = Droid.widthOfTextAtSize(name, 10)
    page.drawText(name, {
      x: 120 - 0.5 * textWidth,
      y: 52.85,
      size: 10,
      font: Droid,
      color: rgb(0.11, 0.11, 0.106)
    })

    textWidth = Droid.widthOfTextAtSize(schule, 7)
    page.drawText(schule, {
      x: 120 - 0.5 * textWidth,
      y: 38.85,
      size: 7,
      font: Droid,
      color: rgb(0.11, 0.11, 0.106)
    })

    if (pngImage) {
      page.drawImage(pngImage, {
        x: 120 - 0.5 * 100,
        y: 60,
        width: 100,
        height: 50
      })
    }

    textWidth = HelveticaBold.widthOfTextAtSize(d.platz, 24)
    page.drawText(d.platz, {
      x: 0.5 * (width - textWidth),
      y: 530 - (typ === 'lktf23' ? 55 : 0),
      size: 24,
      font: HelveticaBold,
      color: rgb(0.769, 0.18, 0.125)
    })

    textWidth = HelveticaBold.widthOfTextAtSize(d.zeit, 24)
    page.drawText(d.zeit, {
      x: 0.5 * (width - textWidth),
      y: 490 - (typ === 'lktf23' ? 55 : 0),
      size: 24,
      font: HelveticaBold,
      color: rgb(0.769, 0.18, 0.125)
    })

    textWidth = Helvetica.widthOfTextAtSize(d.team, 20)
    page.drawText(d.team, {
      x: 0.5 * (width - textWidth),
      y: 450 - (typ === 'lktf23' ? 55 : 0),
      size: 20,
      font: Helvetica,
      color: rgb(0.769, 0.18, 0.125)
    })
  })

  const pdfBytes = await pdfDoc.save()

  saveByteArray('Urkunden.pdf', pdfBytes)
}

const saveByteArray = (fileName, bytes) => {
  const blob = new Blob([bytes], { type: 'application/pdf' })
  const link = document.createElement('a')
  link.href = window.URL.createObjectURL(blob)
  link.download = fileName
  link.click()
}

const urkundeSIM = 'data:application/pdf;base64,[encoded data, 856824 characters]'

const urkundeLKTF23 = 'data:application/pdf;base64,[encoded data, 335000 characters]'

const DroidSans = 'data:@file/x-font-ttf;base64,[encoded data, 54704 characters]'
