package com.akjava.gwt.jszip.client;

import com.akjava.gwt.html5.client.file.Blob;
import com.google.gwt.core.client.JavaScriptObject;

public class JSZip extends JavaScriptObject{
protected JSZip(){}

	public static native final boolean exists()/*-{
	return $wnd.JSZip;
	}-*/;

	public final static native JSZip newJSZip()/*-{
	return new $wnd.JSZip();
	}-*/;
	
	
	public final native JSFile getFile(String fileName)/*-{
	return this.file(fileName);
	}-*/;
	
	public final native JSZip file(String fileName,String text)/*-{
	return this.file(fileName,text);
	}-*/;
	
	
	/**
	 * designed for canvas-url
	 * @param fileName
	 * @param url
	 * @return
	 */
	public final  JSZip base64UrlFile(String fileName,String url){
		int index=url.indexOf("base64,");
		String imgData;
		if(index==-1){
			imgData=url;
		}else{
			imgData=url.substring(index+"base64,".length());
		}
		return file(fileName, imgData,JSFileOptions.newJSFileOptions().base64(true));
	}
	
	public final native JSZip file(String fileName,String text,JSFileOptions option)/*-{
	return this.file(fileName,text,option);
	}-*/;
	
	/**
	 * not tested
	 * @param fileName
	 * @param data
	 * @param option
	 * @return
	 */
	public final native JSZip file(String fileName,JavaScriptObject data,JSFileOptions option)/*-{
	return this.file(fileName,data,option);
	}-*/;
	
	public final native JSZip folder(String folderName)/*-{
	return this.folder(folderName);
	}-*/;
	
	public final native JSZip remove(String name)/*-{
	return this.remove(name);
	}-*/;
	
	//TODO cast type
	public final native Object generate()/*-{
	return this.generate();
	}-*/;
	
	public final native Object generate(GenerateParameter parameter)/*-{
	return this.generate(parameter);
	}-*/;
	
	/**
	 * this is very useful but this method need html5 libs
	 
	<inherits name="com.akjava.gwt.lib.Commons"/>
	<inherits name="com.akjava.gwt.html5.Html5widget" />
	 
	 * @param compression
	 * @return
	 */
	public final native Blob generateBlob(String compression)/*-{
	var parameter={type:"blob"};
	if(compression){
		parameter.compression=compression;
	}	
	return this.generate(parameter);
	}-*/;
}

@@ -33,16 +33,42 @@
 ent;%0D%0A%0D%0A
+import java.util.Date;%0D%0A%0D%0A
 import c
@@ -158,16 +158,67 @@
 Object;%0D
+%0Aimport com.google.gwt.i18n.client.DateTimeFormat;%0D
 %0A%0D%0Apubli
@@ -279,16 +279,121 @@
 ip()%7B%7D%0D%0A
+%09private static final DateTimeFormat dateTimeFormat = DateTimeFormat.getFormat(%22MMMM dd, yyyy HH:mm:ss%22);
 %0D%0A%09publi
@@ -662,117 +662,592 @@
 %09%0D%0A%09
-public final native JSZip file(String fileName,String text)/*-%7B%0D%0A%09return this.file(fileName,text);%0D%0A%09%7D-*/;%0D%0A%09
+/**%0D%0A%09 * %0D%0A%09 * @param fileName%0D%0A%09 * if fileName contain /,directory is created automatically  %0D%0A%09 * @param text%0D%0A%09 * @return%0D%0A%09 */%0D%0A%09public final native JSZip file(String fileName,String text)/*-%7B%0D%0A%09return this.file(fileName,text);%0D%0A%09%7D-*/;%0D%0A%09%0D%0A%09public final native JSZip file(String fileName,String text,String dateLabel)/*-%7B%0D%0A%09return this.file(fileName,text,%7Bdate : new $wnd.Date(dateLabel)%7D);%0D%0A%09%7D-*/;%0D%0A%09%0D%0A%09public final  JSZip file(String fileName,String text,long datetime)%7B%0D%0A%09%09String dateLabel=dateTimeFormat.format(new Date(datetime));%0D%0A%09%09return file(fileName,text,dateLabel);%0D%0A%09%7D
 %0D%0A%09%0D

support cdate