// Path: src/main/java/me/alb_i986/selenium/tinafw/ui/WebPage.java
// public interface WebPage {
// 
// 	/**
// 	 * @see Config#getBaseUrl()
// 	 */
// 	public final static String BASE_URL = Config.getBaseUrl();
// 	
// 
// 	/**
// 	 * @see WebDriver#getTitle()
// 	 */
// 	public String getTitle();
// 
// 	/**
// 	 * @see WebDriver#getCurrentUrl()
// 	 */
// 	public String getCurrentUrl();
// 
// }


import org.openqa.selenium.support.ui.ExpectedConditions;
import me.alb_i986.selenium.tinafw.ui.WebPage;
package me.alb_i986.selenium.tinafw.tasks;



/**
 * Static class providing factory methods for creating instances
 * of common {@link WebTask}'s.
 * Inspired by WebDriver's {@link ExpectedConditions}.
 * <p>
 * It includes factory methods for creating BDD-style tasks, following the 
 * <a href="http://martinfowler.com/bliki/GivenWhenThen.html">
 * Given When Then pattern</a>:
 * {@link #given(WebTask...)}, {@link #when(WebTask...)}, {@link #then(WebTask...)}.
 * They are simply {@link CompositeWebTask}'s, but feature a meaningful
 * name which helps improving the readability of tests.
 *
 */
public class WebTasks {

	/**
	 * Constant that makes {@link #nullTask()} a singleton.
	 * <p>
	 * Though such a task has a state (mainly a user), its state is never used,
	 * therefore it should be safe to make it a singleton.
	 * Tests using it concurrently should be working fine.
	 */
	private static final WebTask NULL_TASK = new BaseWebTask() {
			
			@Override
			public WebPage run(WebPage initialPage) {
				return initialPage;
			}
		};

	protected WebTasks() {
	}

	/**
	 * Task that does nothing, and "finally" returns the very initial page.
	 */
	public static WebTask nullTask() {
		return NULL_TASK;
	}
	
	/**
	 * Task that sleeps for the given number of seconds,
	 * and finally returns the very initial page.
	 */
	public static WebTask sleepFor(int seconds) {
		return new BaseWebTask() {

			@Override
			public WebPage run(WebPage initialPage) {
				try {
					Thread.sleep(seconds * 1000);
				} catch (InterruptedException e) {
					logger.warn("sleep interrupted");
				}
				return initialPage;
			}
		};
	}
	
	/**
	 * @return a {@link CompositeWebTask} made up of the components
	 */
	public static CompositeWebTask composite(WebTask... components) {
// Path: src/main/java/me/alb_i986/selenium/tinafw/ui/WebPage.java
// public interface WebPage {
// 
// 	/**
// 	 * @see Config#getBaseUrl()
// 	 */
// 	public final static String BASE_URL = Config.getBaseUrl();
// 	
// 
// 	/**
// 	 * @see WebDriver#getTitle()
// 	 */
// 	public String getTitle();
// 
// 	/**
// 	 * @see WebDriver#getCurrentUrl()
// 	 */
// 	public String getCurrentUrl();
// 
// }


// Path: src/main/java/me/alb_i986/selenium/tinafw/tasks/WebTasks.java
import org.openqa.selenium.support.ui.ExpectedConditions;
import me.alb_i986.selenium.tinafw.ui.WebPage;

package me.alb_i986.selenium.tinafw.tasks;



/**
 * Static class providing factory methods for creating instances
 * of common {@link WebTask}'s.
 * Inspired by WebDriver's {@link ExpectedConditions}.
 * <p>
 * It includes factory methods for creating BDD-style tasks, following the 
 * <a href="http://martinfowler.com/bliki/GivenWhenThen.html">
 * Given When Then pattern</a>:
 * {@link #given(WebTask...)}, {@link #when(WebTask...)}, {@link #then(WebTask...)}.
 * They are simply {@link CompositeWebTask}'s, but feature a meaningful
 * name which helps improving the readability of tests.
 *
 */
public class WebTasks {

	/**
	 * Constant that makes {@link #nullTask()} a singleton.
	 * <p>
	 * Though such a task has a state (mainly a user), its state is never used,
	 * therefore it should be safe to make it a singleton.
	 * Tests using it concurrently should be working fine.
	 */
	private static final WebTask NULL_TASK = new BaseWebTask() {
			
			@Override
			public WebPage run(WebPage initialPage) {
				return initialPage;
			}
		};

	protected WebTasks() {
	}

	/**
	 * Task that does nothing, and "finally" returns the very initial page.
	 */
	public static WebTask nullTask() {
		return NULL_TASK;
	}
	
	/**
	 * Task that sleeps for the given number of seconds,
	 * and finally returns the very initial page.
	 */
	public static WebTask sleepFor(int seconds) {
		return new BaseWebTask() {

			@Override
			public WebPage run(WebPage initialPage) {
				try {
					Thread.sleep(seconds * 1000);
				} catch (InterruptedException e) {
					logger.warn("sleep interrupted");
				}
				return initialPage;
			}
		};
	}
	
	/**
	 * @return a {@link CompositeWebTask} made up of the components
	 */
	public static CompositeWebTask composite(WebTask... components) {
		return new CompositeWebTask(components);