package org.sagebionetworks.web.client.cookie;

import java.util.Collection;
import java.util.Date;

import org.sagebionetworks.web.client.DisplayUtils;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Cookies;
import com.google.gwt.user.client.Window;

/**
 * A simple wrapper of the GWT cookie implementation.
 * 
 * @author jmhill
 *
 */
public class GWTCookieImpl implements CookieProvider {
	
	@Override
	public String getCookie(String name) {
		return Cookies.getCookie(name);
	}

	@Override
	public Collection<String> getCookieNames() {
		return Cookies.getCookieNames();
	}

	@Override
	public void removeCookie(String key) {
		setCookie(key, "", new Date());
	}

	@Override
	public void setCookie(String name, String value) {
		setCookie(name, value, null);
	}

	@Override
	public void setCookie(String name, String value, Date expires) {
		String domain = CookieUtils.getDomain(Window.Location.getHostName());
		boolean isSecure = domain != null;
		setCookie(name, value, expires, domain, null, isSecure);
	}
	
	@Override
	public void setCookie(String name, String value, Date expires, String domain, String path, boolean secure) {
		double expiresTime = (expires == null) ? 0 : expires.getTime();
		boolean httpOnly = false;
		_setCookie(name, value, expiresTime, domain, path, secure, httpOnly);
	}

	private static native void _setCookie(String name, String value, double expires, String domain, String path, boolean secure, boolean httpOnly) /*-{
	  var c = name + '=' + value;
	  if ( expires )
	    c += ';expires=' + (new Date(expires)).toGMTString();
	  if (domain)
	    c += ';domain=' + domain;
	  if (path)
	    c += ';path=' + path;
	  if (secure)
	    c += ';secure';
	  if (httpOnly)
	  	c += ';HttpOnly';

	  $doc.cookie = c;
	}-*/;
}

@@ -1214,640 +1214,74 @@
 %0D%0A%09%09
-double expiresTime = (expires == null) ? 0 : expires.getTime();%0D%0A%09%09boolean httpOnly = false;%0D%0A%09%09_setCookie(name, value, expiresTime, domain, path, secure, httpOnly);%0D%0A%09%7D%0D%0A%0D%0A%09private static native void _setCookie(String name, String value, double expires, String domain, String path, boolean secure, boolean httpOnly) /*-%7B%0D%0A%09  var c = name + '=' + value;%0D%0A%09  if ( expires )%0D%0A%09    c += ';expires=' + (new Date(expires)).toGMTString();%0D%0A%09  if (domain)%0D%0A%09    c += ';domain=' + domain;%0D%0A%09  if (path)%0D%0A%09    c += ';path=' + path;%0D%0A%09  if (secure)%0D%0A%09    c += ';secure';%0D%0A%09  if (httpOnly)%0D%0A%09  %09c += ';HttpOnly';%0D%0A%0D%0A%09  $doc.cookie = c
+Cookies.setCookie(name, value, expires, domain, path, secure)
 ;%0D%0A%09%7D
--*/;
 %0D%0A%7D%0D

remove native setcookie