import { jsPDF } from 'jspdf';

import { SITE_URL } from './config';

interface PDFProps {
  personalName: string;
  personalEmail: string;
  generatedKey: string;
}

/**
 * This function generate a pdf with a secret key for a user.
 */
function generateBackupPDF({ personalName, personalEmail, generatedKey }: PDFProps) {
  const imgData =
    'data:image/png;base64,[encoded data, 38816 characters]';

  // new jsPDF('p', 'mm', [297, 210]);
  const today = new Date();
  const dd = String(today.getDate()).padStart(2, '0');
  const mm = String(today.getMonth() + 1).padStart(2, '0'); // January is 0!
  const yyyy = today.getFullYear();

  const todayFormatted = `${mm}/${dd}/${yyyy}`;

  // eslint-disable-next-line new-cap
  const doc = new jsPDF('p', 'pt');
  doc.setFillColor(255, 255, 255);
  doc.rect(0, 0, 600, 900, 'F');
  doc.setTextColor(23, 23, 23);
  doc.addImage(imgData, 'png', 30, 35, 535, 72);
  doc.addFont('helvetica', 'normal', '');
  doc.setFontSize(12);
  doc.text(`Created for ${personalName} on ${todayFormatted}.`, 290, 130, { align: 'center' });
  doc.setFontSize(14);
  doc.text(
    'In case you get locked out of you Infisical account, you`ll need these account details',
    32,
    180
  );
  doc.text('to sign in —', 32, 200);
  doc.setFont('helvetica', 'bold');
  doc.text('including your Secret Key, which we absolutely cannot access or', 110, 200);
  doc.text('recover for you. ', 32, 220);
  doc.setFont('helvetica', 'normal');
  doc.text('Recommendations:', 32, 250);
  doc.text(
    '1. We recommend to get your Emergency Kit off your computer and print a copy.',
    32,
    280
  );
  doc.text(
    '2. Store it somewhere safe (such as with your birth certificate, your will, or on your',
    32,
    310
  );
  doc.text('personal cloud storage).', 32, 330);
  doc.setFillColor(251, 255, 158);
  doc.roundedRect(32, 350, 530, 190, 5, 5, 'F');
  doc.setDrawColor(228, 255, 0);
  doc.setLineWidth(1);
  doc.roundedRect(32, 350, 530, 190, 5, 5, 'S');
  doc.setTextColor(43, 43, 43);
  doc.setFont('helvetica', 'bold');
  doc.setFontSize(15);
  doc.text('Infisical Account Details', 290, 375, { align: 'center' });
  doc.setFontSize(12);
  doc.text('SIGN-IN URL', 50, 420);
  doc.text('EMAIL ADDRESS', 50, 465);
  doc.text('SECRET KEY', 50, 510);
  doc.setFont('helvetica', 'normal');
  doc.setFillColor(254, 255, 235);
  doc.roundedRect(170, 398, 375, 35, 5, 5, 'F');
  doc.roundedRect(170, 443, 375, 35, 5, 5, 'F');
  doc.roundedRect(170, 488, 375, 35, 5, 5, 'F');
  doc.setTextColor(23, 23, 23);
  doc.setFontSize(14);
  doc.text(`${SITE_URL}/login`, 180, 420);
  doc.text(personalEmail, 180, 465);
  doc.text(generatedKey, 180, 510);
  doc.text('Need help? Contact us at support@infisical.com', 32, 575);

  doc.save('Infisical Emergency Kit.pdf');
}

export default generateBackupPDF;
