package de.skyrix.zsp.gui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.Timer;
import javax.swing.UIManager;

public final class ProgressDialog extends JDialog {
	/**Needed to start out of a thread.*/
	private ProgressDialog dlg = null;

	private JLabel info = null;

	/**The progress bar.*/
	private JProgressBar progressBar;

	static int blah = 0;

	/**Creates a ProgressDialog instance.
	 * 
	 * @param aParent - the parent of this dialog
	 * @param title   - the title
	 * @param message - the message
	 */
	public ProgressDialog(JDialog aParent, String title, String message) {
		super(aParent, title, true);

		getContentPane().setLayout(new BorderLayout());

		JPanel messagePanel = new JPanel(new FlowLayout(FlowLayout.LEFT));

		messagePanel.add(info = new JLabel(message));

		JPanel progressPanel = new JPanel(new FlowLayout(), false);
		progressPanel.add(progressBar = new JProgressBar());

		progressBar.setStringPainted(false);
		progressBar.setPreferredSize(new Dimension(400, 25));
		progressBar.setIndeterminate(true);

		JPanel buttonPanel1 = new JPanel(new FlowLayout());

		JButton hideButton = new JButton("Hide");
		buttonPanel1.add(hideButton, BorderLayout.WEST);

		JPanel buttonPanel = new JPanel(new BorderLayout(10, 10));
		buttonPanel.add(buttonPanel1, BorderLayout.EAST);

		JPanel main = new JPanel(new BorderLayout(30, 30), false);
		main.add(messagePanel, BorderLayout.NORTH);
		main.add(progressPanel, BorderLayout.CENTER);
		main.add(buttonPanel, BorderLayout.SOUTH);

		getContentPane().add(main, BorderLayout.CENTER);

		dlg = this;
		//setSize(500, 200);
		pack();
		setResizable(false);

		Thread t2 = new Thread(new Starter());
		t2.setPriority(Thread.MIN_PRIORITY);
		t2.start();
	}

	public void setTitle(String title) {
		super.setTitle(title);
	}

	public void setProgress(int value) {
		if (value < 0)
			return;

		progressBar.setIndeterminate(false);
		progressBar.setValue((value > 100) ? 100 : value);
	}

	public void setDescription(String description) {
		if (description == null)
			return;

		if (!description.trim().equals("")) {
			description = convertString(description);
			info.setText(description);
			pack();
		}
	}

	public final int getMaxCharactersPerLineCount() {
		return 60;
	}

	private String convertString(String text) {
		String str = (String) text;
		StringBuffer strBuffer = new StringBuffer();

		//Length of the message
		int length = str.length();
		int index = 0;
		int pos = 0;

		//line length
		int lineLength = getMaxCharactersPerLineCount();

		while (pos + lineLength < length) {
			String dummy = str.substring(pos, pos + lineLength);
			index = dummy.lastIndexOf(" ");

			if (index > -1) {
				strBuffer.append(dummy.substring(0, index));
				strBuffer.append("<br>");
				pos += index;
			}

			else {
				index = str.substring(pos, str.length()).indexOf(" ");
				int nextPos = index + pos;

				if (index < 0)
					strBuffer.append(str.substring(pos, str.length()));

				else {
					dummy = str.substring(pos, nextPos);

					strBuffer.append(dummy);
					strBuffer.append("<br>");

					pos = nextPos;
				}
			}

			pos += 1;
		}

		strBuffer.append(str.substring(pos, str.length()));

		String newText = strBuffer.toString();
		return newText;
	}

	public void stop() {
		dlg.dispose();
	}

	/**Thread to start the dialog.
	 */
	class Starter implements Runnable {
		public void run() {
			if (dlg != null) {
				dlg.setVisible(true);
			}
		}
	}

	public static void main(String[] args) {
		try {
			System.out.println(UIManager.getInstalledLookAndFeels()[0].getName());

			UIManager.setLookAndFeel("com.sun.java.swing.plaf.gtk.GTKLookAndFeel");
			//UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
			final ProgressDialog progressDialog =
				new ProgressDialog(
					new JDialog(),
					"Updateing...",
					"Please wait until updated.");

			progressDialog.show();

			try {
				Thread.sleep(5000);
			}
			catch (Exception e) {
			}

			Timer timer = new Timer(500, new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					progressDialog.setProgress(blah += 5);
				}
			});
			timer.start();

		}
		catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
