package com.codeski.nbt;

import java.awt.GraphicsEnvironment;
import java.io.Console;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.lang.Exception;

import javax.swing.JFileChooser;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.UIManager;

import com.codeski.nbt.tags.NBTCompound;

/**
 * Class allows for GUI and CLI interaction for converting files from NBT to NBT, JSON, or XML.
 */
public class Main
{
	public static void main(String[] args) throws FileNotFoundException, IOException
	{
		try{
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		}
		catch(Exception e){
			e.printStackTrace();
		}
		
		Console console = System.console();
		if (console != null)
			if (args.length == 2) {
				// Read in a user specified file
				File in = new File(args[0]);
				System.out.println("Reading " + in.getCanonicalPath() + "...");
				NBTCompound root = NBTReader.read(in);
				// Write the file to NBT, JSON, or XML
				File out = new File(args[1]);
				String extension = args[1].substring(args[1].lastIndexOf('.') + 1);
				System.out.println("Writing " + out.getCanonicalPath() + "...");
				if (extension.equalsIgnoreCase("dat"))
					NBTWriter.writeNBT(root, out);
				else if (extension.equalsIgnoreCase("json"))
					NBTWriter.writeJSON(root, out);
				else if (extension.equalsIgnoreCase("xml"))
					NBTWriter.writeXML(root, out);
				else {
					System.out.println("File extension was not dat, json, or xml. Writing NBT by default.");
					NBTWriter.writeNBT(root, out);
				}
			} else
				System.out.println("Usage: java -jar " + new File(Main.class.getProtectionDomain().getCodeSource().getLocation().getPath()).getName() + " <file-to-read> <file-to-write>");
		else if (!GraphicsEnvironment.isHeadless()) {
			JFileChooser fc = new JFileChooser();
			FileFilter nbt = new FileNameExtensionFilter("Named Binary Tag (.dat)", "dat");
			fc.addChoosableFileFilter(nbt);
			// Show an open file dialog
			int ret = fc.showOpenDialog(null);
			if (ret == JFileChooser.APPROVE_OPTION) {
				File in = fc.getSelectedFile();
				NBTCompound root = NBTReader.read(in);
				FileFilter json = new FileNameExtensionFilter("JavaScript Object Notation (.json)", "json");
				fc.addChoosableFileFilter(json);
				FileFilter xml = new FileNameExtensionFilter("Extensible Markup Language (.xml)", "xml");
				fc.addChoosableFileFilter(xml);
				fc.setFileFilter(fc.getAcceptAllFileFilter());
				// Show a save file dialog
				ret = fc.showSaveDialog(null);
				if (ret == JFileChooser.APPROVE_OPTION) {
					File out = fc.getSelectedFile();
					if (fc.getFileFilter().equals(nbt))
						NBTWriter.writeNBT(root, out);
					else if (fc.getFileFilter().equals(json))
						NBTWriter.writeJSON(root, out);
					else if (fc.getFileFilter().equals(xml))
						NBTWriter.writeXML(root, out);
					else {
						String ext = out.getName().substring(out.getName().lastIndexOf('.') + 1, out.getName().length());
						if (ext.equalsIgnoreCase("dat"))
							NBTWriter.writeNBT(root, out);
						else if (ext.equalsIgnoreCase("json"))
							NBTWriter.writeJSON(root, out);
						else if (ext.equalsIgnoreCase("xml"))
							NBTWriter.writeXML(root, out);
						else
							NBTWriter.writeNBT(root, out); // Default case is to write NBT data
					}
				}
			}
		}
	}
}

@@ -170,36 +170,8 @@
 ion;
-%0Aimport java.lang.Exception;
 %0A%0Aim
@@ -329,16 +329,68 @@
 Manager;
+%0Aimport javax.swing.UnsupportedLookAndFeelException;
 %0A%0Aimport
@@ -635,147 +635,11 @@
 ion%0A
+
 %09%7B%0A
-%09%09try%7B%0A%09%09%09UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());%0A%09%09%7D%0A%09%09catch(Exception e)%7B%0A%09%09%09e.printStackTrace();%0A%09%09%7D%0A%09%09%0A
 %09%09Co
@@ -1751,16 +1751,252 @@
 ss()) %7B%0A
+%09%09%09try %7B%0A%09%09%09%09UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());%0A%09%09%09%7D catch (ClassNotFoundException %7C InstantiationException %7C IllegalAccessException %7C UnsupportedLookAndFeelException e) %7B%0A%09%09%09%09e.printStackTrace();%0A%09%09%09%7D%0A
 %09%09%09JFile

Move UI look and feel to graphics startup block