package ru.sfwt.mt.addressbook.appmanager;

import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
import org.testng.Assert;
import ru.sfwt.mt.addressbook.model.AddressData;
import ru.sfwt.mt.addressbook.model.Addresses;
import ru.sfwt.mt.addressbook.model.GroupData;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class ContactHelper extends HelperBase {

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

  public void returnToHomePage() {
    click(By.linkText("home"));
  }

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

  public void fillAddressForm(AddressData addressData, boolean creation) {
    type("firstname", addressData.getFirstname());
    type("lastname", addressData.getLastname());
    type("address", addressData.getAddress());
    type("home", addressData.getHomenumber());
    type("mobile", addressData.getMobilenumber());
    type("work", addressData.getWorknumber());
    type("email", addressData.getEmail1());
    type("email2", addressData.getEmail2());
    type("email3", addressData.getEmail3());

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

//  public void selectAddress(int index) {
//     //click(By.xpath("//table[@id='maintable']/tbody/tr[2]/td[8]/a/img"));
//    WebElement row = wd.findElements(By.cssSelector("tr[name=entry]")).get(index);
//    row.findElement(By.xpath("./td[8]/a/img")).click();
//  }

  public void selectAddressById(int id) {
    WebElement row = wd.findElement(By.cssSelector("tr[name=entry]>td.center>input[value='" + id + "']"));
    row.findElement(By.xpath("./../../td[8]/a/img")).click();
  }

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

  public void deleteSelectAddress() {
    click(By.xpath("//div/div[4]/form[2]/input[2]"));
  }

  public void create(AddressData address) {
    fillAddressForm(address, true);
    enterAddressCreation();
    returnToHomePage();
  }
  public void modify(AddressData address) {
    selectAddressById(address.getId());
    fillAddressForm(address, false);
    submitAddressModification();
    returnToHomePage();
  }

//  public void delete(int index) {
//    selectAddress(index);
//    deleteSelectAddress();
//    returnToHomePage();
//  }

  public void delete(AddressData address) {
    selectAddressById(address.getId());
    deleteSelectAddress();
    returnToHomePage();
  }
  public boolean isThereAnAderess() {
    //return isElementPresent(By.xpath("//table[@id='maintable']/tbody/tr[2]/td[8]/a/img"));
    return isElementPresent(By.xpath("//table[@id='maintable']//tr[2]"));
  }

  public int getAddressCount() {
    return wd.findElements(By.name("selected[]")).size();
  }

//  public List<AddressData> list() {
//    List<AddressData> addresses = new ArrayList<AddressData>();
//    List<WebElement> elements = wd.findElements(By.cssSelector("tr[name=entry]"));
//    for (WebElement element : elements) {
//      String firstname = element.findElement(By.xpath("./td[3]")).getText();
//      String lastname = element.findElement(By.xpath("./td[2]")).getText();
//      int id = Integer.parseInt(element.findElement(By.tagName("input")).getAttribute("value"));
//      addresses.add(new AddressData().withId(id).withFirstname(firstname).withLastname(lastname));
//    }
//    return addresses;
//  }
  public Addresses all() {
    Addresses  addresses = new Addresses();
    List<WebElement> elements = wd.findElements(By.cssSelector("tr[name=entry]"));
    for (WebElement element : elements) {
      String firstname = element.findElement(By.xpath("./td[3]")).getText();
      String lastname = element.findElement(By.xpath("./td[2]")).getText();
      int id = Integer.parseInt(element.findElement(By.tagName("input")).getAttribute("value"));
      addresses.add(new AddressData().withId(id).withFirstname(firstname).withLastname(lastname));
    }
    return addresses;
  }


}

@@ -2321,32 +2321,57 @@
 ressCreation();%0A
+    addressCache = null;%0A
     returnToHome
@@ -2532,24 +2532,49 @@
 fication();%0A
+    addressCache = null;%0A
     returnTo
@@ -2823,24 +2823,49 @@
 tAddress();%0A
+    addressCache = null;%0A
     returnTo
@@ -3190,687 +3190,173 @@
  %7D%0A%0A
-//
   p
-ublic List%3CAddressData%3E list() %7B%0A//    List%3CAddressData%3E addresses = new ArrayList%3CAddressData%3E();%0A//    List%3CWebElement%3E elements = wd.findElements(By.cssSelector(%22tr%5Bname=entry%5D%22));%0A//    for (WebElement element : elements) %7B%0A//      String firstname = element.findElement(By.xpath(%22./td%5B3%5D%22)).getText();%0A//      String lastnam
+rivate Addresses addressCache = null;%0A%0A  public Addresses all() %7B%0A    if (addressCach
 e 
+!
 = 
-element.findElement(By.xpath(%22./td%5B2%5D%22)).getText();%0A//      int id = Integer.parseInt(element.findElement(By.tagName(%22input%22)).getAttribute(%22value%22));%0A//      addresses.add(new AddressData().withId(id).withFirstname(firstname).withLastname(lastname)
+null) %7B%0A      return new Addresses(addressCache
 );%0A
-//
     %7D%0A
-//
     
-return addresses;%0A//  %7D%0A  public Addresses all() %7B%0A    Addresses  addresses
+addressCache
  = n
@@ -3759,18 +3759,21 @@
  address
+Cach
 e
-s
 .add(new
@@ -3869,17 +3869,21 @@
 urn 
-a
+new A
 ddresses
 ;%0A  
@@ -3878,16 +3878,30 @@
 ddresses
+(addressCache)
 ;%0A  %7D%0A%0A%0A

Create caching for AddressTests (ContactHelper)