package org.fundacionjala.automation.scenario.steps.admin.location;

import org.fundacionjala.automation.framework.pages.admin.home.AdminPage;
import org.fundacionjala.automation.framework.pages.admin.locations.LocationPage;

import cucumber.api.java.en.When;


public class LocationWhenSteps {
	
	@When("^I add a new location with name: \"([^\"]*)\", display name \"([^\"]*)\" and description \"([^\"]*)\"$")
	public void addALocation(String name, 
			         String displayName, 
			         String description) throws Throwable { 
		
	    	goToLocationPage()
	    	.clickOnAddButton()
	    	.setNameField(name)
	    	.setDisplayNameField(displayName)
	    	.setDescriptionArea(description)
	    	.clickOnSaveButton();
	}
	
	@When("^I open locations page$")
	public void i_open_locations_page() throws Throwable {
		goToLocationPage();
	}
	
	@When("^I update location \"([^\"]*)\" with name: \"([^\"]*)\", display name \"([^\"]*)\" and description \"([^\"]*)\"$")
	public void updateLocation(String currentLocation, 
				   String name, 
				   String displayName, 
				   String description) throws Throwable {
		
		goToLocationPage()
		.doubleClickOnALocation(currentLocation)
		.setNameField(name)
		.setDisplayNameField(displayName)
		.setDescriptionArea(description)
		.clickOnSaveButton();
	}
	
	@When("^I update \"([^\"]*)\" location parent to \"([^\"]*)\"$")
	public void updateLocationParent(String displayName, 
					 String parentName) throws Throwable {
		
		goToLocationPage()
			.doubleClickOnALocation(displayName)
			.clickOnAddParentButton()
			.selectAParentLocation(parentName)
			.clickOnSaveButton();
	}
	
	@When("^I delete the location \"([^\"]*)\"$")
	public void deleteLocation(String displayName) throws Throwable {
		
		goToLocationPage()
			.clickOnALocation(displayName)
			.clickOnRemoveButton()
			.clickOnRemoveButton();
	}
	
	@When("^I add a new location with name: \"([^\"]*)\", display name \"([^\"]*)\" and parent \"([^\"]*)\"$")
	public void addChildLocation(String name, 
				     String displayName, 
				     String parentName) throws Throwable {
		
		goToLocationPage()
			.clickOnAddButton()
			.setNameField(name)
			.setDisplayNameField(displayName)
			.clickOnAddParentButton()
			.selectAParentLocation(parentName)
			.clickOnSaveButton();
	}
	
	@When("^I add a new location with name: \"([^\"]*)\", display name \"([^\"]*)\" and associated room \"([^\"]*)\"$")
	public void addAssociatedLocation(String name, 
					  String displayName, 
					  String roomName) throws Throwable {
		
		 goToLocationPage()
					.clickOnAddButton()
					.setNameField(name)
					.setDisplayNameField(displayName)
					.clickOnLocationAssociationLink()
					.setRoomName(roomName)
					.clickOnAssigned()
					.clickOnAddAvailableRoom()
					.clickOnSaveButton();
	}
	
	@When("^I associate the location \"([^\"]*)\" with a room \"([^\"]*)\"$")
	public void associateLocation(String displayName, 
				      String roomName) throws Throwable {
		
		
		goToLocationPage()
			.doubleClickOnALocation(displayName)
			.clickOnLocationAssociationLink()
			.setRoomName(roomName)
			.clickOnAssigned()
			.clickOnAddAvailableRoom()
			.clickOnSaveButton();
	}
	
	@When("^I delete the association between location \"([^\"]*)\" and \"([^\"]*)\"$")
	public void removeLocationAssociation(String displayName, 
					      String roomName) throws Throwable {
		
		LocationPage locationPage = new LocationPage();
		locationPage
		    .leftMenu
		    .clickOnConferenceRoomsButton();
		
		locationPage
			.leftMenu
			.clickOnLocationsButton()
			.doubleClickOnALocation(displayName)
			.clickOnLocationAssociationLink()
			.clickOnRemoveAssociatedRoom(roomName)
			.clickOnSaveButton();
	}
	
	private LocationPage goToLocationPage(){
		
		AdminPage adminPage = new AdminPage();
		
		LocationPage locationPage = adminPage
						.refreshPage()
						.leftMenu
	 		     		.clickOnLocationsButton();
	    
	    return locationPage;
	}
}

@@ -62,16 +62,95 @@
 ation;%0A%0A
+import org.fundacionjala.automation.framework.maps.admin.resource.ResourceMap;%0A
 import o
@@ -296,16 +296,273 @@
 ionPage;
+%0Aimport org.fundacionjala.automation.framework.pages.admin.resource.ResourcePage;%0Aimport org.fundacionjala.automation.framework.utils.common.BrowserManager;%0Aimport org.fundacionjala.automation.framework.utils.common.UIActions;%0Aimport org.openqa.selenium.By;
 %0A%0Aimport
@@ -4275,10 +4275,641 @@
 age;%0A%09%7D%0A
+%09%0A%09@When(%22%5EI select %5C%22(%5B%5E%5C%22%5D*)%5C%22 on location page size option$%22)%0A%09public LocationPage i_select_on_location_page_size_option(String quantity) throws Throwable %7B%0A%09%09LocationPage locationPage = new LocationPage();%0A%09%09locationPage%0A%09%09    .leftMenu%0A%09%09    .clickOnConferenceRoomsButton();%0A%09%09%0A%09%09locationPage%0A%09%09%09.leftMenu%0A%09%09%09.clickOnLocationsButton();%0A%09%09%0A%09%09UIActions.waitFor(%22//div%5B@id='locationGrid'%5D%22);%0A%09%09String xpath = %22//div%5B@id='locationGrid'%5D/div%5B3%5D/div/div%5B2%5D/div%5B1%5D/select//option%5Btext()='number'%5D%22%0A%09%09%09.replace(%22number%22, quantity);%0A%09%09BrowserManager.getDriver().findElement(By.xpath(xpath)).click();%0A%09%09%0A%09%09return new LocationPage();%0A%09%7D%0A
 %7D%0A

Fix Framework: LocationWhenSteps