/*
 * #%L
 * wcm.io
 * %%
 * Copyright (C) 2018 wcm.io
 * %%
 * 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.
 * #L%
 */
package io.wcm.qa.galenium.maven.freemarker.pojo;

import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.util.ArrayList;

import org.apache.commons.lang3.StringUtils;

import io.wcm.qa.galenium.maven.freemarker.util.ReflectionUtil;

public class InteractionMethodPojo {

  private Method method;

  public InteractionMethodPojo(Method method) {
    setMethod(method);
  }

  public String getBody() {
    StringBuilder body = new StringBuilder();
    if (!StringUtils.equals("void", method.getReturnType().getCanonicalName())) {
      body.append("return ");
    }
    body.append("Element.");
    body.append(method.getName());
    body.append("(");
    body.append(getParametersForCall());
    body.append(");");
    return body.toString();
  }

  public String getHead() {
    StringBuilder head = new StringBuilder();
    head.append("public ");
    head.append(method.getReturnType().getCanonicalName());
    head.append(" ");
    head.append(method.getName());
    head.append("(");
    head.append(getParametersForDeclaration());
    head.append(")");
    return head.toString();
  }

  public Method getMethod() {
    return method;
  }

  public void setMethod(Method method2) {
    this.method = method2;
  }

  private String getParametersForCall() {
    ArrayList<String> paramStrings = new ArrayList<String>();
    Parameter[] parameters = method.getParameters();
    for (Parameter parameter : parameters) {
      if (ReflectionUtil.isSelector(parameter.getType())) {
        paramStrings.add("this");
      }
      else {
        paramStrings.add(parameter.getName());
      }
    }
    return StringUtils.join(paramStrings, ", ");
  }

  private String getParametersForDeclaration() {
    ArrayList<String> paramStrings = new ArrayList<String>();
    Parameter[] parameters = method.getParameters();
    for (Parameter parameter : parameters) {
      if (ReflectionUtil.isSelector(parameter.getType())) {
        continue;
      }
      paramStrings.add(getParameterString(parameter));
    }
    return StringUtils.join(paramStrings, ", ");
  }

  private String getParameterString(Parameter parameter) {
    StringBuilder paramString = new StringBuilder();
    paramString.append(parameter.getType().getCanonicalName());
    paramString.append(" ");
    paramString.append(parameter.getName());
    return paramString.toString();
  }
}

@@ -1516,32 +1516,39 @@
 ppend(method.get
+Generic
 ReturnType().get
@@ -1543,33 +1543,28 @@
 rnType().get
-Canonical
+Type
 Name());%0A   
@@ -1970,32 +1970,37 @@
 %3CString%3E();%0A    
+for (
 Parameter%5B%5D para
@@ -1984,34 +1984,32 @@
   for (Parameter
-%5B%5D
  parameters = me
@@ -1994,35 +1994,34 @@
 ameter parameter
-s =
+ :
  method.getParam
@@ -2031,51 +2031,8 @@
 rs()
-;%0A    for (Parameter parameter : parameters
 ) %7B%0A
@@ -2376,16 +2376,21 @@
 ();%0A    
+for (
 Paramete
@@ -2394,10 +2394,8 @@
 eter
-%5B%5D
  par
@@ -2400,19 +2400,18 @@
 arameter
-s =
+ :
  method.
@@ -2429,51 +2429,8 @@
 rs()
-;%0A    for (Parameter parameter : parameters
 ) %7B%0A

use generics, minor refactoring