package org.chenyang.http.impl.cookie;

import org.chenyang.http.cookie.ClientCookie;
import org.chenyang.http.cookie.SetCookie;
import org.chenyang.http.util.Args;
import java.io.Serializable;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

public class BasicClientCookie implements SetCookie, ClientCookie, Cloneable, Serializable {
   private static final long serialVersionUID = -3869795591041535538L;
   private final String name;
   private Map<String, String> attribs;
   private String value;
   private String cookieComment;
   private String cookieDomain;
   private Date cookieExpiryDate;
   private String cookiePath;
   private boolean isSecure;
   private int cookieVersion;
   private Date creationDate;

   public BasicClientCookie(String name, String value) {
      Args.notNull(name, "Name");
      this.name = name;
      this.attribs = new HashMap();
      this.value = value;
   }

   public String getName() {
      return this.name;
   }

   public String getValue() {
      return this.value;
   }

   public void setValue(String value) {
      this.value = value;
   }

   public String getComment() {
      return this.cookieComment;
   }

   public void setComment(String comment) {
      this.cookieComment = comment;
   }

   public String getCommentURL() {
      return null;
   }

   public Date getExpiryDate() {
      return this.cookieExpiryDate;
   }

   public void setExpiryDate(Date expiryDate) {
      this.cookieExpiryDate = expiryDate;
   }

   public boolean isPersistent() {
      return null != this.cookieExpiryDate;
   }

   public String getDomain() {
      return this.cookieDomain;
   }

   public void setDomain(String domain) {
      if (domain != null) {
         this.cookieDomain = domain.toLowerCase(Locale.ROOT);
      } else {
         this.cookieDomain = null;
      }

   }

   public String getPath() {
      return this.cookiePath;
   }

   public void setPath(String path) {
      this.cookiePath = path;
   }

   public boolean isSecure() {
      return this.isSecure;
   }

   public void setSecure(boolean secure) {
      this.isSecure = secure;
   }

   public int[] getPorts() {
      return null;
   }

   public int getVersion() {
      return this.cookieVersion;
   }

   public void setVersion(int version) {
      this.cookieVersion = version;
   }

   public boolean isExpired(Date date) {
      Args.notNull(date, "Date");
      return this.cookieExpiryDate != null && this.cookieExpiryDate.getTime() <= date.getTime();
   }

   public Date getCreationDate() {
      return this.creationDate;
   }

   public void setCreationDate(Date creationDate) {
      this.creationDate = creationDate;
   }

   public void setAttribute(String name, String value) {
      this.attribs.put(name, value);
   }

   public String getAttribute(String name) {
      return (String)this.attribs.get(name);
   }

   public boolean containsAttribute(String name) {
      return this.attribs.containsKey(name);
   }

   public boolean removeAttribute(String name) {
      return this.attribs.remove(name) != null;
   }

   public Object clone() throws CloneNotSupportedException {
      BasicClientCookie clone = (BasicClientCookie)super.clone();
      clone.attribs = new HashMap(this.attribs);
      return clone;
   }

   public String toString() {
      StringBuilder buffer = new StringBuilder();
      buffer.append("[version: ");
      buffer.append(Integer.toString(this.cookieVersion));
      buffer.append("]");
      buffer.append("[name: ");
      buffer.append(this.name);
      buffer.append("]");
      buffer.append("[value: ");
      buffer.append(this.value);
      buffer.append("]");
      buffer.append("[domain: ");
      buffer.append(this.cookieDomain);
      buffer.append("]");
      buffer.append("[path: ");
      buffer.append(this.cookiePath);
      buffer.append("]");
      buffer.append("[expiry: ");
      buffer.append(this.cookieExpiryDate);
      buffer.append("]");
      return buffer.toString();
   }
}
