/**
 * File: LoginPage.java 27.05.2014, 12:33:23, author: mreinhardt
 * 
 * Project: OwnCloud
 *
 * https://www.martinreinhardt-online.de/apps
 */
package de.martinreinhardt.owncloud.webtest.pages;

import net.thucydides.core.annotations.DefaultUrl;
import net.thucydides.core.annotations.findby.FindBy;
import net.thucydides.core.reports.adaptors.xunit.model.TestError;

import org.apache.log4j.Logger;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

import de.martinreinhardt.owncloud.webtest.util.UITestConstants;

@DefaultUrl(UITestConstants.DEFAULT_URL)
public class RoundCubePage extends PortalPage {

	// Logger
	private static final Logger LOG = Logger.getLogger(RoundCubePage.class);

	/**
	 * iframe id
	 */
	private static final String ROUNDCUBE_FRAME = "roundcubeFrame";

	/**
	 * reference id of loader item
	 */
	private static final String ROUNDCUBE_LOADER = "roundcubeLoader";
	/**
	 * reference xpath to new loader
	 */
	private static final String ROUNDCUBE_LOADER_NEW = "//*[contains(@class,'loading')]";

	@FindBy(id = ROUNDCUBE_FRAME)
	private WebElement rcFrame;

	@FindBy(id = "errorMsg")
	private WebElement errorMsg;

	@FindBy(id = "rcmloginuser")
	private WebElement rcLogin;

	@FindBy(xpath = "(//tbody//td[contains(@class,'subject')]/a)[1]")
	private WebElement firstEmail;

	@FindBy(id = ROUNDCUBE_LOADER)
	private WebElement ajaxLoader;

	@FindBy(xpath = ROUNDCUBE_LOADER_NEW)
	private WebElement newAjaxLoader;

	public RoundCubePage(final WebDriver pWebDriver) {
		super(pWebDriver);
	}

	/**
	 * Waits for the ajax status loader to disappear
	 * 
	 * @throws TestError
	 */
	private void wait_for_rc_load() throws TestError {
		this.load_iFrame(ROUNDCUBE_FRAME);
		try {
			if (element(newAjaxLoader).isCurrentlyVisible()) {
				// wait for loader icon to disappear
				element(newAjaxLoader).waitUntilNotVisible();
				if (isRcLoginDisplayed()) {
					throw new TestError("Roundcube Login should not be visible!");
				}
				LOG.info("AJAX loader disappeared. Loading complete...");
			}
		} catch (final NoSuchElementException e1) {
			LOG.info("New AJAX Roundcube loader not found");
			try {
				if (element(ajaxLoader).isCurrentlyVisible()) {
					// wait for loader icon to disappear
					element(ajaxLoader).waitUntilNotVisible();
					if (isRcLoginDisplayed()) {
						throw new TestError("Roundcube Login should not be visible!");
					}
					LOG.info("AJAX loader disappeared. Loading complete...");
				}
			} catch (final NoSuchElementException e2) {
				LOG.info("AJAX Roundcube loader not found");
			}
		}
	}

	public String getFirstMessageSubject() throws TestError {
		wait_for_rc_load();
		String result = null;
		load_iFrame(ROUNDCUBE_FRAME);
		clickOn(firstEmail);
		result = firstEmail.getText();
		back_to_parent_document();
		return result;
	}

	public boolean isRcLoginDisplayed() throws TestError {
		wait_for_rc_load();
		boolean loginVisible = false;
		try {
			loginVisible = rcLogin.isDisplayed();
		} catch (final NoSuchElementException e) {
		}
		back_to_parent_document();
		return loginVisible;
	}

	public boolean isErrorMessageDisplayed() throws TestError {
		wait_for_rc_load();
		boolean errorDisplayed = false;
		try {
			errorDisplayed = errorMsg.isDisplayed();
		} catch (final NoSuchElementException e) {
		}
		back_to_parent_document();
		return errorDisplayed;
	}

	public boolean isShowingRcFrame() throws TestError {
		wait_for_rc_load();
		boolean frameLoaderd = false;
		try {
			frameLoaderd = rcFrame.isDisplayed();
		} catch (final NoSuchElementException e) {
		}
		back_to_parent_document();
		return frameLoaderd;
	}
}

@@ -1733,16 +1733,47 @@
 Error %7B%0A
+%09%09waitFor(200).milliseconds();%0A
 %09%09this.l
@@ -2226,24 +2226,27 @@
 und%22);%0A%09
+%09%7D%0A
 %09%09try %7B%0A
 %09%09%09%09if (
@@ -2237,17 +2237,16 @@
 %09%09try %7B%0A
-%09
 %09%09%09if (e
@@ -2284,25 +2284,24 @@
 isible()) %7B%0A
-%09
 %09%09%09%09// wait 
@@ -2333,17 +2333,16 @@
 ear%0A%09%09%09%09
-%09
 element(
@@ -2376,17 +2376,16 @@
 ible();%0A
-%09
 %09%09%09%09if (
@@ -2409,25 +2409,24 @@
 d()) %7B%0A%09%09%09%09%09
-%09
 throw new Te
@@ -2476,17 +2476,16 @@
 ble!%22);%0A
-%09
 %09%09%09%09%7D%0A%09%09
@@ -2486,17 +2486,16 @@
 %09%09%7D%0A%09%09%09%09
-%09
 LOG.info
@@ -2547,20 +2547,18 @@
 .%22);%0A%09%09%09
-%09
 %7D%0A
-%09
 %09%09%7D catc
@@ -2594,25 +2594,24 @@
 on e2) %7B%0A%09%09%09
-%09
 LOG.info(%22AJ
@@ -2643,21 +2643,16 @@
 ound%22);%0A
-%09%09%09%7D%0A
 %09%09%7D%0A%09%7D%0A%0A

fix loading detection