/*
 * Created on 15 December 2005, 10:46
 *
 * (C) 2005 Copyright ANSTO. All rights reserved.
 */

package au.gov.ansto.bragg.nexus.base;

import java.beans.PropertyVetoException;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

/** Intermediate implementor for managing typed data context.
 *
 * @author Darren Kelly, Hugh Rayner
 */
public class NXtyped_ extends NXobject_ implements NXtyped {
    
    static final protected String DEFAULT_TYPE_STRING = "NX_CHAR";//TODO justify default strategy     
    
    /**
     * Creates a new instance of NXtyped_
     */
    public NXtyped_() {
    }
    
    static private Map<String,Integer> types = new HashMap();
    static protected void setNXtypeFor(String property,int type) {
        types.put(property,type);
    }
    static protected int getNXtypeFor(String property) {
        return types.get(property);
    }
    
    /** To be written in XML as attribute of an element */
    private Integer nXtype_ =  null;
    transient private Integer nXtype0 =  null;
    public Integer getNXtype() {
        if (nXtype_ == null) return NXtypes.NX_CHAR;
        return nXtype_;
    }
    public void setNXtype(Integer type) throws PropertyVetoException {
        //publish().veto($NXtype,nXtype_,type);
        nXtype0 = nXtype_;
        nXtype_ = type;
        //publish().change($NXtype,nXtype0,nXtype_);
    }   
    
    /** <b>author:</b> hrz 
     */
    public String typeAsString() {
        //XXX DIRTY HACK
        if(getContent() instanceof Float || getContent() instanceof Float[]
                 || getContent() instanceof Float[][]  || getContent() instanceof float[][]
                 || getContent() instanceof float[])
            return "NX_FLOAT";
        if(getContent() instanceof Integer || getContent() instanceof Integer[])
            return "NX_INT";
        String baseType = DEFAULT_TYPE_STRING;
        for(Field f : NXtypes.class.getFields()) {
            try{
                /*
                 debug().echo("Field",f);
                if(f.getName().startsWith(NX_PREFIX))
                    debug().echo("Field.getInt",f.getInt(new NXtypes()));
                debug().echo("f.getName",f.getName());
                debug().echo("getType",getType());
                debug().echo("getType==getInt",getType()==f.get(new NXtypes()));
                */
                 if(
                         f.getName().startsWith(NX_PREFIX) 
                         && 
                         f.get(new NXtypes()) == getNXtype()) {
                    baseType = f.getName();
                }
            } catch(Exception e) {
                e.printStackTrace();
            }
        }
        return baseType;
    }
    
    /** To be written in XML as content of an element */
    private Object content_ = null;
    /** @inheritDoc */
    public Object getContent() {
        return content_;
    }
    /** @inheritDoc */
    public void setContent(Object content) //TODO: strong type check
    //? throws WrongContentException
    {
        content_ = content;
    }
//    static{
//        registerNeXusField($content,NXtyped_.class);
//    }   
}
