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

import hu.swankey.ammo.common.script.yunits.YAttribute;


public class BooleanField extends YAttribute {
	


	public BooleanField(String name) {
		this(name, yclass(), null);
	}	

	protected BooleanField(String name, BooleanType type, Object value) {
		super(name, type, value);
	}
	
	public static BooleanType yclass(){
		return BooleanType.singleton();
	}

	@Override
	public void setValue(Object value) {
		
		if (value instanceof Boolean) {
			super.setValue(value);
		} else {
			throw new RuntimeException("'value' should be Boolean, but is is a '" +
					(value == null? "null" : value.getClass()) + "'");
		}
	}

	@Override
	public Boolean getValue() {
		return (Boolean) super.getValue();
	}

	@Override
	public String toString() {
		return super.toString() + ": " + (getValue() ? "true" : "false");
	}

	@Override
	public BooleanType getYClass() {
		return (BooleanType) super.getYClass();
	}
	
	public Object clone(){
		BooleanField newBool = new BooleanField(getName());
		newBool.setValue( getValue() );
		return newBool;
	}	

	public static class BooleanType extends FieldClass {

		private static final String CLASS_NAME = "Boolean";
		public static final Boolean DEFAULT_VALUE = false;
		
		private static BooleanType singleton;


		public static BooleanType singleton() {
			if (singleton == null) {
				singleton = new BooleanType(CLASS_NAME);
				singleton.addSuperior(YAttribute.yclass());
				placeClass(singleton);
			}
			return singleton;
		}

		/** Instantiate the FieldType */
		protected BooleanType(String name) {
			super(name);
		}



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



        @Override
        public Boolean getDefaultValue() {
            return DEFAULT_VALUE;
        }	
	    
		
        @Override
        public String getName() {
            return CLASS_NAME;
        }
	}
}
