package gov.nih.nci.caadapter.hl7.mif;

import gov.nih.nci.caadapter.ui.common.DefaultSettings;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.Set;
import java.util.Iterator;
import java.util.zip.ZipEntry;

public class MIFIndexParser {
	/**
	 * Read MIF names from MIF file directory
	 * @throws Exception
	 */
	public MIFIndex readMIFIndexInfo(String mifDirPath)throws Exception 
	{
		MIFIndex mifIndexInfos = new MIFIndex();
		File fileDir=new File(mifDirPath);
		if (fileDir.isDirectory())
		{
			File[] filesInDir=fileDir.listFiles();
			for (File oneFile:filesInDir)
				mifIndexInfos.addMessageType(oneFile.getName());
		}
		return mifIndexInfos;
	}
	/**
	 * Read MIF name from zipped MIF file directory
	 * @throws Exception
	 */
	public MIFIndex parseMIFIndexInfo() throws Exception {
		java.net.URL url= Thread.currentThread().getClass().getResource("/mif/COCT_MT150003UV03.mif");
		String urlSt=url.getFile();
		urlSt=urlSt.substring(6,urlSt.indexOf(".zip")+4);
		MIFIndex mifIndexInfos = new MIFIndex();
		try {
			java.util.zip.ZipFile zipFile=new java.util.zip.ZipFile(urlSt);
			for (Enumeration em1 = zipFile.entries(); em1.hasMoreElements();)
			{
				ZipEntry zipFileEntry=(ZipEntry)em1.nextElement();
				String zipFileName=zipFileEntry.getName();
				if(zipFileName.indexOf(".mif")>-1)
				{
					mifIndexInfos.addMessageType(zipFileName.substring(zipFileName.indexOf("/")+1));
				}					
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return mifIndexInfos;
	}

	/**
	 * Print the MIF index with message category and MIF files for each message type
	 *
	 */
	public void printMIFIndex(MIFIndex mifIndexInfos)
	{
		
		for(Object msgCat:mifIndexInfos.getMessageCategory()) {
			System.out.println("message category: "+msgCat);
			Set msgTypeSet=mifIndexInfos.fingMessageTypesWithCategory((String)msgCat);
			Iterator msgIt=msgTypeSet.iterator();
			while (msgIt.hasNext())
			{
				String msgType=(String) msgIt.next();
				System.out.println("MIFIndexParser.printMIFIndex()..MsgCat:"+msgCat+"...MsgType:"+msgType +"..MIF file:"+mifIndexInfos.findMIFFileName(msgType));
			}
		}
	}

	/**
	 * Save a MIFIndex object
	 * @param fileName
	 * @throws Exception
	 */
	public void saveMIFIndex(String fileName, MIFIndex mifIndexInfos ) throws Exception {
		OutputStream os = new FileOutputStream(fileName);
		ObjectOutputStream oos = new ObjectOutputStream(os); 
		oos.writeObject(mifIndexInfos);
		oos.close();
		os.close();
	}

	public  static MIFIndex loadMIFInfos() throws Exception {
		InputStream is = Thread.currentThread().getClass().getResourceAsStream("/mifIndexInfos");
		if (is == null)
		{
			is = Thread.currentThread().getClass().getClassLoader().getResource("/mifIndexInfos").openStream();
			if (is == null)
				throw new Exception("Loading MIF information failure : ");
		}
		ObjectInputStream ois = new ObjectInputStream(is);
		MIFIndex mifIndex = (MIFIndex)ois.readObject();
		ois.close();
		is.close();
		return mifIndex;
	}

	public static void main(String[] args) throws Exception {
		MIFIndexParser mifInfoParser = new MIFIndexParser();
//		MIFIndex mifIndexInfos= mifInfoParser.parseMIFIndexInfo();
//		MIFIndex mifIndexInfos=mifInfoParser.readMIFIndexInfo();
//		mifInfoParser.saveMIFIndex("c:/temp/mifIndexInfos",mifIndexInfos);

		MIFIndex mifIndexInfos=MIFIndexParser.loadMIFInfos();
		
		mifInfoParser.printMIFIndex(mifIndexInfos);
	}
}


@@ -582,18 +582,17 @@
 xception
- 
 %0A
+
 %09%7B%0A%09%09MIF
@@ -1604,21 +1604,16 @@
 );%0A%09%09%09%09%7D
-%09%09%09%09%09
 %0A%09%09%09%7D%0A%09%09
@@ -1882,19 +1882,17 @@
 fos)%0A%09%7B%0A
-%09%09
 %0A
+
 %09%09for(Ob
@@ -2621,17 +2621,16 @@
 eam(os);
- 
 %0A%09%09oos.w
@@ -2865,16 +2865,18 @@
 %0A%09%09%7B%0A%09%09%09
+//
 is = Thr
@@ -2936,17 +2936,105 @@
 source(%22
-/
+mifIndexInfos%22).openStream();%0A%09%09%09is = MIFIndexParser.class.getClassLoader().getResource(%22
 mifIndex
@@ -3662,18 +3662,16 @@
 nfos();%0A
-%09%09
 %0A%09%09mifIn

load MIFIndex from webstart client