package hu.swankey.ammo.common.yggdrasil.ext;

import hu.swankey.ammo.common.script.yunits.YClass;
import hu.swankey.ammo.common.script.yunits.YMethod;
import hu.swankey.ammo.common.yggdrasil.basics.ComplexYObject;
import hu.swankey.ammo.common.yggdrasil.basics.YInteger;
import hu.swankey.ammo.common.yggdrasil.basics.YContainer;
import hu.swankey.ammo.common.yggdrasil.basics.YObject;
import hu.swankey.ammo.common.yggdrasil.definition.YAttributeDefinition;

import java.util.Map;

public class World extends ComplexYObject {
	

	protected World(String name, ComplexYClass type) {
		super(name, type);
	}
	
	public YContainer getObjectsContainer(){
		return (YContainer) get(WorldClass.KEY_OBJECTS);
	}
	
    public static WorldClass yclass() {
    	return WorldClass.singleton();
    }
    
    @Override
    public WorldClass getYClass(){
        return (WorldClass) super.getYClass();
    }
    
    

	public static class WorldClass extends ComplexYClass {

    	private static final String CLASS_NAME = "World";
        private static final String KEY_OBJECTS = "objects";    	
    	private static WorldClass singleton;
    	
    	public static WorldClass singleton() {
    		
    		if (singleton == null){
        		singleton = new WorldClass(CLASS_NAME);
        		singleton.addMethod(Put.function);
        		singleton.addAttribute( YAttributeDefinition.yclass().create(KEY_OBJECTS, YContainer.yclass(), null));        		
        		placeClass(singleton);
    		}
    		
    		return singleton;
    	}
    	
    	protected WorldClass(String name) {
    		super(name, null);
    	}
    	
    	
    	@Override
    	public World create(String name) {
    		World newWorld = new World(name, this);
    		init(newWorld);
    		return newWorld;
    	}
    	
	}
	
	private static class Put extends YMethod {

		private static final String FUNCTION_NAME = "put";
		private static final String KEY_OBJ = "obj";
		private static final String KEY_COORDX = "coordx";
		private static final String KEY_COORDY = "coordy";
		private static final YClass YCLASS_OBJ = ComplexYObject.yclass();
		private static final YClass YCLASS_COORDX = YInteger.yclass();
		private static final YClass YCLASS_COORDY = YInteger.yclass();

		public static final Put function = new Put();

		protected Put() {
			super(FUNCTION_NAME);
			addParameter(KEY_OBJ, YCLASS_OBJ, null);
			addParameter(KEY_COORDX, YCLASS_COORDX, null);
			addParameter(KEY_COORDY, YCLASS_COORDY, null);
			setReturnType( YObject.yclass() );
		}

		@Override
		public YObject run(Map<String, YObject> params, YObject thisElement) {
			
			World thisE = (World) thisElement;
			ComplexYObject obj = (ComplexYObject)params.get(KEY_OBJ);
			Integer coordx = ((YInteger)params.get(KEY_COORDX)).getValue();
			Integer coordy = ((YInteger)params.get(KEY_COORDY)).getValue();
			
			thisE.getObjectsContainer().put(obj);
			
			obj.set("coordx", coordx);
			obj.set("coordy", coordy);
			
			return obj;
		}
	}
}

