package com.kiselev.reflection.ui.method;

import com.kiselev.reflection.ui.annotation.AnnotationsUtils;
import com.kiselev.reflection.ui.argument.ArgumentUtils;
import com.kiselev.reflection.ui.exception.ExceptionUtils;
import com.kiselev.reflection.ui.generic.GenericsUtils;
import com.kiselev.reflection.ui.indent.IndentUtils;
import com.kiselev.reflection.ui.modifier.ModifiersUtils;

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

public class MethodUtils {

    public String getMethods(Class<?> clazz) {
        String methods = "";

        List<String> methodList = new ArrayList<String>();
        for (Method method : clazz.getDeclaredMethods()) {
            boolean isImplementation = clazz.isInterface() || !isRealization(method);
            methodList.add(getMethod(method, isImplementation));
        }

        if (!methodList.isEmpty()) {
            methods += String.join("\n\n", methodList) + "\n";
        }

        return methods;
    }

    private boolean isRealization(Method method) {
        return !(Modifier.isAbstract(method.getModifiers()) || Modifier.isNative(method.getModifiers()));
    }

    private String getMethod(Method method, boolean isImplementation) {
        String methodSignature = "";

        String annotations = new AnnotationsUtils().getAnnotations(method);

        String indent = new IndentUtils().getIndent(method);

        String modifiers = new ModifiersUtils().getModifiers(method.getModifiers());

        String generics = new GenericsUtils().getGenerics(method);

        String returnType = new GenericsUtils().resolveType(method.getGenericReturnType());

        String methodName = method.getName();

        String arguments = new ArgumentUtils().getArguments(method);

        String exceptions = new ExceptionUtils().getExceptions(method);

        String implementation = isImplementation ? ";" : " {\n" + indent + "}";

        methodSignature += annotations + indent + modifiers + generics + returnType + " " + methodName + arguments + exceptions + implementation;

        return methodSignature;
    }
}

@@ -750,95 +750,8 @@
  %7B%0D%0A
-            boolean isImplementation = clazz.isInterface() %7C%7C !isRealization(method);%0D%0A
     
@@ -793,26 +793,8 @@
 thod
-, isImplementation
 ));%0D
@@ -960,176 +960,8 @@
 %0D%0A%0D%0A
-    private boolean isRealization(Method method) %7B%0D%0A        return !(Modifier.isAbstract(method.getModifiers()) %7C%7C Modifier.isNative(method.getModifiers()));%0D%0A    %7D%0D%0A%0D%0A
     
@@ -1002,34 +1002,8 @@
 thod
-, boolean isImplementation
 ) %7B%0D
@@ -1654,49 +1654,44 @@
 ing 
-implementation = isImplementation ? %22;%22 :
+body = isMethodRealization(method) ?
  %22 %7B
@@ -1708,16 +1708,22 @@
 nt + %22%7D%22
+ : %22;%22
 ;%0D%0A%0D%0A   
@@ -1853,57 +1853,220 @@
 s + 
-implementation;%0D%0A%0D%0A        return methodSignature
+body;%0D%0A%0D%0A        return methodSignature;%0D%0A    %7D%0D%0A%0D%0A    private boolean isMethodRealization(Method method) %7B%0D%0A        return !Modifier.isAbstract(method.getModifiers()) && !Modifier.isNative(method.getModifiers())
 ;%0D%0A 

Fix method's body displaying