package org.fundacionjala.automation.framework.pages.tablet.settings;

import java.util.List;

import org.fundacionjala.automation.framework.maps.tablet.settings.NavigationMap;
import org.fundacionjala.automation.framework.utils.common.BrowserManager;
import org.fundacionjala.automation.framework.utils.common.ExplicitWait;
import org.fundacionjala.automation.framework.utils.common.LogManager;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class NavigationPage extends SettingsPage {

    @FindBy(xpath = NavigationMap.DEFAULT_ROOM_TOGGLE_BUTTON)
    WebElement defaultRoomToggleButton;
    @FindBy(xpath = NavigationMap.SAVE_BUTTON)
    WebElement saveButton;
    @FindBy(xpath = NavigationMap.ROOMS_LIST)
    WebElement roomsList;

    public NavigationPage() {
	PageFactory.initElements(BrowserManager.getDriver(), this);
    }

    public NavigationPage clickOnRoomToggleButton() {
	(new WebDriverWait(BrowserManager.getDriver(), 30))
		.until(ExpectedConditions
			.elementToBeClickable(defaultRoomToggleButton));
	defaultRoomToggleButton.click();

	LogManager.info("Room Toggle Button has been clicked");

	return this;
    }

    public NavigationPage clickOnSaveButton() {
	(new WebDriverWait(BrowserManager.getDriver(), 30))
		.until(ExpectedConditions.elementToBeClickable(saveButton));
	saveButton.click();

	LogManager.info("Save Button has been clicked");

	return this;
    }

    public NavigationPage selectConferenceRoom(String roomName) {
	WebElement room = getConferenceRoom(roomName);
	room.click();

	return this;
    }

    /**
     * This function is for getting a room by name
     * @param name
     * @return one Room if it is found else null
     */
    private WebElement getConferenceRoom(String name) {
	ExplicitWait.getWhenVisible(By.xpath(NavigationMap.ROOMS_LIST), 60);
	List<WebElement> rooms = roomsList.findElements(By
		.xpath(NavigationMap.ROOMS_LIST_ELEMENT));
	for (WebElement room : rooms) {
	    String roomName = room.findElement(
		    By.xpath(NavigationMap.ROOM_NAME)).getText();
	    if (roomName.equals(name)) {
		return room;
	    }
	}
	return null;
    }

    /**
     * This function is to verify if a room exist in room list on Navigate page.
     * @param roomName
     * @return true if any room is found else false
     */
    public boolean verifyIfExistRoomInList(String roomName) {
	return getConferenceRoom(roomName) != null ? true : false;
    }
}

@@ -87,17 +87,16 @@
 l.List;%0A
-%0A
 import o
@@ -169,16 +169,173 @@
 ionMap;%0A
+import org.fundacionjala.automation.framework.utils.api.managers.RoomAPIManager;%0Aimport org.fundacionjala.automation.framework.utils.api.objects.admin.Room;%0A
 import o
@@ -537,32 +537,134 @@
 mon.LogManager;%0A
+import org.fundacionjala.automation.framework.utils.common.PropertiesReader;%0Aimport org.junit.Assert;%0A
 import org.openq
@@ -920,16 +920,17 @@
 rWait;%0A%0A
+%0A
 public c
@@ -1220,16 +1220,92 @@
 omsList;
+%0A    @FindBy(xpath = NavigationMap.SEARCH_FIELD)%0A    WebElement searchField;
 %0A%0A    pu
@@ -2904,16 +2904,16 @@
 Name) %7B%0A
-
 %09return 
@@ -2970,10 +2970,1308 @@
 ;%0A    %7D%0A
+    %0A    /**%0A     * This function is to verify that all rooms are in the list on Navigate page.%0A     * @return true if the size of the list on Navigate page is equals to the corresponding size(API)%0A     */%0A    public boolean verifyIfRoomsExist() throws Throwable %7B%0A%09boolean verification = false;%0A%09new RoomAPIManager();%0A%09List%3CRoom%3E rooms = RoomAPIManager.getRequest(PropertiesReader.getServiceURL()+%22/rooms%22);%0A%09List%3CWebElement%3E roomsTable = roomsList.findElements(By.xpath(NavigationMap.ROOMS_LIST_ELEMENT));%0A%09if(rooms.size() == roomsTable.size())%7B%0A%09    verification = true;%0A%09%7D%0A%09return verification;%0A    %7D%0A    %0A    public NavigationPage insertFilterSearch(String filter) %7B%0A%09searchField.clear();%0A%09searchField.sendKeys(filter);%0A%09return this;%0A    %7D%0A    %0A    public void verifyIfRoomsExistAccordingFilter(String filter) throws Throwable %7B%0A%09int size = 0;%0A%09new RoomAPIManager();%0A%09List%3CString%3E rooms = RoomAPIManager.getRoomsByCriteria(filter);%0A%09List%3CWebElement%3E roomsSearch = roomsList.findElements(By.xpath(NavigationMap.ROOMS_LIST_ELEMENT));%0A%09for (WebElement webElement : roomsSearch) %7B%0A%09    if(rooms.contains(webElement.getText().trim()))%7B%0A%09%09size++;%0A%09    %7D%0A%09%7D%0A%09Assert.assertEquals(%0A%09%09%22Quantity of rooms on rooms table is not the same than rooms that match with a citeria%22,%0A%09%09rooms.size(), size);%0A    %7D%0A
 %7D%0A

Fix Framework: NavigationPage