package descriptors.JET;

import descriptors.PulseSignalDesc;

/** Descriptor for JET JPF data signal 
 *  
 * Holds information of where the JPF is and nothing about what it 
 * is or what it contains. Use for making requests.
 * 
 */
public class JPFDesc extends PulseSignalDesc {
	//for JPFs
	private boolean offline;
	private boolean raw;
	private String subSystem;
	private String signal;
	
	public final String pathFormat = "/{jet/jet-offline}/pulse/jpf/subsys/signal[#raw]";
	public final String getPathFormat(){ return pathFormat; }
	
	/** Descriptor for JPF signal from path */
	public JPFDesc(String path){
		setFromPath(path);
	}

	
	/** Descriptor for JPF signal */
	public JPFDesc(int pulse, String subSystem, String signal, boolean offline, boolean raw){
		this.pulse = pulse;
		this.subSystem = subSystem;
		this.signal = signal;
		this.offline = offline;
		this.raw = raw;
	}
	
	
	/** Return path to signal file in disk cache. */
	final public String toString(){
		String path;
		path =  (offline ? "/jet-offline/" : "/jet/")
				+ pulse
				+ "/jpf/"
				+ subSystem
				+ "/" + signal
				+ (raw ? "#raw" : "");
		return path.toLowerCase();		
	}
	
	/** Set descriptor from path */
	final public void setFromPath(String path){
		try{
			path = path.toLowerCase();
			
			//break path into parts
			String parts[] = path.split("\\/");
			
			int j=0;
			while(parts[j].length()<=0) j++; //move to first non-blank entry
			
			//first part must be "jet-offline" or "jet" (or maybe not)
			if( parts[j].matches("^[0-9]*$") ) { //all numbers then we have pulse straight out
				System.out.println("No machine specified, assuming JET");
			} else {
				if(parts[j].equalsIgnoreCase("jet-offline")){
					offline = true;								
				}else if(parts[j].equalsIgnoreCase("jet"))
					offline = false;
				else
					throw new IllegalArgumentException("Not a jet path, expecting: " + pathFormat);
				
				do j++; while(parts[j].length()<=0); //move to next non-blank entry
			}
			
			//first part is pulse number			
			try{
				pulse = Integer.parseInt(parts[j]);			
			}catch(NumberFormatException err){ pulse = 0; }
			if(pulse <= 0)
				throw new IllegalArgumentException("Invalid pulse number '"+parts[j]+"'. Expecting: " + pathFormat);
			
			do j++; while(parts[j].length()<=0); //move to next non-blank entry
			
			if(!parts[j].equalsIgnoreCase("jpf"))
				throw new IllegalArgumentException("setting JPFDesc object on non jpf signal '"+path+"'");
			
			do j++; while(parts[j].length()<=0); //move to next non-blank entry
			
			subSystem = parts[j];
			
			do j++; while(parts[j].length()<=0); //move to next non-blank entry
			
			signal = parts[j];
			for(j++;j<parts.length;j++)
				if(parts[j].length() > 0)
					signal += "/" + parts[j];
			
			raw = false;
			
			String bits[] = signal.split("#");
			if(bits.length > 1){
				signal = bits[0];
				raw = bits[1].equalsIgnoreCase("raw");						
			}
			
		}catch(NullPointerException err){
			throw new IllegalArgumentException("Invalid path '"+path+"', expecting: " + pathFormat);
		}
	}
	
	public final boolean isOffline() { return offline;	}
	public final void setOffline( boolean offline) { this.offline = offline; }
	public final boolean isRaw() { return raw; }
	public final void setRaw( boolean raw) { this.raw = raw; }
	public final String getSignal() { return signal; }
	public final void setSignal(String signal) { this.signal = signal; }
	public final String getSubSystem() { return subSystem; }
	public final void setSubSystem(String subSystem) { this.subSystem = subSystem; }
	
}
