package ca.geepsy.lab.marsbot.craft;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

import ca.geepsy.lab.marsbot.api.Bot;
import ca.geepsy.lab.marsbot.api.BotEngine;
import ca.geepsy.lab.marsbot.api.EnergyProvider;
import ca.geepsy.lab.marsbot.shop.SimpleBattery;
import ca.geepsy.lab.marsbot.shop.SimpleEngine;

public class BotFactory {

	private static BotFactory instance;
	
	
	public static BotFactory getInstance(){
		if(instance == null){
			instance = new BotFactory();
		}
		return instance;
	}
	
	public <T extends Bot> T createBot(Class<T> pattern){
		T bot = null;
		if(pattern == null){
			System.out.println("Craftman: you didn't give me any pattern!");
		}
		else{
			System.out.println("Craftman: you want a "+pattern.getName());
			try {
				Constructor<T> botConstructor = pattern.getConstructor();
				bot = botConstructor.newInstance();
				EnergyProvider energyProvider = new SimpleBattery(100);
				BotEngine engine = new SimpleEngine(energyProvider);
				bot.setEngine(engine);
			} catch (SecurityException e) {
			} catch (NoSuchMethodException e) {
			} catch (IllegalArgumentException e) {
			} catch (InstantiationException e) {
			} catch (IllegalAccessException e) {
			} catch (InvocationTargetException e) {
			}
		}
		return bot;
	}
}
