/**
 * Created by artemr on 11/25/2016.
 */
package by.stqa.pft.addressbook.appmanager;

import org.openqa.selenium.By;
import org.openqa.selenium.NoAlertPresentException;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;

import java.util.Iterator;
import java.util.List;

public class HelperBase {
  private FirefoxDriver wd;

  public HelperBase(FirefoxDriver wd) {
    this.wd = wd;
  }

  public void type(By locator, String text) {
    wd.findElement(locator).click();
    wd.findElement(locator).clear();
    wd.findElement(locator).sendKeys(text);
  }

  public void select(By locator, String text) {
    Select select = new Select(wd.findElement(locator));
    // TODO add check on option persistence
    select.selectByVisibleText(text);
  }

  public void click(By locator) {
    wd.findElement(locator).click();
  }

  public String[] getTitles(){
    List<WebElement> elements = wd.findElements(By.name("selected[]"));
    String[] out = new String[elements.size()];
    int i = 0;
    for (WebElement item : elements) {
      out[i] = item.getAttribute("title");
      ++i;
    }
    return out;
  }

  public boolean isAlertPresent() {
    try {
      wd.switchTo().alert();
      return true;
    } catch (NoAlertPresentException e) {
      return false;
    }
  }

  public void closeAlert(boolean confirm) {
    if(confirm){
      wd.switchTo().alert().accept();
    }else {
      wd.switchTo().alert().dismiss();
    }
  }

  public String[] getTableRowsText(By tableLocator){
    WebElement table = wd.findElement(tableLocator);
    List<WebElement> rows = table.findElements(By.name("entry"));
    String[] out = new String[rows.size()];
    int i = 0;
    for(WebElement row: rows){
      out[i] = row.getText();
      ++i;
    }
    return out;
  }

  public WebElement getTableRowByText(By tableLocator, String text){
    WebElement table = wd.findElement(tableLocator);
    for(WebElement row: table.findElements(By.name("entry"))){
      // think on better condition here
      if(row.getText().contains(text)){
        return row;
      }
    }
    return table;
  }

  public void clickTableElement(By tableLocator, String text, By elementLocator){
    WebElement row = getTableRowByText(tableLocator, text);
    row.findElement(elementLocator).click();
  }
}

@@ -936,16 +936,17 @@
 Titles()
+ 
 %7B%0A    Li
@@ -1411,16 +1411,17 @@
 %7B%0A    if
+ 
 (confirm
@@ -1421,16 +1421,17 @@
 confirm)
+ 
 %7B%0A      
@@ -1467,16 +1467,17 @@
 );%0A    %7D
+ 
 else %7B%0A 
@@ -1568,32 +1568,33 @@
 By tableLocator)
+ 
 %7B%0A    WebElement
@@ -1738,32 +1738,37 @@
 ws.size()%5D;%0A    
+for (
 int i = 0;%0A    f
@@ -1765,38 +1765,31 @@
 = 0;
-%0A    for(WebElement row: rows)
+ i %3C rows.size(); i++) 
 %7B%0A  
@@ -1804,16 +1804,24 @@
 i%5D = row
+s.get(i)
 .getText
@@ -1875,35 +1875,29 @@
 lic 
-WebElement getTableRowByTex
+void clickTableElemen
 t(By
@@ -1922,17 +1922,37 @@
 ing text
-)
+, By elementLocator) 
 %7B%0A    We
@@ -2006,12 +2006,13 @@
     
-for(
+List%3C
 WebE
@@ -2021,13 +2021,16 @@
 ment
+%3E
  row
-:
+s =
  tab
@@ -2062,18 +2062,97 @@
 entry%22))
-)%7B
+;%0A    for (int i = 0; i %3C rows.size(); i++) %7B%0A      WebElement row = rows.get(i);
 %0A      /
@@ -2192,16 +2192,17 @@
       if
+ 
 (row.get
@@ -2223,16 +2223,17 @@
 s(text))
+ 
 %7B%0A      
@@ -2239,203 +2239,8 @@
    r
-eturn row;%0A      %7D%0A    %7D%0A    return table;%0A  %7D%0A%0A  public void clickTableElement(By tableLocator, String text, By elementLocator)%7B%0A    WebElement row = getTableRowByText(tableLocator, text);%0A    r
 ow.f
@@ -2270,19 +2270,48 @@
 or).click();
+%0A        break;%0A      %7D%0A    %7D
 %0A  %7D%0A%7D%0A

replace foreach with for