package org.jboss.resteasy.client.jaxrs.internal.proxy.processors.invocation;

import org.jboss.resteasy.client.jaxrs.internal.ClientConfiguration;
import org.jboss.resteasy.client.jaxrs.internal.ClientInvocation;
import org.jboss.resteasy.client.jaxrs.internal.ClientInvocationBuilder;
import org.jboss.resteasy.client.jaxrs.internal.proxy.processors.InvocationProcessor;

import javax.ws.rs.core.Cookie;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;

/**
 * @author <a href="mailto:bill@burkecentral.com">Bill Burke</a>
 * @version $Revision: 1 $
 */
public class CookieParamProcessor implements InvocationProcessor
{
   private String cookieName;
   protected Annotation[] annotations;
   protected Type type;

   public CookieParamProcessor(final String cookieName)
   {
      this.cookieName = cookieName;
   }

   public CookieParamProcessor(final String cookieName, final Type type, final Annotation[] annotations)
   {
      this.cookieName = cookieName;
      this.annotations = annotations;
      this.type = type;
   }

   public String getCookieName()
   {
      return cookieName;
   }

   @Override
   public void process(ClientInvocationBuilder request, Object object)
   {
      if (object == null) return;  // don't set a null value
      if (object instanceof Cookie)
      {
         Cookie cookie = (Cookie) object;
         request.cookie(cookie);
      }
      else
      {
         request.cookie(new Cookie(cookieName, request.getInvocation().getClientConfiguration().toString(object)));
      }
   }

   @Override
   public void process(ClientInvocation invocation, Object object)
   {
      if (object == null) return;  // don't set a null value
      if (object instanceof Cookie)
      {
         Cookie cookie = (Cookie) object;
         invocation.getHeaders().cookie(cookie);
      }
      else
      {
         ClientConfiguration cc = invocation.getClientConfiguration();
         String s = (annotations != null && type != null) ? cc.toString(object, type, annotations) : cc.toString(object);
         invocation.getHeaders().cookie(new Cookie(cookieName, s));
      }
   }
}
