package com.agile.train.model;

import com.google.common.base.Joiner;
import com.google.common.collect.Lists;
import japa.parser.ast.body.MethodDeclaration;
import japa.parser.ast.body.Parameter;

import java.lang.reflect.Modifier;
import java.util.List;

public class MethodDisplayer {
    private String modifierText;
    private String modifier;
    private String showText;
    private String findText;

    public MethodDisplayer(MethodDeclaration methodDeclaration) {
        this.modifierText = Modifier.toString(methodDeclaration.getModifiers());
        this.modifier = getModifierBy(modifierText);

        List<Parameter> parameters = methodDeclaration.getParameters();

        this.showText = getShowTextBy(methodDeclaration, parameters);
        this.findText = getFindText(methodDeclaration, parameters);
    }

    private String getFindText(MethodDeclaration methodDeclaration, List<Parameter> parameters) {
        if (parameters == null || parameters.isEmpty()) return getDefaultFindText(methodDeclaration);

        StringBuilder findString = new StringBuilder();
        findString.append(methodDeclaration.getName()).append("(");
        Joiner.on(", ").appendTo(findString, parameters);
        findString.append(")");
        return findString.toString();
    }

    private String getShowTextBy(MethodDeclaration methodDeclaration, List<Parameter> parameters) {
        if (parameters == null || parameters.isEmpty()) return getDefaultShowText(methodDeclaration);

        StringBuilder showString = new StringBuilder();
        showString.append(methodDeclaration.getName()).append("(");

        List<String> parameterTypes = Lists.newArrayList();
        for (Parameter parameter : parameters) {
            parameterTypes.add(parameter.getType().toString());
        }
        Joiner.on(",").appendTo(showString, parameterTypes);
        showString.append(")").append(": ").append(methodDeclaration.getType().toString());
        return showString.toString();
    }

    private String getDefaultFindText(MethodDeclaration methodDeclaration) {
        return methodDeclaration.getName() + "()";
    }

    private String getDefaultShowText(MethodDeclaration methodDeclaration) {
        return getDefaultFindText(methodDeclaration) + ": " + methodDeclaration.getType().toString();
    }

    private String getModifierBy(String modifierText) {
        if (modifierText.contains("public")) return "public";
        if (modifierText.contains("protected")) return "protected";
        if (modifierText.contains("private")) return "private";
        return "default";
    }

    public String getModifier() {
        return modifier;
    }

    public String getModifierText() {
        return modifierText;
    }

    public String getShowText() {
        return showText;
    }

    public String getFindText() {
        return findText;
    }

    @Override
    public String toString() {
        return "MethodDisplayer{" +
                "modifierText='" + modifierText + '\'' +
                ", modifier='" + modifier + '\'' +
                ", showText='" + showText + '\'' +
                ", findText='" + findText + '\'' +
                '}';
    }
}

@@ -280,16 +280,266 @@
 layer %7B%0A
+    private static final String MODIFIER_PUBLIC = %22public%22;%0A    private static final String MODIFIER_PROTECTED = %22protected%22;%0A    private static final String MODIFIER_PRIVATE = %22private%22;%0A    private static final String MODIFIER_DEFAULT = %22default%22;%0A
     priv
@@ -2654,166 +2654,208 @@
 ins(
-%22public%22)) return %22public%22;%0A        if (modifierText.contains(%22protected%22)) return %22protected%22;%0A        if (modifierText.contains(%22private%22)) return %22private%22
+MODIFIER_PUBLIC)) return MODIFIER_PUBLIC;%0A        if (modifierText.contains(MODIFIER_PROTECTED)) return MODIFIER_PROTECTED;%0A        if (modifierText.contains(MODIFIER_PRIVATE)) return MODIFIER_PRIVATE
 ;%0A  
@@ -2867,25 +2867,32 @@
  return 
-%22default%22
+MODIFIER_DEFAULT
 ;%0A    %7D%0A

refactor code