package info.bukova.isspst.signapi;

import info.bukova.isspst.Constants;
import info.bukova.isspst.SessionData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.ServletContext;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

/**
 * @author Pepa Rokos
 */

@Controller
public class SigningController {

	@Autowired
	private SessionData sessionData;
	@Autowired
	private JnlpGenerator jnlpGenerator;
	@Autowired
	private ServletContext context;

	@RequestMapping(value="/sign/data", method= RequestMethod.GET)
	public void dataForSign(HttpServletResponse response, HttpServletRequest request) {
		SignData data = (SignData) sessionData.getProperty(Constants.KEY_SIGN_DATA);
		ObjectOutputStream os = null;

		try {
			os = new ObjectOutputStream(response.getOutputStream());
			response.setContentType("application/x-java-serialized-object");
			os.writeObject(data);
			os.flush();
			os.close();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (os != null) {
				try {
					os.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}

	}

	@RequestMapping(value="/sign/data", method= RequestMethod.POST)
	public void signedData(HttpServletRequest request, HttpServletResponse response) {
		ObjectInputStream ois = null;
		ObjectOutputStream os = null;
		String error = null;

		try {
			ois = new ObjectInputStream(request.getInputStream());
			SignData data = (SignData) ois.readObject();
			sessionData.setProperty(Constants.KEY_SIGN_DATA, data);
		} catch (IOException e) {
			e.printStackTrace();
			error = "IO error";
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
			error = "Loading error";
		}

		try {
			SignUploadResponse resp = new SignUploadResponse();

			if (error == null) {
				resp.setOk(true);
			} else {
				resp.setOk(false);
				resp.setErrorMessage(error);
			}

			os = new ObjectOutputStream(response.getOutputStream());
			response.setContentType("application/x-java-serialized-object");
			os.writeObject(resp);
			os.flush();
			os.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@RequestMapping(value="/sign/signer.jnlp", method= RequestMethod.GET)
	public void jnlp(HttpServletResponse response) {
		byte[] data = jnlpGenerator.generate();
		sendByte(response, data, "application/x-java-jnlp-file");
	}

	@RequestMapping(value="/sign/pdfsigner.jar", method= RequestMethod.GET)
	public void pdfsigner(HttpServletResponse response) {
		sendJar(response, "Linux");
	}

	@RequestMapping(value="/sign/pdfsignerWin.jar", method= RequestMethod.GET)
	public void pdfsignerWin(HttpServletResponse response) {
		sendJar(response, "Windows");
	}

	private void sendJar(HttpServletResponse response, String os) {
		File inputJar;

		if (os.startsWith("Windows")) {
			inputJar = new File(context.getRealPath("/WEB-INF/signer/PDFSignerWin.jar"));
		} else {
			inputJar = new File(context.getRealPath("/WEB-INF/signer/PDFSigner.jar"));
		}

		try {
			byte[] data = new byte[(int) inputJar.length()];
			FileInputStream is = new FileInputStream(inputJar);
			is.read(data);
			sendByte(response, data, "application/java-archive");
			is.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private void sendByte(HttpServletResponse response, byte[] data, String contentType) {
		ServletOutputStream os = null;

		try {
			os = response.getOutputStream();
			response.setContentType(contentType);
			response.setContentLength(data.length);
			os.write(data);
			os.flush();
			os.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
