package white.box.tester.appmanager;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import white.box.tester.model.UserData;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Max on 9/18/2016.
 */
public class ContactHelper extends HelperBase {


  public ContactHelper(WebDriver wd) {
    super(wd);
  }

  public void createUser(UserData user) {
    newUser();
    fillUserField(user);
    submitCreating();
  }

  public void deleteFirstContact() {
    initModification(0);
    deleteUser();
  }

  // object action
  // user create read update delete list CRUDL IE P
  // book CRUDL
  // film CRUDL

  // ==========================================================================================

  public void submitCreating() {
    click(By.xpath("//div[@id='content']/form/input[21]"));
  }

  public void fillUserField(UserData UserData) {
    type(By.name("firstname"), UserData.getFirstname());
    type(By.name("lastname"), UserData.getLastname());
    type(By.name("address"), UserData.getAddress());
    type(By.name("home"), UserData.getPhone());
  }

  public void newUser() {
    click(By.linkText("add new"));
  }

  public void initModification(int index) {
    //wd.findElements(By.name("selected[]")).get(index).click();
    click(By.xpath("//*[@id='maintable']//tr[" + index + 2 + "]/td[8]/a"));
  }

  public void deleteUser() {
    click(By.xpath("//div[@id='content']/form[2]/input[2]"));
  }

  public void initUser() {
    By id = By.id("42");
    if (!isElementSelected(id))
      click(id);
  }

  public void deleteUser1() {
    wd.findElement(By.xpath("//div[@id='content']/form[2]/div[2]/input")).click();
  }

  public boolean isThereAUser() {
    return isElementPresent(By.name("selected[]"));
  }

  public List<UserData> getContactList() {
    List<UserData> contacts = new ArrayList<UserData>();

    List<WebElement> rows = findElements(By.cssSelector("tr[name=entry]"));

    for (WebElement row : rows) {
      int id = Integer.parseInt(row.findElement(By.cssSelector("td:first-of-type input")).getAttribute("id"));

      String firstname = row.findElement(By.cssSelector("td:nth-of-type(3)")).getText();
      String lastname = row.findElement(By.cssSelector("td:nth-of-type(2)")).getText();
// phone adress email
      contacts.add(new UserData(id, firstname, lastname, null, null));
    }
    return contacts;
  }
}
@@ -143,58 +143,8 @@
 nt;%0A
-import org.openqa.selenium.firefox.FirefoxDriver;%0A
 impo
@@ -179,16 +179,16 @@
 erData;%0A
+
 %0Aimport 
@@ -579,111 +579,8 @@
  %7D%0A%0A
-  // object action%0A  // user create read update delete list CRUDL IE P%0A  // book CRUDL%0A  // film CRUDL%0A
 %0A  /
@@ -1144,24 +1144,24 @@
 nt index) %7B%0A
+
     //wd.fin
@@ -1217,84 +1217,8 @@
 ();%0A
-    click(By.xpath(%22//*%5B@id='maintable'%5D//tr%5B%22 + index + 2 + %22%5D/td%5B8%5D/a%22));%0A
   %7D%0A

update Lesson4