package com.github.gfx.android.orma.processor;

import com.github.gfx.android.orma.annotation.Column;
import com.github.gfx.android.orma.annotation.PrimaryKey;
import com.github.gfx.android.orma.annotation.Table;
import com.squareup.javapoet.ClassName;

import java.util.ArrayList;
import java.util.List;

import javax.lang.model.element.TypeElement;

public class SchemaDefinition {

    final TypeElement typeElement;

    final ClassName modelClassName;

    final ClassName schemaClassName;

    final ClassName relationClassName;

    final ClassName updaterClassName;

    final ClassName deleterClassName;

    final String tableName;

    final List<ColumnDefinition> columns = new ArrayList<>();

    public SchemaDefinition(TypeElement typeElement) {
        this.typeElement = typeElement;
        this.modelClassName = ClassName.get(typeElement);

        Table table = typeElement.getAnnotation(Table.class);
        this.schemaClassName = helperClassName(table.schemaClassName(), modelClassName, "_Schema");
        this.relationClassName = helperClassName(table.relationClassName(), modelClassName, "_Relation");
        this.updaterClassName = helperClassName(table.updaterClassName(), modelClassName, "_Updater");
        this.deleterClassName = helperClassName(table.deleterClassName(), modelClassName, "_Deleter");
        this.tableName = firstNonEmptyName(table.value(), modelClassName.simpleName());

        typeElement.getEnclosedElements().forEach(element -> {
            if (element.getAnnotation(Column.class) != null || element.getAnnotation(PrimaryKey.class) != null) {
                columns.add(new ColumnDefinition(element));
            }
        });
    }

    private static ClassName helperClassName(String specifiedName, ClassName modelClassName, String helperSuffix) {
        String simpleName = firstNonEmptyName(specifiedName, modelClassName.simpleName() + helperSuffix);
        return ClassName.get(modelClassName.packageName(), simpleName);
    }

    static String firstNonEmptyName(String... names) {
        for (String name : names) {
            if (name != null && !name.equals("")) {
                return name;
            }
        }
        throw new AssertionError("No non-empty string found");
    }

    public TypeElement getElement() {
        return typeElement;
    }

    public String getPackageName() {
        return schemaClassName.packageName();
    }

    public String getTableName() {
        return tableName;
    }

    public ClassName getModelClassName() {
        return modelClassName;
    }

    public ClassName getSchemaClassName() {
        return schemaClassName;
    }

    public ClassName getRelationClassName() {
        return relationClassName;
    }

    public ClassName getUpdaterClassName() {
        return updaterClassName;
    }

    public ClassName getDeleterClassName() {
        return deleterClassName;
    }

    public List<ColumnDefinition> getColumns() {
        return columns;
    }

    public List<ColumnDefinition> getColumnsWithoutAutoId() {
        List<ColumnDefinition> list = new ArrayList<>(columns.size());
        for (ColumnDefinition c : columns) {
            if (!c.autoId) {
                list.add(c);
            }
        }
        return list;
    }

    public ColumnDefinition getPrimaryKey() {
        for (ColumnDefinition c : columns) {
            if (c.primaryKey) {
                return c;
            }
        }
        return null;
    }

    @Override
    public String toString() {
        return getModelClassName().simpleName();
    }
}

@@ -297,16 +297,52 @@
 il.List;
+%0Aimport java.util.stream.Collectors;
 %0A%0Aimport
@@ -3095,217 +3095,83 @@
     
-List%3CColumnDefinition%3E list = new ArrayList%3C%3E(columns.size());%0A        for (ColumnDefinition c : columns) %7B%0A            if (!c.autoId) %7B%0A                list.add(c);%0A            %7D%0A        %7D%0A        return list
+return columns.stream().filter(c -%3E !c.autoId).collect(Collectors.toList())
 ;%0A  

use Java8 API