package ua.projects.javatests.addressbook.appmanager;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.ui.Select;
import org.testng.Assert;
import ua.projects.javatests.addressbook.model.ContactData;
import ua.projects.javatests.addressbook.model.GroupData;

public class ContactHelper extends HelperBase {

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

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

    public void fillContactForm(ContactData contactData, boolean creation) {
            if (creation) {
                click(By.name("new_group"));
                    if (wd.findElements(By.cssSelector("select[name=new_group] option")).size() > 1) {
                    return;
                } else {
                    GroupHelper gh = new GroupHelper(wd);
                    NavigationHelper nh = new NavigationHelper(wd);
                    nh.goToGroupPage();
                    gh.createGroup(new GroupData("test1", null, null));
                    click(By.linkText("add new"));
                    type(By.name("firstname"), contactData.getFirstName());
                    type(By.name("lastname"), contactData.getLastName());
                    type(By.name("nickname"), contactData.getNickname());
                    type(By.name("company"), contactData.getWorkPlace());
                    type(By.name("home"), contactData.getTelephoneNumber());
                    type(By.name("email"), contactData.getEmail());
                }
                new Select(wd.findElement(By.name("new_group"))).selectByVisibleText(contactData.getGroup());
            } else {
                Assert.assertFalse(isElementPresent(By.name("new_group")));
            }
        type(By.name("firstname"), contactData.getFirstName());
        type(By.name("lastname"), contactData.getLastName());
        type(By.name("nickname"), contactData.getNickname());
        type(By.name("company"), contactData.getWorkPlace());
        type(By.name("home"), contactData.getTelephoneNumber());
        type(By.name("email"), contactData.getEmail());
    }

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

    public void selectContact() {
        click(By.name("selected[]"));
    }

    public void deleteSelectedContact() {
        click(By.cssSelector(".left>input[value='Delete']"));
    }

    public void initContactModification() {
        click(By.cssSelector(".center>a>img[title='Edit']"));
    }

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

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

    public void createContact(ContactData contact) {
        fillContactForm(contact, true);
        submitNewContact();
        NavigationHelper nh = new NavigationHelper(wd);
        nh.returnToHomePage();
    }
}

@@ -601,32 +601,403 @@
 ean creation) %7B%0A
+        type(By.name(%22firstname%22), contactData.getFirstName());%0A        type(By.name(%22lastname%22), contactData.getLastName());%0A        type(By.name(%22nickname%22), contactData.getNickname());%0A        type(By.name(%22company%22), contactData.getWorkPlace());%0A        type(By.name(%22home%22), contactData.getTelephoneNumber());%0A        type(By.name(%22email%22), contactData.getEmail());%0A
             if (
@@ -2184,379 +2184,8 @@
   %7D%0A
-        type(By.name(%22firstname%22), contactData.getFirstName());%0A        type(By.name(%22lastname%22), contactData.getLastName());%0A        type(By.name(%22nickname%22), contactData.getNickname());%0A        type(By.name(%22company%22), contactData.getWorkPlace());%0A        type(By.name(%22home%22), contactData.getTelephoneNumber());%0A        type(By.name(%22email%22), contactData.getEmail());%0A
     

Fix bug in ContactHelper.