package com.agilex.healthcare.schedulingmanager.pdf;

import com.agilex.healthcare.mobilehealthplatform.domain.filter.datefilter.DateFilter;
import com.agilex.healthcare.utility.DateHelper;
import com.lowagie.text.*;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;

public abstract class AbstractPage {
	protected Document document;
	protected PageEvent event;
	protected PdfWriter writer;
	protected PdfGenerationContext context;
	
	public AbstractPage(Document document, PageEvent event, PdfWriter writer, PdfGenerationContext context) {
		this.document = document;
		this.event = event;
		this.writer = writer;
		this.context = context;
	}

	public abstract void create();
	
	public void addHeading(String title, String anchorName) throws DocumentException {
		PdfPTable heading = new PdfPTable(1);
		heading.setWidthPercentage(100);
		heading.setSpacingBefore(20);
		heading.setSpacingAfter(5);
		
		Anchor anchorTarget = new Anchor(title.toUpperCase(), Stylesheet.getH2());
        anchorTarget.setName(anchorName);
		
		PdfPCell cell = new PdfPCell(anchorTarget);
		cell.setBorderWidth(0);
		cell.setPadding(0);
		heading.addCell(cell);
		
		document.add(heading);
	}

	public void addDateRange(DateFilter dateFilter) throws DocumentException {
		if (dateFilter != null && dateFilter.getStartDate() != null && dateFilter.getEndDate() != null) {
			String startDate = DateHelper.formatDate(dateFilter.getStartDate());
			String endDate = DateHelper.formatDate(dateFilter.getEndDate());
			PdfPTable dateRange = new PdfPTable(1);
			dateRange.setWidthPercentage(100);
			dateRange.setSpacingBefore(5);
			dateRange.setSpacingAfter(5);
			
			PdfPCell cell = 
					new PdfPCell(
						new Phrase(
								context.getMessageSource().getMessage("report.dateRange", 
										new String[] {
											startDate,
											endDate
										},
										context.getLocale()), Stylesheet.getH3()
									)
						);
			cell.setBorderWidth(0);
			cell.setPadding(0);
			dateRange.addCell(cell);
			
			document.add(dateRange);
		}
	}
	
	public void addNoDataFound() throws DocumentException {
		PdfPTable noDataFound = new PdfPTable(1);
		noDataFound.setWidthPercentage(100);
		noDataFound.setSpacingBefore(10);
		noDataFound.setSpacingAfter(10);
		
		PdfPCell cell = new PdfPCell(new Phrase(context.getMessageSource().getMessage("report.noDataFound", null, context.getLocale()), Stylesheet.getItalicLarge()));
		cell.setBorderWidth(0);
		cell.setPadding(0);
		cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
		noDataFound.addCell(cell);
		
		document.add(noDataFound);
	}
	
	public PdfPTable createInformationTable() throws DocumentException {
		PdfPTable resultInformationTable = new PdfPTable(2);
		resultInformationTable.setWidthPercentage(100);
		resultInformationTable.setWidths(new int[] { 20, 85 });
		resultInformationTable.setSpacingBefore(10);
		resultInformationTable.setSpacingAfter(10);
		
		return resultInformationTable;
	}
	
	public void addInformationRow(PdfPTable table, boolean bold, String key, String value) {
		Font font = null;
		if (bold) {
			font = Stylesheet.getBold();
		} else {
			font = Stylesheet.getNormal();
		}
		
		PdfPCell cell = new PdfPCell(new Phrase(key, font));
		cell.setBorderWidth(0.5f);
		cell.setBackgroundColor(Stylesheet.getInfoTableKey());
		table.addCell(cell);
		cell = new PdfPCell(new Phrase(value, font));
		cell.setBorderWidth(0.5f);
		cell.setBackgroundColor(Stylesheet.getInfoTableValue());
		table.addCell(cell);
	}
	
	public PdfPCell createTableHeader(String title, boolean lastCol, int horizontalAlignment) {
		PdfPCell cell = new PdfPCell(new Phrase(title, Stylesheet.getBold()));
		
		cell.setHorizontalAlignment(horizontalAlignment);
		

		cell.setBorderWidth(0);
		
		if (lastCol) {
			cell.setBorderWidthRight(0);
		} else {
			cell.setBorderWidthRight(0);
		}
		cell.setBorderWidthBottom(1);
		cell.setBackgroundColor(Stylesheet.getTableHeader());
		cell.setPadding(5);
		
		return cell;
	}
	
	public PdfPCell createCell(Phrase phrase, int rowNum, boolean lastCol, boolean lastRow, int horizontalAlignment) {
		PdfPCell cell = new PdfPCell(phrase);
		cell.setVerticalAlignment(PdfPCell.ALIGN_TOP);
		cell.setHorizontalAlignment(horizontalAlignment);
		
		if (rowNum % 2 == 0) {
			cell.setBackgroundColor(Stylesheet.getTableRow1());
		} else {
			cell.setBackgroundColor(Stylesheet.getTableRow2());
		}
		
		cell.setBorderWidthTop(0);
		if (lastRow) {
			cell.setBorderWidthBottom(0);
		} else {
			cell.setBorderWidthBottom(0);
		}

		cell.setBorderWidthLeft(0);
		if (lastCol) {
			cell.setBorderWidthRight(0);
		} else {
			cell.setBorderWidthRight(0);
		}

		cell.setPadding(5);
		
		return cell;
	}
}
