/*
 * RemoteManagedObject.java
 *
 * Created on June 9, 2003, 5:17 PM
 */

package hep.aida.ref.remote;

import java.util.logging.Logger;
import hep.aida.dev.IDevMutableStore;
import hep.aida.ref.AidaUtils;
import hep.aida.ref.ManagedObject;
import hep.aida.ref.event.IsObservable;
import hep.aida.ref.event.HistogramEvent;

/**
 * Base class for all IManagedObjects in local AIDA Tree 
 * that are copies of remote AIDA Tree objects. If dataIsValid
 * state changes, event is fired to notify listeners.
 * @author  serbo
 */
public abstract class RemoteManagedObject extends ManagedObject implements IsObservable {

    protected IDevMutableStore store = null;
    protected String treePath = null;
    protected String aidaType = "IManagedObject";
    protected boolean dataIsValid = false;
    protected Logger remoteLogger = Logger.getLogger("hep.aida.ref.remote");
    //protected long timeOfLastUpdate; // in milliseconds
    //protected String xAxisType;
    //protected String yAxisType;

    /** Creates a new instance of RemoteManagedObject */
    public RemoteManagedObject(String name) {
        super(name);
        fillable = false;
        //xAxisType = null;
        //yAxisType = null;
    }

    protected void makeSureDataIsValid() {
        if (dataIsValid || fillable) return; 
        
        if (store instanceof RemoteMutableStore) 
            ((RemoteMutableStore) store).handleDataUpdate(this, treePath, aidaType);
        else 
            store.updateData(treePath, aidaType);
    }
    
    public String getAIDAType() {
        if (aidaType != null && !aidaType.trim().equals("")) return aidaType;
        else return super.getAIDAType();
    }
    
    public void setStore(IDevMutableStore store) {
        this.store = store;
    }
    
    //public void setXAxisType(String type) { xAxisType = type; }
    //public String getXAxisType() { return xAxisType; }
    
    //public void setYAxisType(String type) { yAxisType = type; }
    //public String getYAxisType() { return yAxisType; }
    
    /** Set what folder this histogram belongs to. */
    public void setTreeFolder(String treeFolder) { 
        String histName = (name() == null) ? "Unknown" : name();
        if (histName.startsWith("/")) {
            histName = histName.substring(1);
            histName = AidaUtils.modifyName(histName);
            histName = "/" + histName;
        } else {
            histName = AidaUtils.modifyName(histName);
        }
            
        //System.out.println("RemoteManagedObject:: name="+histName+",  path="+treeFolder);

        if (treeFolder.endsWith("//")) treeFolder = treeFolder.substring(0, treeFolder.length()-2);
        
        if (!treeFolder.startsWith("/")) treeFolder = "/"+treeFolder;
        else if (treeFolder.startsWith("//")) treeFolder = treeFolder.substring(1);
        
        if (treeFolder.equals("/") && histName.startsWith("/")) this.treePath = histName; 
        else if (treeFolder.endsWith("/") && histName.startsWith("/")) this.treePath = treeFolder+histName.substring(1);
        else if (treeFolder.endsWith("/") || histName.startsWith("/")) this.treePath = treeFolder+histName;
        else this.treePath = treeFolder+"/"+histName; 
        
        //System.out.println("\tpath="+treePath);
    }
    
    public String getTreePath() { return treePath; }
    
    /**
     * This is the only method that throws HistogramEvents.
     */
    public void setDataValid(boolean dataIsValid) {
        remoteLogger.finest("RemoteMutableObject.setDataValid  DATA: new="+dataIsValid+", old="+this.dataIsValid+", throwEvent="+isValid+", path="+treePath);
        if (this.dataIsValid != dataIsValid) {
            this.dataIsValid = dataIsValid;
            //if (isValid && !dataIsValid) fireStateChanged();
            if (isValid) fireStateChanged();
        }
    }
    
    public boolean isDataValid() {
        //remoteLogger.finest("RemoteMutableObject.isDataValid dataIsValid="+dataIsValid);
        return dataIsValid;
    }
    
    //public void setTimeOfLastUpdate(long t) { this.timeOfLastUpdate = t; }
    //public long getTimeOfLastUpdate() { return timeOfLastUpdate; }
    
}
