package ru.stqa.pft.addressbook.appmanager;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.Select;
import org.testng.Assert;
import ru.stqa.pft.addressbook.model.UserData;
import ru.stqa.pft.addressbook.model.Users;

import java.util.List;

/**
 * Created by jane on 10/31/16.
 */
public class UserHelper extends HelperBase {

  public UserHelper(WebDriver wd) {
    super(wd);

  }

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

  public void selectUser(int index) {
    wd.findElements(By.name("selected[]")).get(index).click();
  }

  public void selectUserById(int id) {
    wd.findElement(By.cssSelector("input[value='" + id + "']")).click();
  }


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

  public void fillUserForm(UserData userData, boolean creation) {
    type(By.name("firstname"), userData.getFirstname());
    type(By.name("lastname"), userData.getLastname());
    type(By.name("address"), userData.getAddress());
    type(By.name("home"), userData.getHomenumber());
    type(By.name("email"), userData.getEmail());

    if (creation) {
      new Select(wd.findElement(By.name("new_group"))).selectByVisibleText(userData.getGroup());
    } else {
      Assert.assertFalse(isElementPresent(By.name("new_group")));
    }

  }

  public void initUserCreation() {
    wd.findElement(By.linkText("add new")).click();
  }

  public void initUserModification() {
    wd.findElement(By.xpath("//img[@title=\"Edit\"]")).click();
  }

  public void submitUserModification() {
    click(By.name("update"));
  }

  public void closeUserDeletionAlert() {
    wd.switchTo().alert().accept();
  }

  private void goToHomePage() {
    click(By.linkText("home"));
  }


  public void create(UserData user) {
    initUserCreation();
    fillUserForm(user, true);
    submitUserCreation();
    goToHomePage();
  }

  public void modify(UserData user) {
    selectUserById(user.getId());
    initUserModification();
    fillUserForm(user, false);
    submitUserModification();
    goToHomePage();
  }

  public void delete(UserData user) {
    selectUserById(user.getId());
    deleteSelectedUsers();
    closeUserDeletionAlert();
    goToHomePage();
  }

  public Users all() {
    Users users = new Users();
    List<WebElement> elements = wd.findElements(By.name("entry"));
    for (WebElement element : elements){
      String lastName = element.findElement(By.xpath(".//td[2]")).getText();
      String firstName = element.findElement(By.xpath(".//td[3]")).getText();
      int id = Integer.parseInt(element.findElement(By.tagName("input")).getAttribute("value"));
      users.add(new UserData().withId(id).withFirstname(firstName).withLastname(lastName));
    }
    return users;
  }

  public UserData infoFromEditForm(UserData user) {
    initUserModificationById(user.getId());
    String firstname = wd.findElement(By.name("firstname")).getAttribute("value");
    String lastname = wd.findElement(By.name("lastname")).getAttribute("value");
    String home = wd.findElement(By.name("home")).getAttribute("value");
    String mobile = wd.findElement(By.name("mobile")).getAttribute("value");
    String work = wd.findElement(By.name("work")).getAttribute("value");
    wd.navigate().back();
    return new UserData().withId(user.getId()).withFirstname(firstname).withLastname(lastname)
            .withHomePhone(home).withMobilePhone(mobile).withWorkPhone(work);
  }

  private void initUserModificationById(int id) {
    WebElement checkbox = wd.findElement(By.cssSelector(String.format("input[value='%s']", id)));
    WebElement row = checkbox.findElement(By.xpath("./../.."));
    List<WebElement> cells = row.findElements(By.tagName("td"));
    cells.get(7).findElement(By.tagName("a")).click();

//    wd.findElement(By.xpath(String.format("//input[@value='%s']/../../td[8]/a", id))).click();
//    wd.findElement(By.xpath(String.format("//tr[.//input[@value='%s']]/td[8]/a", id))).click();
//    wd.findElement(By.cssSelector(String.format("a[href='edit.php?id=%s']", id))).click();
  }

}
@@ -327,19 +327,67 @@
 va.util.
-Lis
+HashSet;%0Aimport java.util.List;%0Aimport java.util.Se
 t;%0A%0A/**%0A
@@ -2460,49 +2460,77 @@
 lic 
-Users all() %7B%0A    Users users = new Users
+Set%3CUserData%3E all() %7B%0A    Set%3CUserData%3E users = new HashSet%3CUserData%3E
 ();%0A
@@ -2550,23 +2550,19 @@
 lement%3E 
-element
+row
 s = wd.f
@@ -2616,35 +2616,28 @@
 ent 
-element : element
+row : row
 s)
+ 
 %7B%0A      
 Stri
@@ -2636,111 +2636,36 @@
     
-String lastName = element.findElement(By.xpath(%22.//td%5B2%5D%22)).getText();%0A      String firstName = element
+List%3CWebElement%3E cells = row
 .fin
@@ -2676,39 +2676,26 @@
 ment
+s
 (By.
-xpath(%22.//td%5B3%5D%22)).getText(
+tagName(%22td%22)
 );%0A 
@@ -2729,15 +2729,20 @@
 Int(
+c
 el
-ement
+ls.get(0)
 .fin
@@ -2791,24 +2791,121 @@
 (%22value%22));%0A
+      String lastname = cells.get(1).getText();%0A      String firstname = cells.get(2).getText();%0A
       users.
@@ -2953,17 +2953,17 @@
 me(first
-N
+n
 ame).wit
@@ -2976,17 +2976,17 @@
 ame(last
-N
+n
 ame));%0A 

update UserHelper