package com.blogspot.jvalentino.usnrauto.reportmerger.service.individual

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.edit.PDPageContentStream;

import com.blogspot.jvalentino.usnrauto.main.AppState;
import com.blogspot.jvalentino.usnrauto.reportmerger.data.summary.SummaryReport;
import com.blogspot.jvalentino.usnrauto.util.PdfUtil;

import static com.blogspot.jvalentino.usnrauto.util.FormatUtil.*;
import static com.blogspot.jvalentino.usnrauto.util.PdfUtil.*;

class CoverPage {
	
	private static AppState appState = AppState.getInstance()

	/**
	 * This is used to generate an informational cover page
	 * @param document
	 * @param report
	 * @return
	 */
	static PDPage draw(PDDocument document, SummaryReport report) {
		PDPage page = new PDPage()
		
		PDPageContentStream cos = new PDPageContentStream(document, page);

		int y = getTopOfPagePosition(page)
		
		PdfUtil.drawCenteredText("INDIVIDUAL MEMBER READINESS REPORT",
			WEIGHT_BOLD, FONT_LARGE, page, cos, y)
		
		y -= GAP
		PdfUtil.drawText("This report was automatically generated by the USNR Admin Automation Project.",
				WEIGHT_NORMAL, FONT_NORMAL, page, cos, MARGIN_LEFT, y)
		
		y -= GAP
		PdfUtil.drawText("- Source Code: https://bitbucket.org/usnr/usnr-admin-automation (access restricted)",
			WEIGHT_NORMAL, FONT_NORMAL, page, cos, MARGIN_LEFT + 20, y)
		
		y -= GAP
		PdfUtil.drawText("- Groovy-Java based JNLP Application to allow remote installation on any machine with Java.",
			WEIGHT_NORMAL, FONT_NORMAL, page, cos, MARGIN_LEFT + 20, y)
		
		y -= GAP
		PdfUtil.drawText("- This program and its source contain no member information, and the source is tested against fake people.",
			WEIGHT_NORMAL, FONT_NORMAL, page, cos, MARGIN_LEFT + 20, y)
		
		y -= GAP
		PdfUtil.drawText("- This program is executed on the local machine, where member data must be supplied.",
			WEIGHT_NORMAL, FONT_NORMAL, page, cos, MARGIN_LEFT + 20, y)
		
		y -= GAP
		PdfUtil.drawText("- This automates the combining and analysis of several Navy system spreadsheets locally.",
			WEIGHT_NORMAL, FONT_NORMAL, page, cos, MARGIN_LEFT + 20, y)
				
		y -= GAP
		PdfUtil.drawText("The purpose of this project is to automatically generate member level action items and unit summary reports",
				WEIGHT_NORMAL, FONT_NORMAL, page, cos, MARGIN_LEFT, y)
		
		y -= GAP
		PdfUtil.drawText("by combining information from the following systems and reports:",
				WEIGHT_NORMAL, FONT_NORMAL, page, cos, MARGIN_LEFT, y)
		
		String[][] content = new String[3][2]
		content[0] = ["NRRM: Smart RUAD Report", "NRRM: Individual Medical Readiness Report"]
		content[1] = ["FLTMPS: GMT Report", "FLTMPS: IA Report"]
		content[2] = ["FLTMPS: e-Learning Report", "Spreadsheet containing manual AT, security, and account inputs"]
		
		y -= GAP
		y = PdfUtil.drawTable(page, cos, y.floatValue(), MARGIN_LEFT.floatValue(), content)
		
		// Download section
		y -= GAP
		PdfUtil.drawText("DOWNLOAD",
			WEIGHT_BOLD, FONT_LARGE, page, cos, MARGIN_LEFT, y)
		
		y -= GAP
		PdfUtil.drawText("There are three ways to download, install, and run this application:",
				WEIGHT_NORMAL, FONT_NORMAL, page, cos, MARGIN_LEFT, y)
		
		y -= GAP
		PdfUtil.drawText("- Windows Installer: " + appState.getWindowsInstallerUrl() ,
			WEIGHT_NORMAL, FONT_NORMAL, page, cos, MARGIN_LEFT + 20, y)
		
		y -= GAP
		PdfUtil.drawText("- OS X App: " + appState.getMacInstallerUrl(),
			WEIGHT_NORMAL, FONT_NORMAL, page, cos, MARGIN_LEFT + 20, y)
		
		y -= GAP
		PdfUtil.drawText("- Java Web Start: " + appState.getJnlpUrl(),
			WEIGHT_NORMAL, FONT_NORMAL, page, cos, MARGIN_LEFT + 20, y)
		
		y -= GAP
		
		// Generated Warning section
		y -= GAP
		PdfUtil.drawText("GENERATED WARNINGS",
			WEIGHT_BOLD, FONT_LARGE, page, cos, MARGIN_LEFT, y)
		
		y -= GAP
		PdfUtil.drawText("Since these reports do not contain a method for guaranteed unique member identification,",
			WEIGHT_NORMAL, FONT_NORMAL, page, cos, MARGIN_LEFT, y)
		
		y -= GAP
		PdfUtil.drawText("summary data can only be retrieved for members that can be uniquely identified by name and rank.",
			WEIGHT_NORMAL, FONT_NORMAL, page, cos, MARGIN_LEFT, y)
		
		y -= GAP
		PdfUtil.drawText("It is also possible for members to be present in the RUAD but not show up in other systems for a unit.",
			WEIGHT_NORMAL, FONT_NORMAL, page, cos, MARGIN_LEFT, y)
		
		y -= GAP
		PdfUtil.drawText("This can result in members that will have incomplete information in the report.",
			WEIGHT_NORMAL, FONT_NORMAL, page, cos, MARGIN_LEFT, y)
		
		y -= GAP_MICRO
		
		String[][] warningContent = PdfUtil.generateTrainingTableData(report.getWarnings(), 2)
		y = PdfUtil.drawTable(page, cos, y.floatValue(), MARGIN_LEFT.floatValue(), warningContent, null, FONT_SMALL - 1)
		
		PdfUtil.drawCenteredText(report.pdfFooter,
				WEIGHT_NORMAL, FONT_SMALL, page, cos, FOOTER_POSITION)
		
		cos.close()
		
		return page
	}
}
