package sim.credit;

import sim.MainWindow;
import java.awt.*;
import java.awt.event.*;
import java.io.InputStream;
import java.net.URL;
import java.util.Scanner;

public class AboutWindow extends Dialog implements ActionListener {

	public AboutWindow() {

		super(MainWindow.MAIN_WINDOW, "About");

		this.setResizable(false);
		this.setLocationRelativeTo(null);
		this.setSize(600, 500);

		//picture
		AWTImageComponent about = new AWTImageComponent();
		this.add(about, BorderLayout.WEST);

		// credit
		InputStream input = SplashWindow.class.getResourceAsStream("about.txt");
		Scanner sc = new Scanner(input);
		String str = sc.nextLine();

		TextArea textArea = new TextArea("", 80, 80, TextArea.SCROLLBARS_VERTICAL_ONLY);
		textArea.setEditable(false);
		textArea.setForeground(Color.BLACK);
		textArea.setFont(new Font("Arial", Font.PLAIN, 13));
		textArea.setSize(500, 500);

		while (sc.hasNextLine()) {
			str += sc.nextLine() + "\n";
		}

		textArea.setText(str);
		this.add(textArea, BorderLayout.CENTER);

		//OK button
		Panel p = new Panel();
		Button ok = new Button("OK");
		ok.addActionListener(this);
		p.add(ok, BorderLayout.EAST);
		this.add(p, BorderLayout.SOUTH);

		this.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				dispose();
			}
		});
	}

	public void actionPerformed(ActionEvent e) {
		dispose();
	}

	private class AWTImageComponent extends Panel {

		Image image;

		public AWTImageComponent() {
			loadImage();
		}

		public void paint(Graphics g) {
			super.paint(g);
			int w = getWidth();
			int h = getHeight();
			int imageWidth = image.getWidth(this);
			int imageHeight = image.getHeight(this);
			int x = (w - imageWidth) / 2;
			int y = (h - imageHeight) / 2;
			g.drawImage(image, x, y, this);
		}

		public Dimension getPreferredSize() {
			return new Dimension(image.getWidth(this), image.getHeight(this));
		}

		private void loadImage() {
			String fileName = "/sim/credit/About.png";
			URL url = getClass().getResource(fileName);
			Toolkit toolkit = Toolkit.getDefaultToolkit();
			image = toolkit.getImage(url);
			MediaTracker tracker = new MediaTracker(this);
			tracker.addImage(image, 0);
			try {
				tracker.waitForID(0);
			} catch (InterruptedException ie) {
				System.out.println("interrupt: " + ie.getMessage());
			}
		}
	}
}
