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 PPFDesc extends PulseSignalDesc {
	//for PPFs
	private String uid;
	private String dda;
	private String dtype;
	private int seq;
	
	public final String pathFormat = "/pulse/ppf[-uid]/dda/dtype[#seq]";
	public final String getPathFormat(){ return pathFormat; }

	/** Descriptor for PPF signal from path */
	public PPFDesc(String path){
		setFromPath(path);
	}

	/** Descriptor for PPF signal */
	public PPFDesc(int pulse, String dda, String dtype){
		this(pulse, dda, dtype, "", 0);
	}
	
	/** Descriptor for PPF signal */
	public PPFDesc(int pulse, String dda, String dtype, int seq){
		this(pulse, dda, dtype, "", seq);
	}
	
	/** Descriptor for PPF signal */
	public PPFDesc(int pulse, String dda, String dtype, String uid){
		this(pulse, dda, dtype, uid, 0);
	}
	
	/** Descriptor for PPF signal */
	public PPFDesc(int pulse, String dda, String dtype, String uid, int seq){
		this.pulse = pulse;
		this.dda = dda;
		this.uid = uid;
		this.dtype = dtype;
		this.seq = seq; 
	}
		
	/** Return path to signal file in disk cache. */
	final public String toString(){
		String path;
		
		path = "/jet/" + pulse
				+ "/ppf" 
				+ ((!uid.equalsIgnoreCase("") && !uid.equalsIgnoreCase("JETPPF")) ? ("-" + uid) : "" ) + "/"
				+ dda + "/" 
				+ dtype
				+ ((seq!=0) ? ("#" + seq) : "");
		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
			 
			//assume JET signal if pulse comes first
			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"))
					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]+"'. " + pathFormat);
			
			do j++; while(parts[j].length()<=0); //move to next non-blank entry
			
			if(!parts[j].substring(0, 3).equalsIgnoreCase("ppf"))
				throw new IllegalArgumentException("setting PPFDesc object on non ppf signal '"+path+"'");
							
			if(parts[j].length() > 4){
				if(parts[j].charAt(3) != '-')
					throw new IllegalArgumentException("Invalid path. " + pathFormat);
				uid = parts[j].substring(4);
				if(uid.equalsIgnoreCase("JETPPF"))
					uid = "";							 
			}else uid = "";
			
			do j++; while(parts[j].length()<=0); //move to next non-blank entry
			
			dda = parts[j];
			
			do j++; while(parts[j].length()<=0); //move to next non-blank entry
			
			dtype = parts[j];
						
			String bits[] = dtype.split("#");
			if(bits.length > 1){
				dtype = bits[0];
				try{
					seq = Integer.parseInt(bits[1]);			
				}catch(NumberFormatException err){ seq = 0; }												
			}
				
		}catch(NullPointerException err){
			throw new IllegalArgumentException("Invalid path '"+path+"', expecting: " + pathFormat);
		}
	}

	
	/** Returns the dtype in upper-case, truncated to or padded with space to 4 characters */
	public final String getDtypeFourCharUCase() {
		char str[] = new char[4];
		for(int i=0;i<4;i++)
			str[i] = (dtype.length() > i) ? Character.toUpperCase(dtype.charAt(i)) : ' ';
		return new String(str);
	}
	
	/** Returns the DDA in upper-case, truncated to or padded with space to 4 characters */
	public final String getDdaFourCharUCase() { 
		char str[] = new char[4];
		for(int i=0;i<4;i++)
			str[i] = (dda.length() > i) ? Character.toUpperCase(dda.charAt(i)) : ' ';
		return new String(str);
	}
	
	public final String getDda() { return dda; }
	public final void setDda(String dda) { this.dda = dda; }
	public final String getDtype() { return dtype; }
	public final void setDtype(String dtype) { this.dtype = dtype; }
	public final int getSeq() { return seq; }
	public final void setSeq(int seq) { this.seq = seq; }
	public final String getUid() { return uid; }
	public final void setUid(String uid) { this.uid = uid; }	

}
