package com.robohorse.robopojogenerator.generator.postprocessors;

import com.robohorse.robopojogenerator.generator.ClassItem;
import com.robohorse.robopojogenerator.models.GenerationModel;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * This is the KotlinDataClassPostProcessor class
 * Please put some info here.
 *
 * @author Wafer Li
 * @since 16/10/24 00:30
 */
public class KotlinDataClassPostProcessor extends AbsPostProcessor {

    @Override
    public String proceedClassBody(ClassItem classItem, GenerationModel generationModel) {

        removeClassImportSemicolon(classItem);

        StringBuilder       classBodyBuilder = new StringBuilder();
        Map<String, String> classFields      = classItem.getClassFields();

        for (String objectName : classFields.keySet()) {

            String type = proceedType(classFields.get(objectName));
            String annotation = proceedAnnotation(classItem.getAnnotation());

            classBodyBuilder.append(
                    classTemplateProcessor
                            .createKotlinDataClassField(type, objectName, annotation));
        }

        return classBodyBuilder.toString();
    }


    private String proceedAnnotation(String annotation) {
        return annotation.replaceAll("@", "@field:");
    }


    private String proceedType(String type) {

        type = type.replaceAll("Object", "Any");
        return type.replaceAll(">", "?>");
    }


    private void removeClassImportSemicolon(ClassItem classItem) {

        Set<String> classImports = classItem.getClassImports();

        Set<String> classImportWithoutSemicolon = new HashSet<String>();

        for (String classImport : classImports) {
            classImport = classImport.substring(0, classImport.indexOf(";"));
            classImportWithoutSemicolon.add(classImport);
        }

        classImports.clear();
        classImports.addAll(classImportWithoutSemicolon);
    }


    @Override
    public String createClassTemplate(ClassItem classItem, String classBody) {
        return classTemplateProcessor.createClassBodyKotlinDataClass(classItem, classBody);
    }
}

@@ -120,16 +120,83 @@
 ssItem;%0A
+import com.robohorse.robopojogenerator.generator.consts.ClassType;%0A
 import c
@@ -250,16 +250,16 @@
 nModel;%0A
-
 %0Aimport 
@@ -320,16 +320,80 @@
 til.Set;
+%0Aimport java.util.regex.Matcher;%0Aimport java.util.regex.Pattern;
 %0A%0A/**%0A *
@@ -972,16 +972,22 @@
 ng type 
+      
 = procee
@@ -1386,24 +1386,25 @@
 notation) %7B%0A
+%0A
         retu
@@ -1512,47 +1512,423 @@
     
-type = type.replaceAll(%22Object%22,
+final String regex = %22%3C(%5B%5E%3C%3E%5D*)%3E%22;%0A        final Pattern pattern = Pattern.compile(regex);%0A        final Matcher matcher = pattern.matcher(type);%0A%0A        String innerType;%0A        if (matcher.groupCount() != 0) %7B%0A            // Type is List%0A%0A            innerType = generateHelper.upperCaseFirst(matcher.group(0));%0A%0A            if (innerType.equals(ClassType.OBJECT.getBoxed())) %7B%0A                innerType =
  %22Any%22
-)
 ;%0A  
@@ -1937,41 +1937,489 @@
     
+ 
-return type.replaceAll(%22%3E%22, %22?%3E%22)
+   %7D%0A            else if (innerType.equals(ClassType.INTEGER.getBoxed())) %7B%0A                innerType = %22Int%22;%0A            %7D%0A%0A            type = type.replaceAll(regex, %22%3C%22 + innerType + %22%3E%22);%0A            type = type.replaceAll(%22%3E%22, %22?%3E%22);%0A        %7D%0A        else %7B%0A            // Type is object or primitive%0A%0A            type = generateHelper.upperCaseFirst(type);%0A%0A            if (type.equals(%22Object%22)) %7B%0A                type = %22Any%22;%0A            %7D%0A        %7D%0A%0A        return type
 ;%0A  
@@ -2930,32 +2930,32 @@
 %0A%0A    @Override%0A
-
     public Strin
@@ -3013,24 +3013,25 @@
 lassBody) %7B%0A
+%0A
         retu

Change the type to kotlin style