/**
 * Copyright (C) 2010 Google 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.google.inject.servlet;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.logging.Logger;

import javax.servlet.Filter;
import javax.servlet.ServletContext;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import junit.framework.AssertionFailedError;

import com.google.inject.Binding;
import com.google.inject.Injector;
import com.google.inject.Stage;
import com.google.inject.internal.util.ImmutableSet;
import com.google.inject.internal.util.Lists;
import com.google.inject.internal.util.Objects;
import com.google.inject.internal.util.ToStringBuilder;
import com.google.inject.spi.DefaultBindingTargetVisitor;

/**
 * A visitor for testing the servlet SPI extension.
 * 
 * @author sameb@google.com (Sam Berlin)
 */
class ServletSpiVisitor
    extends DefaultBindingTargetVisitor<Object, Integer>
    implements ServletModuleTargetVisitor<Object, Integer> {
  
  int otherCount = 0;
  int currentCount = 0;
  List<Params> actual = Lists.newArrayList();
  
  /* The set of classes that are allowed to be "other" bindings. */
  Set<Class> allowedClasses;
  
  ServletSpiVisitor(boolean forInjector) {
    ImmutableSet.Builder<Class> builder = ImmutableSet.builder();
    // always ignore these things...
    builder.add(ServletRequest.class,
        ServletResponse.class, ManagedFilterPipeline.class, ManagedServletPipeline.class,
        FilterPipeline.class, ServletContext.class, HttpServletRequest.class, Filter.class,
        HttpServletResponse.class, HttpSession.class, Map.class, HttpServlet.class);
    if(forInjector) {
      // only ignore these if this is for the live injector, any other time it'd be an error!
      builder.add(Injector.class, Stage.class, Logger.class);
    }
    this.allowedClasses = builder.build();
  }
  
  public Integer visit(InstanceFilterBinding binding) {
    actual.add(new Params(binding.getPattern(), binding.getFilterInstance(), binding.getInitParams(), binding.getUriPatternType()));
    return currentCount++;
  }
  
  public Integer visit(InstanceServletBinding binding) {
    actual.add(new Params(binding.getPattern(), binding.getServletInstance(), binding.getInitParams(), binding.getUriPatternType()));
    return currentCount++;
  }
  
  public Integer visit(LinkedFilterBinding binding) {
    actual.add(new Params(binding.getPattern(), binding.getLinkedKey(), binding.getInitParams(), binding.getUriPatternType()));
    return currentCount++;
  }
  
  public Integer visit(LinkedServletBinding binding) {
    actual.add(new Params(binding.getPattern(), binding.getLinkedKey(), binding.getInitParams(), binding.getUriPatternType()));
    return currentCount++;
  }

  @Override
  protected Integer visitOther(Binding<? extends Object> binding) {
    if(!allowedClasses.contains(binding.getKey().getTypeLiteral().getRawType())) {
      throw new AssertionFailedError("invalid other binding: " + binding);
    }
    otherCount++;
    return currentCount++;
  }
  
  static class Params {
    private final String pattern;
    private final Object keyOrInstance;
    private final Map<String, String> params;
    private UriPatternType patternType;
    
    Params(String pattern, Object keyOrInstance, Map params, UriPatternType patternType) {
      this.pattern = pattern;
      this.keyOrInstance = keyOrInstance;
      this.params = params;
      this.patternType = patternType;
    }
    
    @Override
    public boolean equals(Object obj) {
      if(obj instanceof Params) {
        Params o = (Params)obj;
        return Objects.equal(pattern, o.pattern)
            && Objects.equal(keyOrInstance, o.keyOrInstance)
            && Objects.equal(params, o.params)
            && Objects.equal(patternType, o.patternType);
      } else {
        return false;
      }
    }
    
    @Override
    public int hashCode() {
      return Objects.hashCode(pattern, keyOrInstance, params, patternType);
    }
    
    @Override
    public String toString() {
      return new ToStringBuilder(Params.class)
        .add("pattern", pattern)
        .add("keyOrInstance", keyOrInstance)
        .add("initParams", params)
        .add("patternType", patternType)
        .toString();
    }
  }
}
@@ -2738,37 +2738,24 @@
 rams(binding
-.getPattern()
 , binding.ge
@@ -2775,62 +2775,8 @@
 ce()
-, binding.getInitParams(), binding.getUriPatternType()
 ));%0D
@@ -2900,37 +2900,24 @@
 rams(binding
-.getPattern()
 , binding.ge
@@ -2938,62 +2938,8 @@
 ce()
-, binding.getInitParams(), binding.getUriPatternType()
 ));%0D
@@ -3060,37 +3060,24 @@
 rams(binding
-.getPattern()
 , binding.ge
@@ -3092,62 +3092,8 @@
 ey()
-, binding.getInitParams(), binding.getUriPatternType()
 ));%0D
@@ -3223,21 +3223,8 @@
 ding
-.getPattern()
 , bi
@@ -3247,62 +3247,8 @@
 ey()
-, binding.getInitParams(), binding.getUriPatternType()
 ));%0D
@@ -3748,16 +3748,22 @@
 private 
+final 
 UriPatte
@@ -3785,24 +3785,291 @@
 ype;%0D%0A    %0D%0A
+    Params(ServletModuleBinding binding, Object keyOrInstance) %7B%0D%0A      this.pattern = binding.getPattern();%0D%0A      this.keyOrInstance = keyOrInstance;%0D%0A      this.params = binding.getInitParams();%0D%0A      this.patternType = binding.getUriPatternType();%0D%0A    %7D%0D%0A    %0D%0A
     Params(S
@@ -4693,36 +4693,32 @@
       %7D%0D%0A    %7D%0D%0A
-    
 %0D%0A    @Override%0D

simplify servlet SPI test.