package ru.lanwen.raml.rarc.api.ra;

import com.squareup.javapoet.ClassName;
import com.squareup.javapoet.MethodSpec;
import org.raml.model.Resource;

import static javax.lang.model.element.Modifier.PUBLIC;
import static org.apache.commons.lang3.StringUtils.isNotEmpty;
import static org.apache.commons.lang3.StringUtils.substringAfter;
import static org.apache.commons.lang3.StringUtils.uncapitalize;
import static ru.lanwen.raml.rarc.api.ApiResourceClass.className;
import static ru.lanwen.raml.rarc.api.ApiResourceClass.classPart;
import static ru.lanwen.raml.rarc.api.ApiResourceClass.packageName;

/**
 * @author lanwen (Merkushev Kirill)
 */
public class NextResourceMethods {
    
    public static MethodSpec baseResource(Resource resource, String basePackage, String confFName, String reqSupplFName) {
        return MethodSpec.methodBuilder(uncapitalize(substringAfter(className(resource), "Api")))
                .returns(ClassName.get(basePackage + "." + packageName(resource), className(resource)))
                .addStatement("return new $N($N.$N.get())", className(resource), confFName, reqSupplFName)
                .addModifiers(PUBLIC)
                .build();
    }
 
    public static MethodSpec childResource(Resource resource, String basePackage, String reqSpecFName) {
        String methodName = uncapitalize(isNotEmpty(resource.getDisplayName()) 
                ? classPart(resource) 
                : substringAfter(className(resource), "Api"));
        
        return MethodSpec.methodBuilder(methodName)
                .returns(ClassName.get(basePackage + "." + packageName(resource), className(resource)))
                .addStatement("return new $N($N)", className(resource), reqSpecFName)
                .addModifiers(PUBLIC)
                .build();
    }
}

@@ -204,138 +204,8 @@
 IC;%0A
-import static org.apache.commons.lang3.StringUtils.isNotEmpty;%0Aimport static org.apache.commons.lang3.StringUtils.substringAfter;%0A
 impo
@@ -265,16 +265,16 @@
 talize;%0A
+
 import s
@@ -731,32 +731,17 @@
 ize(
-substringAfter(
 class
-Name
+Part
 (res
@@ -746,24 +746,16 @@
 esource)
-, %22Api%22)
 ))%0A     
@@ -1182,145 +1182,26 @@
 ize(
-isNotEmpty(resource.getDisplayName()) %0A                ? classPart(resource) %0A                : substringAfter(className(resource), %22Api%22
+classPart(resource
 ));%0A

simplify api method names