package com.reallinkbilling.print;

import java.io.IOException;
import java.io.OutputStream;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.dispatcher.SessionMap;
import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionSupport;

public class BillPrintOnlyType2Attachment extends ActionSupport implements SessionAware {

	private static final long serialVersionUID = -7226952973395769194L;

	private SessionMap<String, Object> sessionMap;

	private String billNumber;
	private String billType;
	private String typeOfBill;
	private String key;
	private String language;
	private String billDateTime;

	private String attachmentFilePath;
	private String attachmentFileName;
	private byte[] attachmentBytes;

	public void generateAttachmentPdf() {
		try {
			String currentLanguage = this.language != null ? this.language : "ENGLISH";
			String currentBillType = this.billType != null ? this.billType : "A5";

			this.attachmentBytes = BillPdfAttachmentGenerator.fetchBillPdfFromCurrentSession("billPrintOnlyType2",
					this.key, this.billNumber, this.billDateTime, this.typeOfBill, currentBillType, currentLanguage);
			this.attachmentFilePath = null;
			this.attachmentFileName = "Kharedi-Bill.pdf";
			writePdfToResponse(this.attachmentBytes, this.attachmentFileName);
		} catch (Exception e) {
			addActionError("Unable to generate type2 bill PDF attachment: " + e.getMessage());
		}
	}

	public void printBillWithOutSession() {
		try {
			this.attachmentBytes = BillPdfAttachmentGenerator.fetchBillPdfWithoutSessionKey(this.key, this.billType);
			writePdfToResponse(this.attachmentBytes, "Kharedi-Bill.pdf");
		} catch (Exception e) {
			addActionError("Unable to generate type2 bill PDF without session: " + e.getMessage());
			writeErrorToResponse("Unable to generate bill PDF: " + e.getMessage());
		}
	}

	public SessionMap<String, Object> getSessionMap() {
		return sessionMap;
	}

	public void setSessionMap(SessionMap<String, Object> sessionMap) {
		this.sessionMap = sessionMap;
	}

	public String getBillNumber() {
		return billNumber;
	}

	public void setBillNumber(String billNumber) {
		this.billNumber = billNumber;
	}

	public String getBillType() {
		return billType;
	}

	public void setBillType(String billType) {
		this.billType = billType;
	}

	public String getTypeOfBill() {
		return typeOfBill;
	}

	public void setTypeOfBill(String typeOfBill) {
		this.typeOfBill = typeOfBill;
	}

	public String getKey() {
		return key;
	}

	public void setKey(String key) {
		this.key = key;
	}

	public String getLanguage() {
		return language;
	}

	public void setLanguage(String language) {
		this.language = language;
	}

	public String getBillDateTime() {
		return billDateTime;
	}

	public void setBillDateTime(String billDateTime) {
		this.billDateTime = billDateTime;
	}

	public String getAttachmentFilePath() {
		return attachmentFilePath;
	}

	public void setAttachmentFilePath(String attachmentFilePath) {
		this.attachmentFilePath = attachmentFilePath;
	}

	public String getAttachmentFileName() {
		return attachmentFileName;
	}

	public void setAttachmentFileName(String attachmentFileName) {
		this.attachmentFileName = attachmentFileName;
	}

	public byte[] getAttachmentBytes() {
		return attachmentBytes;
	}

	public void setAttachmentBytes(byte[] attachmentBytes) {
		this.attachmentBytes = attachmentBytes;
	}

	@Override
	public void setSession(Map<String, Object> map) {
		sessionMap = (SessionMap<String, Object>) map;
	}

	private void writePdfToResponse(byte[] pdfBytes, String fileName) throws IOException {
		if (pdfBytes == null || pdfBytes.length == 0) {
			throw new IOException("Generated PDF is empty.");
		}
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("application/pdf");
		response.setHeader("Content-Disposition", "inline; filename=\"" + fileName + "\"");
		response.setContentLength(pdfBytes.length);
		try (OutputStream outputStream = response.getOutputStream()) {
			outputStream.write(pdfBytes);
			outputStream.flush();
		}
	}

	private void writeErrorToResponse(String message) {
		try {
			HttpServletResponse response = ServletActionContext.getResponse();
			response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
			response.setContentType("text/plain;charset=UTF-8");
			try (OutputStream outputStream = response.getOutputStream()) {
				outputStream.write(message.getBytes("UTF-8"));
				outputStream.flush();
			}
		} catch (Exception ignore) {
		}
	}
}
