import { fontkit, PDFDocument, PDFPage, rgb, shuffle } from "./deps.ts";
import { JSONType, Voca } from "../types.ts";

function downloadBuffer(buffer: Uint8Array, fileName: string) {
  const a: HTMLAnchorElement = document.createElement("a");
  const blob = new Blob([buffer], { type: "application/octet-stream" });

  a.href = URL.createObjectURL(blob);
  a.download = fileName;
  document.body.appendChild(a);
  a.style.display = "none";
  a.click();
  a.remove();
}

export async function makePdf(voca_list: Voca[]) {
  const pdfDoc = await PDFDocument.create();

  const fontBytes = await fetch("./NanumGothic.ttf").then((res) =>
    res.arrayBuffer()
  );
  pdfDoc.registerFontkit(fontkit);
  const customFont = await pdfDoc.embedFont(fontBytes);

  const fontSize = 12;
  const margin = 20;
  const perColumn = 25;
  const perPage = 50;

  let page!: PDFPage;
  let col = -1;
  let yCursor = 1;

  // 문제 생성
  for (let i = 0; i < voca_list.length; ++i) {
    const data = voca_list[i];
    if (i % perPage === 0) {
      page = pdfDoc.addPage();
      yCursor = 1;
      col = -1;
    }
    if (i % perColumn === 0) {
      col++;
      yCursor = 1;
    }
    page.drawText(`${i + 1}. ${data.word}`, {
      x: margin + (page.getWidth() / 2) * col,
      y: page.getHeight() - (fontSize + margin) * yCursor,
      size: fontSize,
      font: customFont,
      color: rgb(0, 0, 0),
    });
    page.drawText("_________________", {
      x: margin + (page.getWidth() / 2) * col + 140,
      y: page.getHeight() - (fontSize + margin) * yCursor - 3,
      size: fontSize,
      font: customFont,
      color: rgb(0, 0, 0),
    });
    yCursor++;
  }

  // 답 생성
  for (let i = 0; i < voca_list.length; ++i) {
    const data = voca_list[i];
    if (i % perPage === 0) {
      page = pdfDoc.addPage();
      yCursor = 1;
      col = -1;
    }
    if (i % perColumn === 0) {
      col++;
      yCursor = 1;
    }
    page.drawText(`${i + 1}. ${data.word}`, {
      x: margin + (page.getWidth() / 2) * col,
      y: page.getHeight() - (fontSize + margin) * yCursor,
      size: fontSize,
      font: customFont,
      color: rgb(0, 0, 0),
    });
    page.drawText("_________________", {
      x: margin + (page.getWidth() / 2) * col + 140,
      y: page.getHeight() - (fontSize + margin) * yCursor - 3,
      size: fontSize,
      font: customFont,
      color: rgb(0, 0, 0),
    });
    page.drawText(data.answer, {
      x: margin + (page.getWidth() / 2) * col + 140,
      y: page.getHeight() - (fontSize + margin) * yCursor,
      size: 8,
      font: customFont,
      color: rgb(1, 0, 0),
    });
    yCursor++;
  }

  const pdfBytes = await pdfDoc.save();
  return pdfBytes;
}

export async function downloadVocaQuiz(levels: number[], day: number) {
  const json: JSONType = await (await fetch("/voca.json")).json();
  const voca_list = json.data.filter((chunk) =>
    levels.includes(chunk.level) && chunk.day === day
  ).flatMap((chunk) => chunk.voca_list);
  const bufferPdf = await makePdf(shuffle(voca_list));
  downloadBuffer(bufferPdf, "voca.pdf");
}
