/**
 * 
 */
package org.richfaces.realworld;

import org.testng.Assert;

import com.thoughtworks.selenium.DefaultSelenium;

/**
 * @author Andrey Markavtsov
 *
 */
public class RealWorldHelper {
	
		
	public static interface HtmlConstants {
		public static interface LoginPage {
			String usernameId = "loginPanelForm:username";
			String passwordId = "loginPanelForm:password";
			String loginButtonPath= "//td[@class='login-table-col']/input[1]";
			String loginLinkId = "j_id27:loginPanelShowlink";
			
			String LOGIN_NAME = "amarkhel";
			String LOGIN_PASSWORD = "12345";
		};
		
		public static interface IndexPage {
			String loggedUserPath = "//a[@class='logged-user']";
			String shelfCountPath = "//table[@id='menuform:menu']/tbody/tr/td[2]";
			int SHELF_COUNT = 2;
			String ALBUM_PATH = "//div[@class='preview_box_album_120']/table/tbody/tr/td/a";
			String ALBUM_NAME_PATH = "//div[@class='preview_box_album_120']/div[@class='album_name']";

		}
		
		public static interface AlbumArea {
			String ALBUM_HEADER_NAME = "//table[@class='album-header-table']/tbody/tr/td/h1";
			String IMAGES_CONTAINER = "//span[@id='mainform:userAlbumImages']";
		}
		
		public static interface TreeArea {
			
		}
	};

	
	public static void Login(DefaultSelenium selenium) {
		selenium.click("id=" + HtmlConstants.LoginPage.loginLinkId);
		try {
			Thread.sleep(5000);
		}catch (Exception e) {
			Assert.fail("Test failed caused by: " + e);
		}
		
		AssertVisible(selenium, HtmlConstants.LoginPage.usernameId, "Input for username in not visible");
		AssertVisible(selenium, HtmlConstants.LoginPage.passwordId, "Input for password in not visible");
		
		String type = selenium.getAttribute("//*[@id='"+HtmlConstants.LoginPage.passwordId+"']/@type");
		if (!"password".equals(type)) {
			Assert.fail("Password input should be of 'password' type");
		}
		
		setValueById(selenium, HtmlConstants.LoginPage.usernameId, HtmlConstants.LoginPage.LOGIN_NAME);
		setValueById(selenium, HtmlConstants.LoginPage.passwordId, HtmlConstants.LoginPage.LOGIN_PASSWORD);
		
		selenium.click(HtmlConstants.LoginPage.loginButtonPath);
		selenium.waitForPageToLoad("10000");
		
		String loggedText = selenium.getText(HtmlConstants.IndexPage.loggedUserPath);
		if (!loggedText.contains(HtmlConstants.LoginPage.LOGIN_NAME)) {
			Assert.fail("Authentication was not succesfull. Logged user text should contain typed login name");
		}
		
		String shelfCount = selenium.getText(HtmlConstants.IndexPage.shelfCountPath);
		if (!String.valueOf(HtmlConstants.IndexPage.SHELF_COUNT).equals(shelfCount)) {
			Assert.fail("Shelf count for user [" + HtmlConstants.LoginPage.LOGIN_NAME + "] should be "  + HtmlConstants.IndexPage.SHELF_COUNT + ".But was " + shelfCount);
		}
		
	}
	
	public static void AssertVisible(DefaultSelenium selenium, String id, String message) {
	        Assert.assertTrue(selenium.isVisible("id=" + id), message);
	}
	
	public static void setValueById(DefaultSelenium selenium, String id, String value) {
		selenium.getEval(String.format("selenium.browserbot.getCurrentWindow().document.getElementById('%1$s').value='%2$s';", id, value));
	}

}
