/*
 * Copyright (c) 2005-2008 Substance Kirill Grouchnikov. All Rights Reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *  o Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *
 *  o Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *
 *  o Neither the name of Substance Kirill Grouchnikov nor the names of
 *    its contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package tools.docrobot;

import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.CountDownLatch;

import javax.imageio.ImageIO;
import javax.swing.*;

import org.jvnet.substance.SubstanceLookAndFeel;
import org.jvnet.substance.skin.SubstanceBusinessBlackSteelLookAndFeel;
import org.jvnet.substance.utils.SubstanceImageCreator;

/**
 * The base class for taking a single screenshot for Substance documentation.
 * 
 * @author Kirill Grouchnikov
 */
public abstract class ImageWatermarkRobot {
	/**
	 * The screenshot filename.
	 */
	protected String screenshotFilename;

	/**
	 * The frame instance.
	 */
	protected JFrame sf;

	/**
	 * Used to wait for the rendering.
	 */
	protected CountDownLatch gate;

	/**
	 * Creates the new screenshot robot.
	 * 
	 * @param screenshotFilename
	 * 		The screenshot filename.
	 */
	public ImageWatermarkRobot(String screenshotFilename) {
		this.screenshotFilename = screenshotFilename;
	}

	/**
	 * Runs the screenshot process.
	 */
	public void run() {
		long start = System.currentTimeMillis();
		this.gate = new CountDownLatch(1);
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				try {
					UIManager
							.setLookAndFeel(new SubstanceBusinessBlackSteelLookAndFeel());
				} catch (Exception exc) {
					exc.printStackTrace();
				}
				SwingUtilities.invokeLater(new Runnable() {
					public void run() {
						JFrame.setDefaultLookAndFeelDecorated(true);
						sf = new JFrame("Sample frame");
						sf.addComponentListener(new ComponentAdapter() {
							@Override
							public void componentShown(ComponentEvent e) {
								SwingUtilities.invokeLater(new Runnable() {
									public void run() {
										apply();
										sf
												.setIconImage(SubstanceImageCreator
														.getColorSchemeImage(
																null,
																new ImageIcon(
																		ImageWatermarkRobot.class
																				.getClassLoader()
																				.getResource(
																						"test/resource/image-x-generic.png")),
																SubstanceLookAndFeel
																		.getCurrentSkin(
																				sf
																						.getRootPane())
																		.getMainActiveColorScheme()));
										makeScreenshot();
										SwingUtilities
												.invokeLater(new Runnable() {
													public void run() {
														sf.dispose();
														gate.countDown();
													}
												});
									}
								});
							}
						});
						sf.setSize(238, 261);

						sf.setLayout(new FlowLayout(FlowLayout.CENTER));
						JButton defButton = new JButton("default");
						JButton disButton = new JButton("disabled");
						JButton regButton = new JButton("regular");
						disButton.setEnabled(false);
						sf.add(defButton);
						sf.add(disButton);
						sf.add(regButton);
						sf.getRootPane().setDefaultButton(defButton);

						BufferedImage iconImage = new BufferedImage(1, 1,
								BufferedImage.TYPE_INT_ARGB);
						sf.setIconImage(iconImage);
						sf.setLocationRelativeTo(null);
						sf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
						sf.setVisible(true);
					}
				});
			}
		});
		try {
			gate.await();
		} catch (InterruptedException ie) {
		}
		long end = System.currentTimeMillis();
		System.out.println(this.getClass().getSimpleName() + " : "
				+ (end - start) + "ms");
	}

	/**
	 * Applies instance-specific Substance settings before taking the
	 * screenshot.
	 */
	protected abstract void apply();

	/**
	 * Creates the screenshot and saves it on the disk.
	 */
	public void makeScreenshot() {
		BufferedImage bi = new BufferedImage(sf.getWidth(), sf.getHeight(),
				BufferedImage.TYPE_INT_ARGB);
		Graphics g = bi.getGraphics();
		sf.paint(g);
		try {
			ImageIO.write(bi, "png", new File(this.screenshotFilename));
		} catch (IOException ioe) {
			ioe.printStackTrace();
		}
	}
}
