package com.artemis.generator.strategy.e;

import com.artemis.generator.common.IterativeModelStrategy;
import com.artemis.generator.model.FluidTypes;
import com.artemis.generator.model.artemis.ComponentDescriptor;
import com.artemis.generator.model.type.MethodDescriptor;
import com.artemis.generator.model.type.TypeModel;
import com.artemis.generator.util.MethodBuilder;

import java.lang.reflect.Modifier;

import static org.reflections.ReflectionUtils.*;

/**
 * Generate boolean accessors for flag components.
 * <p>
 * Flag components are all components with no public fields and methods.
 *
 * @author Daan van Yperen
 */
public class FieldComponentBooleanAccessorStrategy extends IterativeModelStrategy {

    @Override
    protected void apply(ComponentDescriptor component, TypeModel model) {
        Class type = component.getComponentType();

        if (component.isFlagComponent()) {
            model.add(createCheckFlagComponentExistenceMethod(component));
            model.add(createFlagComponentToggleMethod(component));
        }
    }

    private MethodDescriptor createFlagComponentToggleMethod(ComponentDescriptor component) {
        return
                new MethodBuilder(FluidTypes.E_TYPE, component.getMethodPrefix())
                        .parameter(boolean.class, "value")
                        .mapper("if (value) ", component, ".create(entityId)")
                        .mapper("if (!value) ", component, ".remove(entityId)")
                        .debugNotes("flag component(=field/method-less) " + component.getComponentType().getName())
                        .returnFluid()
                        .build();
    }

    /**
     * T componentName() -> create new entity.
     */
    private MethodDescriptor createCheckFlagComponentExistenceMethod(ComponentDescriptor component) {
        return
                new MethodBuilder(boolean.class, "is" + component.getName())
                        .debugNotes("flag component(=field/method-less) " + component.getComponentType().getName())
                        .mapper("return ", component, ".has(entityId)")
                        .build();
    }
}

@@ -1335,23 +1335,8 @@
 per(
-%22if (value) %22, 
 comp
@@ -1344,22 +1344,19 @@
 nent, %22.
-create
+set
 (entityI
@@ -1360,88 +1360,15 @@
 tyId
-)%22)%0A                        .mapper(%22if (!value) %22, component, %22.remove(entityId
+, value
 )%22)%0A

Simplify boolean accessor.