import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.multipart.FilePart;
import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
import org.apache.commons.httpclient.methods.multipart.Part;
import org.apache.commons.httpclient.methods.multipart.StringPart;
import org.apache.commons.io.IOUtils;
 
class transformer_two{
	public static void main(String [] args) {
	    String convertUrl = "http://ice-service.usq.edu.au/api/convert/";
		if(args.length == 1) {
			String fileName = args[0];
			System.out.println(fileName);
			File f = new File(fileName);
			if(f.exists()) {
				String extResult = getExtension(f);
				if (!extResult.startsWith("Error")) {
					convertUrl = convertUrl + extResult;
					getRendition(f, convertUrl);
				}
			} else {
				System.out.println("Error: file doesn't exist!");
			}
		} else {
			System.out.println("Usage: java transformer fileName");
		}
	}
  
	public static void getRendition (File sourceFile, String convertUrl) {
		try{
				HttpClient client = new HttpClient();	
				PostMethod filePost = new PostMethod(convertUrl);
				Part[] parts = {
						new StringPart("zip", "1"),
						new StringPart("toc", "1"),
						new StringPart("pdfLink", "1"),
						new StringPart("pathext", ""),
						new StringPart("template", getTemplate()),
						new FilePart("file", sourceFile)
				};
				
				filePost.setRequestEntity(new MultipartRequestEntity(parts, filePost.getParams()));
				client.executeMethod(filePost);
				
			    //Get Response
				InputStream is = filePost.getResponseBodyAsStream();
				String[] filePart = sourceFile.getName().split("\\.");
			    System.out.println(sourceFile.getName());
			    String outputFilename = "/tmp/"+ filePart[0] + ".zip";
			    FileOutputStream fos = new FileOutputStream(outputFilename);
			    IOUtils.copy(is, fos);
			    
			    is.close();
			    fos.close();
				System.out.println("Rendition zip file is located at: " + outputFilename);
		} catch (Exception e) {
			System.out.println("An error occurred: " + e.getClass());
			e.printStackTrace();
		}
	}
 
	private static String getExtension(File fileObject) {
		/** getExtension method
		 * @param File fileObject
		 * @return String
		 */
		String[] parts = fileObject.getName().split("\\.");
		String ext = "";
		if (parts.length == 2) {
			ext = parts[1];
			return ext;
		} 
		return "Error: Unable to detect file extension properly!";
	}

	private static String getTemplate (){
		String template = "<html>" +
  "<head>" +
    "<meta http-equiv='Content-Type' content='text/html; charset=UTF-8'/>" +
    "<title>Default Template</title>" +
    "<style type='text/css'>" +
      ".rendition-links { text-align: right; }" +
      ".body table td { vertical-align: top; }" +
    "</style>" +
    "<style class='sub style-css' type='text/css'></style>" +
  "</head>" +
  "<body>" +
    "<div class='rendition-links'>" +
      "<span class='ins source-link'></span>" +
      "<span class='ins slide-link'></span>" +
      "<span class='ins pdf-rendition-link'></span>" +
    "</div>" +
    "<h1 class='ins title'></h1>" +
    "<div class='ins page-toc'></div>" +
    "<div class='ins body'></div>" +
  "</body>" +
"</html>";
		return template;
	}
}
