package background;

import hu.swankey.ammo.common.controll.AmmoException;
import hu.swankey.ammo.common.script.Synthetizer;
import hu.swankey.ammo.common.yggdrasil.basics.ComplexYObject;
import hu.swankey.ammo.common.yggdrasil.basics.YContainer;
import hu.swankey.ammo.common.yggdrasil.basics.ComplexYObject.ComplexYClass;
import hu.swankey.ammo.common.yggdrasil.definition.ComplexYClassDefinition;
import hu.swankey.ammo.common.yggdrasil.ext.Command;
import hu.swankey.ammo.common.yggdrasil.ext.Universe;
import hu.swankey.ammo.common.yggdrasil.ext.World;
import hu.swankey.ammo.common.yggdrasil.ext.FieldObject.FieldObjectClass;
import surface.CommandBar;
import client_engine.ClientController;


public class Controller extends ClientController {

    
   
    /** A futás befejeződik, ha ennek értéke false-ra vált */
    private boolean running = true;


    private CommandBar commandBar;

    /* --------------------------------------------------------------------- */            
    /* --- Létrehozás --- */   
    
    
    /** Constructor 
     * @throws AmmoException */
    public Controller(String name, String clientver) throws AmmoException {
        super(name, clientver);
        
        YContainer multiverse = getYggdrasil().getRoot().getMultiverse();        
        Universe universe = Universe.yclass().create("Playground");
        Universe universe2 = Universe.yclass().create("AM");
        Universe universe3 = Universe.yclass().create("AnotherUniverse");
        multiverse.put(universe);
        multiverse.put(universe2);
        multiverse.put(universe3);
        
        World world1 = World.yclass().create("World1");
        World world2 = World.yclass().create("World2");
        universe.addWorld(world1);
        universe.addWorld(world2);
       
        setUniverse(universe);
        setWorld(world1);
        

//        String objStr = "public FieldObject {\n" +
//        		"   public basics.Integer coordx;\n" +
//        		"   public basics.Integer coordy;\n" +
//        		"}";
        
        String fragObjStr = "public FragileObject extends ext.FieldObject {\n" +
		"   private basics.Integer life;" +
		"}";
        
        String charStr = "public Character extends ext.FragileObject {\n" +
        		"   public basics.Integer strength;\n" +
				"   public basics.Integer dexterity;\n" +
				"   public basics.Integer vitality;\n" +
				"   public basics.Integer charisma;\n" +
				"   public basics.Boolean gender;\n" +
				"   public basics.Void fire(ext.FieldObject target) {\n" +
				"      target.life = target.life - 10;\n" +
				"   }\n"+
				"   public basics.Void south() { coordy = coordy + 1; }\n"+
				"   public basics.Void east()  { coordx = coordx + 1; }\n"+
				"   public basics.Void north() { coordy = coordy - 1; }\n"+
				"   public basics.Void west() { coordx = coordx - 1; }\n"+
				"}";
        
        
        String movingObjStr = "public MovingObject extends ext.FieldObject {\n" +
		"   public basics.Integer irany;\n" +
		"   public basics.Void autoscript(){\n" +
		"      switch(irany) {\n" +
		"         case 0: {\n" +
		//"             coordx = coordx + 1;\n" +
		"             south(); \n" +
		"             irany = 1;\n" +
		"         }\n" +
		"         case 1: {\n" +
		//"             coordy = coordy + 1;\n" +
		"             west(); \n" +
		"             irany = 2;\n" +
		"         }\n" +
		"         case 2: {\n" +
		//"             coordx = coordx - 1;\n" +
		"               north();\n" +
		"             irany = 3;\n" +
		"         }\n" +
		"         case 3: {\n" +
		//"             coordy = coordy - 1;\n" +
		"               east();" +
		"             irany = 0;\n" +
		"         }\n" +
		"      }\n" +
		"   }\n" +
		"}";
        
        String movingCharStr = "public MovingCharacter extends ext.MovingObject, ext.Character {\n" +
        "}";
        
        
        Command fire = Command.yclass().create("fire");
        fire.setTitle("Fire!!");
        fire.setCommand("fire(target: %2);");
        fire.setPrimaryClass("ext.Character");
        fire.setSecondaryClass("ext.FragileObject");
        universe.addCommand(fire);
        
        Command north = Command.yclass().create("north");
        north.setTitle("Step North /\\");
        north.setCommand("north();");
        north.setPrimaryClass("ext.Character");
        universe.addCommand(north); 
        
        Command east = Command.yclass().create("east");
        east.setTitle("Step East ->");
        east.setCommand("east();");
        east.setPrimaryClass("ext.Character");
        universe.addCommand(east); 
        
        Command south = Command.yclass().create("south");
        south.setTitle("Step South \\/");
        south.setCommand("south();");
        south.setPrimaryClass("ext.Character");
        universe.addCommand(south);
        
        Command west = Command.yclass().create("west");
        west.setTitle("Step West <-");
        west.setCommand("west();");
        west.setPrimaryClass("ext.Character");
        universe.addCommand(west);

        
//        ComplexClassDefinition objDef = Synthetizer.createCustomClassDefinition(objStr);
        FieldObjectClass.singleton();
        ComplexYClassDefinition fragObjDef = Synthetizer.createCustomClassDefinition(fragObjStr);
        ComplexYClassDefinition charDef = Synthetizer.createCustomClassDefinition(charStr);
        ComplexYClassDefinition movingObjDef = Synthetizer.createCustomClassDefinition(movingObjStr);
        ComplexYClassDefinition movingCharDef = Synthetizer.createCustomClassDefinition(movingCharStr);
           
        ComplexYClass objType = fragObjDef.getWrapped();
        
        YContainer olist = world1.getObjectsContainer();

        ComplexYObject obj1 = objType.create("obj1");
        obj1.set("coordx", 1);
        obj1.set("coordy", 1);
        obj1.set("life", 100);
        olist.put(obj1);
        
        ComplexYObject obj2 = objType.create("obj2");
        obj2.set("coordx", 10);
        obj2.set("coordy", 12);
        obj2.set("life", 80);
        olist.put(obj2);
        
        ComplexYObject obj3 = FieldObjectClass.singleton().create("MassiveMass");
        obj3.set("coordx", 7);
        obj3.set("coordy", 2);
        olist.put(obj3);
        
        ComplexYObject obj4 = movingCharDef.getWrapped().create("JohhnieWalker");
        obj4.set("coordx", 4);
        obj4.set("coordy", 13);
        obj4.set("irany", 1);
        obj4.set("life", 68);
        olist.put(obj4);
        
        ComplexYObject player = charDef.getWrapped().create("Player");
        player.set("coordx", 7);
        player.set("coordy", 7);
        player.set("life", 100);
        olist.put(player);
        
        setPrimaryElement(player);
        
        
//        String A = "public A { public basics.Integer elementA; }";
//        String B = "public B { public basics.Integer elementB; }";
//        String C = "public C extends ext.A, ext.B {public basics.Integer elementC; }";
//        String D = "public D extends ext.C { public basics.Integer elementD; }";
        
//        Syntetizer.createCustomClassDefinition(A);
//        Syntetizer.createCustomClassDefinition(B);
//        Syntetizer.createCustomClassDefinition(C);
//        ComplexClassDefinition classD = Syntetizer.createCustomClassDefinition(D);
   
//        obj1.put( classD.getWrapped().create("naezmi") );
        
        
        
        

    }    
    
    
    
    /* --------------------------------------------------------------------- */   
    
    
    /** Fókuszban levő area beállítása */
//    public void setFocusedArea(Area area){
//        assert area != null;
//        this.focusedArea = area;
//    }    
//    

//    public World getWorld(){
//        return getYggdrasil().getWorld();
//    }
    
    
    

    /** Kliens típusa */
    /*public String getClientType(){
        return cbase.getClientType();
    }*/
    
    /** Kliens verziószáma */
    /*public String getClientVer(){
        return cbase.getClientVer();
    }    */
    
    /** Fut-e még a rendszer, vagy leállítás alatt van? */
    public boolean isSystemRunning(){
        return running;
    }

    
//    public CommandBar getCommandBar(){
//    	if (commandBar == null)
//    		commandBar = CommandBar.createCommandBar(this);
//    	
//    	return commandBar;
//    }

}
