/*******************************************************************************
 * Copyright 2014 Rafael Garcia Moreno.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/
package com.bladecoder.engineeditor.common;

import java.io.IOException;
import java.lang.annotation.Annotation;
import java.util.HashMap;
import java.util.Hashtable;

import com.badlogic.gdx.utils.reflect.ClassReflection;
import com.badlogic.gdx.utils.reflect.ReflectionException;
import com.bladecoder.engine.actions.Action;
import com.bladecoder.engine.actions.ActionDescription;
import com.bladecoder.engine.actions.ActionFactory;
import com.bladecoder.engine.util.ActionUtils;
import com.bladecoder.engine.util.EngineLogger;

import eu.infomas.annotation.AnnotationDetector;
import eu.infomas.annotation.AnnotationDetector.TypeReporter;

public class ActionDetector {

	private static HashMap<String, Class<?>> actions = null;

	public static void detect() {

		actions = new HashMap<String, Class<?>>();

		final TypeReporter reporter = new TypeReporter() {

			@SuppressWarnings("unchecked")
			@Override
			public Class<? extends Annotation>[] annotations() {
				return new Class[] { ActionDescription.class };
			}

			@SuppressWarnings("unchecked")
			@Override
			public void reportTypeAnnotation(Class<? extends Annotation> annotation, String className) {
				Class<Action> c = null;

				try {
					c = ClassReflection.forName(className);
				} catch (ReflectionException e) {
					e.printStackTrace();
				}

				String name = ActionUtils.getName(c);

				if (!name.equals("End"))
					actions.put(name, c);
			}

		};

		final AnnotationDetector cf = new AnnotationDetector(reporter);

		try {
			cf.detect();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static String[] getActionNames() {
		if (actions == null) {
			detect();
		}

		addCustomActions();

		return actions.keySet().toArray(new String[actions.size()]);
	}

	public static Action create(String name, HashMap<String, String> params) {
		Class<?> c = actions.get(name);
		
		if(c == null) {
			addCustomActions();
			 c = actions.get(name);
		}

		if (c == null) {
			EngineLogger.error("Action with name '" + name + "' not found.");

			return null;
		}

		try {
			return ActionFactory.createByClass(c.getName(), params);
		} catch (ClassNotFoundException | ReflectionException e) {
			EngineLogger.error("Action with name '" + name + "' not found.");

			return null;
		}
	}

	private static void addCustomActions() {
		Hashtable<String, Class<?>> coreClasses = ((FolderClassLoader) ActionFactory.getActionClassLoader())
				.getClasses();

		for (Class<?> c : coreClasses.values()) {
			if (c.getAnnotation(ActionDescription.class) != null) {
				actions.put(ActionUtils.getName(c), c);
			}
		}
	}

}

@@ -863,24 +863,52 @@
 Annotation;%0A
+import java.util.ArrayList;%0A
 import java.
@@ -2448,24 +2448,114 @@
 etect();%0A%09%09%7D
+%0A%09%09%0A%09%09ArrayList%3CString%3E names = new ArrayList%3CString%3E();%0A%09%09names.addAll(actions.keySet());
 %0A%0A%09%09addCusto
@@ -2563,16 +2563,21 @@
 Actions(
+names
 );%0A%0A%09%09re
@@ -2581,32 +2581,21 @@
 %09return 
-actions.keySet()
+names
 .toArray
@@ -2606,22 +2606,20 @@
  String%5B
-action
+name
 s.size()
@@ -2764,47 +2764,64 @@
 %0A%09%09%09
-addC
+// Search in c
 ustom
-A
+ a
 ctions
-();
 %0A%09%09%09
- 
 c = 
-actions.get
+getCustomActionByName
 (nam
@@ -3196,24 +3196,388 @@
 stomActions(
+ArrayList%3CString%3E names) %7B%0A%09%09Hashtable%3CString, Class%3C?%3E%3E coreClasses = ((FolderClassLoader) ActionFactory.getActionClassLoader())%0A%09%09%09%09.getClasses();%0A%0A%09%09for (Class%3C?%3E c : coreClasses.values()) %7B%0A%09%09%09if (c.getAnnotation(ActionDescription.class) != null) %7B%0A%09%09%09%09names.add(ActionUtils.getName(c));%0A%09%09%09%7D%0A%09%09%7D%0A%09%7D%0A%09%0A%09private static Class%3C?%3E getCustomActionByName(String name
 ) %7B%0A%09%09Hashta
@@ -3798,27 +3798,30 @@
 ) %7B%0A%09%09%09%09
-actions.put
+if(name.equals
 (ActionU
@@ -3835,20 +3835,33 @@
 tName(c)
-,
+))%0A%09%09%09%09%09%09return
  c
-)
 ;%0A%09%09%09%7D%0A%09
@@ -3859,18 +3859,36 @@
 c;%0A%09%09%09%7D%0A%09%09%7D%0A
+%09%09%0A%09%09return null;%0A
 %09%7D%0A%0A%7D%0A

Fix obtaining custom actions.