package descriptors.mast;

import descriptors.PulseSignalDesc;

public class IDAMDesc extends PulseSignalDesc {

	//for PPFs
	private String uid;
	private String signalName;	
	private int seq;
	private boolean error;
	
	public final String pathFormat = "mast/pulse/idam[-uid]/signalname[#seq][#err]";
	public final String getPathFormat(){ return pathFormat; }

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

	/** Descriptor for PPF signal */
	public IDAMDesc(int pulse, String signalName){
		this(pulse, signalName, "", 0);
	}
	
	/** Descriptor for PPF signal */
	public IDAMDesc(int pulse, String signalName, boolean errorSignal){
		this(pulse, signalName, "", 0, errorSignal);
	}

	/** Descriptor for PPF signal */
	public IDAMDesc(int pulse, String signalName, int seq){
		this(pulse, signalName, "", seq);
	}
	
	/** Descriptor for PPF signal */
	public IDAMDesc(int pulse, String signalName, String uid){
		this(pulse, signalName, uid, 0);
	}

	/** Descriptor for PPF signal */
	public IDAMDesc(int pulse, String signalName, String uid, int seq){
		this(pulse, signalName, uid, 0, false); 
	}
	
	/** Descriptor for PPF signal */
	public IDAMDesc(int pulse, String signalName, String uid, int seq, boolean error){
		this.pulse = pulse;
		this.signalName = signalName;
		this.uid = uid;		
		this.seq = seq;
		this.error = error;
	}
		
	/** Return path to signal file in disk cache. */
	final public String toString(){
		String path;
		
		path = "/mast/" + pulse 	
				+ "/idam"
				+ ((!uid.equalsIgnoreCase("")) ? ("-" + uid) : "" ) + "/"
				+ signalName
				+ ((seq!=0) ? ("#" + seq) : "")
				+ (error ? "#err" : "");
		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
			 
			 if(!parts[j].substring(0, 4).equalsIgnoreCase("mast"))
					throw new IllegalArgumentException("setting MASTDesc object on non mast signal '"+path+"'");
			 
			 do j++; while(parts[j].length()<=0); //move to next non-blank entry		
						
			//next 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, 4).equalsIgnoreCase("idam"))
				throw new IllegalArgumentException("setting MASTDesc object on non idam signal '"+path+"'");
							
			if(parts[j].length() > 5){
				if(parts[j].charAt(4) != '-')
					throw new IllegalArgumentException("Invalid path. " + pathFormat);
				uid = parts[j].substring(5);					 
			}else uid = "";
			
			
			do j++; while(parts[j].length()<=0); //move to next non-blank entry
			
			signalName = parts[j];
						
			String bits[] = signalName.split("#");
			signalName = bits[0];
			for(int i=1; i < bits.length; i++){
				if(bits[i].equalsIgnoreCase("err"))
					error=true;
				else{
					try{
						seq = Integer.parseInt(bits[1]);			
					}catch(NumberFormatException err){ seq = 0; }												
				}
			}
				
		}catch(NullPointerException err){
			throw new IllegalArgumentException("Invalid path '"+path+"', expecting: " + pathFormat);
		}
	}

	
			
	public final String getSignalName() { return signalName; }
	public final void setSignalName(String signalName) { this.signalName = signalName; }
	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; }		
	public final boolean isErrorSignal(){ return error; }
}
