package j3deditor.bin.util;

import java.awt.BorderLayout;

import java.awt.Dimension;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;


/**
 * Tool for visualizing messages
 * on <code>javax.swing.JOptionPane</code>.
 *
 * @author Risto Seene
 */
public class Messaging{


	/**
	 * Creates an instance of <code>Messaging</code>.
	 */
	public Messaging(){
	}


	/**
	 * Returns JPanel with JLabel for Throwable's message
	 * and JTextarea for stack trace if stack trace is enabled,
	 * otherwise Throwable's String representation will be returned.
	 * 
	 * @param t
	 * 	Throwable which information will be returned
	 * @return Returnes Throwable's information on JPanel or as String
	 */
	private Object getContent(Throwable t){
		boolean stacktrace = true;
		try{
			stacktrace = Utilities.userdata.getBooleanData("stacktrace");
		} catch(Throwable t1){
		}

		if(stacktrace){
			JTextArea stackTxt = new JTextArea();

			t.printStackTrace();
			StackTraceElement[] stack = t.getStackTrace();
			for(int i = 0; i < stack.length; i++){
				stackTxt.append(stack[i].toString());
				if(i < stack.length - 1){
					stackTxt.append("\n");
				}
			}

			JPanel contentPanel = new JPanel();
			contentPanel.setLayout(new BorderLayout());
			JLabel txt = new JLabel(t.toString());
			contentPanel.add(txt, BorderLayout.NORTH);
			contentPanel.add(new JScrollPane(stackTxt), BorderLayout.CENTER);
			Dimension txtDim = txt.getPreferredSize();
			if(txtDim.getWidth() < 500){
				txtDim.setSize(500, txtDim.getHeight());
			}
			contentPanel.setPreferredSize(new Dimension((int)(txtDim.getWidth()), (int)(txtDim.getHeight()) + 80));

			return contentPanel;
		}
		return t.toString();
	}


	/**
	 * Shows Throwable's detail message.
	 *
	 * @param t
	 *	Throwable which message will be shown
	 */
	public void throwError(Throwable t){
		if(t instanceof OutOfMemoryError){
			this.throwError("Out of memory error!\nApplication may have become unstable.");
		}
		else {
			throwError(getContent(t));
		}
	}


	/**
	 * Shows object on error dialog.
	 *
	 * @param message
	 *	input message
	 */
	public void throwError(Object message){
		String title = "Error";
		try{
			title = Utilities.translator.translate(getClass(), "error");
		} catch(Throwable t){
		}
		JOptionPane.showMessageDialog(null, message, title, JOptionPane.ERROR_MESSAGE);
	}


	/**
	 * Shows Throwable's detail message
	 * and quits the application.
	 *
	 * @param t
	 *	Throwable which message will be shown
	 */
	public void throwFatalError(Throwable t){
	          if(t instanceof OutOfMemoryError){
	                    this.throwError("Out of memory error!\nApplication may have become unstable.");
	          }
	          else {
			throwFatalError(getContent(t));
		}
	}


	/**
	 * Shows object on error dialog
	 * and quits the application.
	 *
	 * @param message
	 *	input message
	 */
	public void throwFatalError(Object message){
		String title = "Fatal Error";
		try{
			title = Utilities.translator.translate(getClass(), "fatalerror");
		} catch(Throwable t){
		}

		if(message instanceof String){
			message = message + "\n Terminating program!";
		}
		JOptionPane.showMessageDialog(null, message, title, JOptionPane.ERROR_MESSAGE);
		System.exit(0);
	}


	/**
	 * Shows object on warning dialog.
	 *
	 * @param message
	 *	input message
	 */
	public void throwWarning(Object message){
		String title = "Warning";
		try{
			title = Utilities.translator.translate(getClass(), "warning");
		} catch(Throwable t){
		}
		JOptionPane.showMessageDialog(null, message, title, JOptionPane.WARNING_MESSAGE);
	}


	/**
	 * Shows Throwable's detail message.
	 *
	 * @param t
	 *	Throwable which message will be shown
	 */
	public void throwWarning(Throwable t){
	          if(t instanceof OutOfMemoryError){
	                    this.throwError("Out of memory error!\nApplication may have become unstable.");
	          }
	          else {
			throwWarning(getContent(t));
		}
	}


	/**
	 * Shows object on message dialog.
	 *
	 * @param message
	 *	input message
	 */
	public void throwInfo(Object message){
		String title = "Info";
		try{
			title = Utilities.translator.translate(getClass(), "info");
		} catch(Throwable t){
		}
		JOptionPane.showMessageDialog(null, message, title, JOptionPane.INFORMATION_MESSAGE);
	}


	/**
	 * Shows object on question dialog
	 * and returns an integer representing user interaction.
	 *
	 * @param message
	 *	input message
	 * @return Returns an integer representing user interaction:
	 *	<ul><li><code>JOptionPane.YES_OPTION</code></li>
	 *	<li><code>JOptionPane.NO_OPTION</code></li>
	 *	<li><code>JOptionPane.CANCEL_OPTION</code></li>
	 *	<li><code>JOptionPane.OK_OPTION</code></li>
	 *	<li><code>JOptionPane.CLOSED_OPTION</code></li></ul>
	 */
	public int throwQuestion(Object message){
		String title = "J3De";
		int option = JOptionPane.showOptionDialog(null, message, title, JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, null, null);

		return option;
	}

}
