package ru.stqa.sch.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.stqa.sch.addressbook.model.ContactData;
import ru.stqa.sch.addressbook.model.GroupData;

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

public class ContactHelper extends HelperBase{

    public NavigationHelper navigationHelper;

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

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

    public void fillContactForm(ContactData contactData, boolean creation) {
        type(By.name("firstname"), contactData.getFirstname());
        type(By.name("lastname"), contactData.getLastname());
        type(By.name("address"), contactData.getAddress());
        type(By.name("mobile"), contactData.getMobile());
        type(By.name("email"), contactData.getEmail());

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

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

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

    public void initContactModification() {
        click(By.xpath("//table[@id='maintable']/tbody/tr[2]/td[8]/a/img"));
    }

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

    public void createContact(ContactData contact, boolean creation) {
        navigationHelper = new NavigationHelper(wd);
        navigationHelper.gotoContactPage();
        fillContactForm(contact, creation);
        subvitContactCreation();
    }

    public boolean isThereAContact() {
        return isElementPresence(By.name("selected[]"));
    }

    public void checkContact(ContactData contact, boolean creation) {
        navigationHelper = new NavigationHelper(wd);
        navigationHelper.gotoHomePage();
        if (! isThereAContact()) {
            createContact(contact, creation);
        }
    }

    public int getContactCount() {
        return elementCount(By.name("selected[]"));
    }

    public List<ContactData> getContactList() {
        List<ContactData> contacts = new ArrayList<ContactData>();
        List<WebElement> elements = wd.findElements(By.xpath("//table[@id='maintable']//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();
            String id = element.findElement(By.tagName("input")).getAttribute("value");
            ContactData contact = new ContactData(firstName, lastName, null, null, null, null);
            contacts.add(contact);
        }
        return contacts;
    }
}

@@ -2977,20 +2977,34 @@
     
-Str
 in
-g
+t
  id = 
+Integer.parseInt(
 elem
@@ -3061,16 +3061,17 @@
 %22value%22)
+)
 ;%0A      

Revert "Revert "contact sort in modification tests""