import {
  getAssets,
  getContent,
  replaceAssesmentData,
  replaceCompetenciesStrWeak,
  replaceFooter,
  replacePersonality,
  replacePracticalTips,
  replaceWaysToDevelop,
  replaceYourIndividualDevPlan,
} from "./utils";
import { Promise } from "bluebird";
import { ReportType, RequestData } from "./types";

const pdf = Promise.promisifyAll(require("html-pdf"));

const createPDF = (html: string) => {
  var options = {
    format: "A4",
    base: "file:///" + __dirname + "/",
    localUrlAccess: true, // set this to true to enable local file access
    phantomPath: require("phantomjs-prebuilt").path,
  };
  return new Promise((resolve, reject) => {
    pdf.create(html, options).toBuffer((err: any, buffer: Buffer) => {
      if (err !== null) {
        reject(err);
      } else {
        resolve(buffer);
      }
    });
  });
};

const createReport = async (_data: RequestData, type: ReportType) => {
  let content: string;
  //Get the template corresponding to report
  try {
    content = getContent(type);
    if (process.platform === "linux" || process.platform === "darwin") {
      content = content.replace(
        '<link href="http://fonts.cdnfonts.com/css/circular-std" rel="stylesheet" />',
        ""
      );
    }

    content = replaceCompetenciesStrWeak(_data.params.competencies, content);
    content = replacePersonality(_data.params.personality, content);

    let personalitySectionOffset: number = 0;
    const averageHeight = 225; //Estimated height for personality card

    if (_data.params.personality.length < 5) {
      personalitySectionOffset = averageHeight * 5;
    }

    if (
      _data.params.personality.length >= 5 &&
      _data.params.personality.length < 10
    ) {
      personalitySectionOffset = averageHeight * 10;
    }

    if (
      _data.params.personality.length >= 10 &&
      _data.params.personality.length < 15
    ) {
      personalitySectionOffset = averageHeight * 15;
    }

    if (_data.params.personality.length >= 15) {
      personalitySectionOffset = averageHeight * 20;
    }

    let nextPosition = 2000 + personalitySectionOffset;
    if (_data.params.waysToDevelop) {
      content = replaceWaysToDevelop(
        _data.params.waysToDevelop,
        nextPosition,
        content
      );
    }
    const wtdQty = _data.params.waysToDevelop.length;
    nextPosition =
      wtdQty / 2 === 1
        ? 850 * (wtdQty / 2) + nextPosition + 45
        : 1200 * (wtdQty / 2) + nextPosition + 45;
    content = replacePracticalTips(content, nextPosition);
    nextPosition = nextPosition + 1000;
    content = replaceYourIndividualDevPlan(content, nextPosition);
    nextPosition = nextPosition + 1235;
    content = replaceFooter(content, nextPosition);
    const assets = getAssets(type);

    content = replaceAssesmentData(
      [..._data.params.header, ...assets],
      content
    );

    const buffer = await createPDF(content);
    return buffer;
  } catch (e) {
    console.error(e);
    throw Error();
  }
};

export { createReport };
