/*
 * Copyright 2010 Proofpoint, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.proofpoint.http.client;

import com.google.common.annotations.Beta;
import com.google.inject.Binder;
import com.google.inject.binder.LinkedBindingBuilder;
import com.google.inject.multibindings.Multibinder;

import java.lang.annotation.Annotation;
import java.util.Collection;

import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.inject.multibindings.Multibinder.newSetBinder;

@Beta
public class HttpClientBinder
{
    private final Binder binder;
    private final Binder rootBinder;

    private HttpClientBinder(Binder binder, Binder rootBinder)
    {
        this.binder = checkNotNull(binder, "binder is null");
        this.rootBinder = checkNotNull(rootBinder, "rootBinder is null");
    }

    public static HttpClientBinder httpClientBinder(Binder binder)
    {
        return new HttpClientBinder(binder, binder);
    }

    public static HttpClientBinder httpClientPrivateBinder(Binder privateBinder, Binder rootBinder)
    {
        return new HttpClientBinder(privateBinder, rootBinder);
    }

    public HttpClientBindingBuilder bindHttpClient(String name, Class<? extends Annotation> annotation)
    {
        checkNotNull(name, "name is null");
        checkNotNull(annotation, "annotation is null");
        return createBindingBuilder(new AsyncHttpClientModule(name, annotation, rootBinder));
    }

    private HttpClientBindingBuilder createBindingBuilder(AsyncHttpClientModule module)
    {
        binder.install(module);
        return new HttpClientBindingBuilder(module,
                newSetBinder(binder, HttpRequestFilter.class, module.getFilterQualifier()));
    }

    /**
     * @deprecated Use {@link #bindHttpClient(String, Class)}
     */
    @Deprecated
    public HttpClientAsyncBindingBuilder bindAsyncHttpClient(String name, Class<? extends Annotation> annotation)
    {
        checkNotNull(name, "name is null");
        checkNotNull(annotation, "annotation is null");
        return createAsyncBindingBuilder(new AsyncHttpClientModule(name, annotation, rootBinder));
    }

    private HttpClientAsyncBindingBuilder createAsyncBindingBuilder(AsyncHttpClientModule module)
    {
        binder.install(module);
        return new HttpClientAsyncBindingBuilder(module,
                newSetBinder(binder, HttpRequestFilter.class, module.getFilterQualifier()));
    }

    public static class HttpClientBindingBuilder
        extends HttpClientAsyncBindingBuilder
    {
        public HttpClientBindingBuilder(AsyncHttpClientModule module, Multibinder<HttpRequestFilter> multibinder)
        {
            super(module, multibinder);
        }
    }

    public static class HttpClientAsyncBindingBuilder
            extends AbstractHttpClientBindingBuilder<HttpClientAsyncBindingBuilder>
    {
        private final AsyncHttpClientModule module;

        private HttpClientAsyncBindingBuilder(AsyncHttpClientModule module, Multibinder<HttpRequestFilter> multibinder)
        {
            super(module, multibinder);
            this.module = module;
        }

        public HttpClientAsyncBindingBuilder withPrivateIoThreadPool()
        {
            module.withPrivateIoThreadPool();
            return this;
        }
    }

    public abstract static class AbstractHttpClientBindingBuilder<T extends AbstractHttpClientBindingBuilder<T>>
    {
        protected final AbstractHttpClientModule module;
        private final Multibinder<HttpRequestFilter> multibinder;

        private AbstractHttpClientBindingBuilder(AbstractHttpClientModule module, Multibinder<HttpRequestFilter> multibinder)
        {
            this.module = module;
            this.multibinder = multibinder;
        }

        @SuppressWarnings("unchecked")
        public T withAlias(Class<? extends Annotation> alias)
        {
            module.addAlias(alias);
            return (T) this;
        }

        @SuppressWarnings("unchecked")
        public T withAliases(Collection<Class<? extends Annotation>> aliases)
        {
            for (Class<? extends Annotation> annotation : aliases) {
                module.addAlias(annotation);
            }
            return (T) this;
        }

        public LinkedBindingBuilder<HttpRequestFilter> addFilterBinding()
        {
            return multibinder.addBinding();
        }

        @SuppressWarnings("unchecked")
        public T withFilter(Class<? extends HttpRequestFilter> filterClass)
        {
            multibinder.addBinding().to(filterClass);
            return (T) this;
        }

        public T withTracing()
        {
            return withFilter(TraceTokenRequestFilter.class);
        }
    }
}

@@ -3294,92 +3294,8 @@
 der%0A
-            extends AbstractHttpClientBindingBuilder%3CHttpClientAsyncBindingBuilder%3E%0A
     
@@ -3348,17 +3348,16 @@
 module;%0A
-%0A
         
@@ -3368,653 +3368,90 @@
 ate 
-HttpClientAsyncBindingBuilder(AsyncHttpClientModule module, Multibinder%3CHttpRequestFilter%3E multibinder)%0A        %7B%0A            super(module, multibinder);%0A            this.module = module;%0A        %7D%0A%0A        public HttpClientAsyncBindingBuilder withPrivateIoThreadPool()%0A        %7B%0A            module.withPrivateIoThreadPool();%0A            return this;%0A        %7D%0A    %7D%0A%0A    public abstract static class AbstractHttpClientBindingBuilder%3CT extends AbstractHttpClientBindingBuilder%3CT%3E%3E%0A    %7B%0A        protected final AbstractHttpClientModule module;%0A        private final Multibinder%3CHttpRequestFilter%3E multibinder;%0A%0A        private AbstractHttpClient
+final Multibinder%3CHttpRequestFilter%3E multibinder;%0A%0A        private HttpClientAsync
 Bind
@@ -3466,15 +3466,12 @@
 er(A
-bstract
+sync
 Http
@@ -3680,33 +3680,61 @@
 %0A        public 
-T
+HttpClientAsyncBindingBuilder
  withAlias(Class
@@ -3819,36 +3819,32 @@
           return
- (T)
  this;%0A        %7D
@@ -3895,25 +3895,53 @@
      public 
-T
+HttpClientAsyncBindingBuilder
  withAliases
@@ -4138,36 +4138,32 @@
           return
- (T)
  this;%0A        %7D
@@ -4354,25 +4354,53 @@
      public 
-T
+HttpClientAsyncBindingBuilder
  withFilter(
@@ -4533,12 +4533,8 @@
 turn
- (T)
  thi
@@ -4562,17 +4562,45 @@
  public 
-T
+HttpClientAsyncBindingBuilder
  withTra
@@ -4683,21 +4683,184 @@
 );%0A        %7D
+%0A%0A        public HttpClientAsyncBindingBuilder withPrivateIoThreadPool()%0A        %7B%0A            module.withPrivateIoThreadPool();%0A            return this;%0A        %7D
 %0A    %7D%0A%7D%0A

Simplify HttpClientBinder