package tests;

import java.io.File;
import java.io.IOException;

import oneLiners.OneLiners;

import signals.JET.*;
import descriptors.JET.*;
import mds.JetMDSFetcher;
import junit.framework.TestCase;

@SuppressWarnings("unused") //They're tests, we don't care if nothing is done with the signals
public class JetPFTests extends TestCase {
	
	String cachePath = System.getProperty("java.io.tmpdir") + "/jetSignalCache";
	//String cachePath = null;

	public void testSignalFetch(){		
		try{
			OneLiners.recursiveDelete(cachePath); //clear the cache completely
						
			JetMDSFetcher jmds = new JetMDSFetcher(cachePath);
			
			//JetMDSFetcher jmds = new JetMDSFetcher("127.0.0.1",8500,cachePath);
			jmds.getSig("/jet/70545/ppf/lidr/ne");
			
			System.out.println("Latest pulse: " + jmds.getLatestPulse());
			
			float sspr[][], t[];
			double ssprd[][], td[];
			
			//Directly fetch the data from the EFIT/SSPR ppf 
			PPF ssprPPF = (PPF) jmds.getSig( new PPFDesc(70545, "efit", "sspr", 0), true );
			sspr = (float[][])ssprPPF.get2DData();
			t = (float[])ssprPPF.getTVector();
			
			PPF iplaPPF = (PPF) jmds.getSig("/jet/65977/ppf/magn/ipla");
			float ip[] = (float[])iplaPPF.get1DData();
			t = (float[])iplaPPF.getTVector();
			
			//try writing this out as 'to-be-uploaded' ppf under UID 'test'
			PPFDesc desc = new PPFDesc(65977, "MAGN", "IPLA", "test");
			PPF ppfNew = new PPF(desc, ip, t);			
			try {
				ppfNew.writeToCache(cachePath);
			} catch (IOException e) {
				e.printStackTrace();
			}
			
			
			
			//Fetch it as double's instead
			ssprd = (double[][])ssprPPF.getDataAsType(double.class);
			td = (double[])ssprPPF.getTVectorAsType(double.class);
		
			//Fetch the vacuum field in one line
			//The tvector line will get the signal from the memory cache
			//so will be a bit slower if there are lots of signals, but not much
			// (also check multi-'/'s don't break things)
			double bvac[] = (double[]) jmds.getSig("/jet/70545///ppf/efit/////bvac").getDataAsType(double.class);
			double bvact[] = (double[]) jmds.getSig("/jet/70545/ppf/efit/bvac").getCoordsAsType(0, double.class);
			
			//Fetch from different UID in one line
			jmds.getSig("/jet/70545/ppf-mbrix/eftf/bvac").getData();
			
			
			System.out.println("68547/ke9d/ne comment: " + ((PPF)jmds.getSig("/jet/68547/ppf/ke9d/ne", true)).getComment());
			
			System.out.println("68547/kg1v/lid3 status: " + ((PPF)jmds.getSig("/jet/68547/ppf/kg1v/lid3", true)).getSystemStatusFlag());
			
			//Normal JPF test (KG4)
			int jpfdata[] = (int[]) jmds.getSig("/jet/70545/jpf/df/g4-hwp1<pos").getDataAsType(int.class);
			
			//Raw JPF test (LIDAR) - should come as byte[] and should have no tvector data
			JPF rawJPF = (JPF)jmds.getSig("/jet/70545/jpf/dc/ke3-spho3#raw");
			byte rawdata[] = (byte[]) rawJPF.getData();
			assertTrue("Check no t vector on raw JPF data", rawJPF.getTVector() == null);
			
			//TODO: Probably ought to test offline JPFs
			
			System.out.println("done");
			
			jmds.dumpMemoryCache();
			
			
		}catch(RuntimeException e){
			e.printStackTrace();
			throw e;
		}
	}
	
	public void testReloadFromCache(){
		try{
			System.out.println("Getting from cache...");
			
			//testSignalFetch should have populated the cache, so can now create a fetcher from cache only
			//But also, by default the fetcher won't start the connection unless it needs to.
			//So we can connect this is a fake server and it shouldn't ever know.
			JetMDSFetcher jmds = new JetMDSFetcher("fake.server.please.dont.connect.com", 1234, cachePath);
			
			//refetch some things
			double bvact[] = (double[]) jmds.getSig("/jet/70545/ppf/efit/bvac").getCoordsAsType(0, double.class);		
			System.out.println("68547/ke9d/ne comment: " + ((PPF)jmds.getSig("/jet/68547/ppf/ke9d/ne")).getComment());		
			System.out.println("68547/kg1v/lid3 status: " + ((PPF)jmds.getSig("/jet/68547/ppf/kg1v/lid3")).getSystemStatusFlag());
			
			jmds.dumpMemoryCache();
			
		}catch(RuntimeException e){
			e.printStackTrace();
			throw e;
		}
	}
}
