package utils;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Vector;

import base.SignalFetcher;

import oneLiners.AsciiMatrixFile;

import mds.*;
import signals.*;


/** Fetches a single signal or list of signals for a single pulse or list of pulses.
 * The signal is dropped to disk cache and some basic information is displayed.  
 */
public class MdsFetch {

	public static void main(String args[]) {
		String cachePath = null;
		int pulses[] = null;
		String signals[] = null;		
		SignalFetcher mdsf;
		
		// LOCAL DEBUG ONLY
		 /* args = new String[]{ "jet", "/work/data-hdf/lists/pulses/C25-with-ke9d-ppf.txt",
							 "/work/data-hdf/lists/sigs/ke9dppf.txt", 
							 "/work/data-hdf" };
			 args = new String[]{ "mast", "13500",
			 "idam/ip", 
			 "/work/data-hdf" };
		 // */
		
		if(args.length < 4){
			System.out.println("Usage: machine pulse/pulseListFile signalPath/signalListFile cacheDir [mdsServer mdsPort]");
			return;
		}
		
		cachePath = args[3];
		String machine = args[0];
		
		if(machine.equalsIgnoreCase("jet")){
			if(args.length >= 6) //if present, use the mdsPlus setup
				mdsf = new JetMDSFetcher(args[4],Integer.parseInt(args[5]),cachePath);
			else //otherwise use this default:
				mdsf = new JetMDSFetcher(cachePath);
		}else{
			if(args.length >= 6) //if present, use the mdsPlus setup
				mdsf = new MastMDSFetcher(args[4],Integer.parseInt(args[5]),cachePath);
			else //otherwise use this default:
				mdsf = new MastMDSFetcher(cachePath);
		}
		
		//attempt to read the pulse argument as a file
		try{
			double tmp[][] = AsciiMatrixFile.load(args[1],true);
			pulses = new int[tmp[0].length];
			for(int i=0;i<tmp[0].length;i++)
				pulses[i] = (int)tmp[0][i];
		}catch(IOException err){ //not a file, try as single pulse number
			try{
				pulses = new int[]{ Integer.parseInt(args[1]) };			
			}catch(NumberFormatException err2){ System.err.println("arg0 doesnt seem to be a file or a number"); return; }
		}
		
		//attempt to read the signal argument as a file
		try{
			signals = readSignalList(args[2]);
		}catch(IOException err){ //not a file, use a signal signal line
			signals = new String[]{ args[2] };
		}
		
		getSignals(mdsf, machine, pulses, signals);
		
	}
	
	public static void getSignals(SignalFetcher mdsf, String machine, int pulses[], String signals[]){
		
		for(int p=0;p<pulses.length;p++){			
			String offlineStr = "";			
			for(int i=0;i<signals.length;i++){
				String signal = new String(signals[i]);
				
				if(!signal.startsWith("/"))
					signal = "/" + signal;
				try{			
					Signal s = mdsf.getSig(machine + "/" + pulses[p] + signal);
					System.out.println("Retreived signal: " + s);
					mdsf.freeFromMemory(s);
					System.gc();
				}catch(RuntimeException err){
					System.err.println(err.getMessage());
					err.printStackTrace();
				}
			}
		}
		
	}
	
	public static String[] readSignalList(String fileName) throws IOException{
		Vector<String> sigs = new Vector<String>();

		FileInputStream f = new FileInputStream(fileName);
		BufferedReader reader = new BufferedReader( new InputStreamReader(f));
		String line;

		do{
			do{
				line = reader.readLine();				
				if(line == null)break;
				line = line.trim();
			}
			while(line.length() == 0 || (line.charAt(0) == '/' && line.charAt(1) == '/'));
			if(line == null)break;

			sigs.add(line);
		}while(true);

		String signals[] = new String[sigs.size()];
		for(int i=0;i<sigs.size();i++)
			signals[i] = sigs.elementAt(i);

		return signals;
	}

}
