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

import hu.swankey.ammo.common.script.yunits.YClass;
import hu.swankey.ammo.common.script.yunits.YMethod;

import java.util.Map;

public class YSimpleClass extends YClass {

    /** The name of the type */
    private static final String CLASS_NAME = "Field";
    
    /** The instantiated type */
    private static YSimpleClass singleton;

    /** Default value */
    private static final Object DEFAULT_VALUE = 0;
    
    public static YSimpleClass singleton() {
        if (singleton == null) {
            singleton =  new YSimpleClass(CLASS_NAME);
            singleton.addSuperior( YObject.yclass() );
            singleton.addMethod(Assignment.function);
            placeClass(singleton);
        }
        return singleton;
    }




    /** Instantiate a general FieldType */
    /*private FieldType() {
        super(TYPENAME);
    }*/



    /** Instantiate a specific FieldType */
    protected YSimpleClass(String name) {
        super(name, null);
    }



//    public Object getDefaultValue() {
//        return DEFAULT_VALUE;
//    }


    
    public YObject create(String name) {
         return new SimpleObject(name, this, null);
    }

//    @Override
//    public Field create(String name, Object value) {
//        return new Field(name, value);
//    }



//    /** Get the instantiated general FieldType */
//    public static FieldType type() {
//        return TYPE;
//    }



    /** Get the name of the type */
    @Override
    public String getName() {
        return CLASS_NAME;
    }
    
    public Object getDefaultValue() {
        return DEFAULT_VALUE;
    }
    
    
    private static class Assignment extends YMethod {

        private static final String FUNCTION_NAME = "=";
        private static final String KEY_VALUE = "value";
        private static final YClass YCLASS_VALUE = YSimpleClass.singleton();

        public static final Assignment function = new Assignment();

        protected Assignment() {
            super(FUNCTION_NAME);
            addParameter(KEY_VALUE, YCLASS_VALUE, null);
            setReturnType(SimpleObject.yclass());
        }

        @Override
        public YObject run(Map<String, YObject> params, YObject thisElement) {
            
            int newValue = ((IntegerField)params.get(KEY_VALUE)).getValue();
            ((SimpleObject)thisElement).setValue( newValue );

            return thisElement;
        }
    }
}