package qa.softwaretesting.addressbook.tests;

import org.testng.Assert;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import qa.softwaretesting.addressbook.model.ContactData;
import qa.softwaretesting.addressbook.model.Contacts;
import qa.softwaretesting.addressbook.model.GroupData;
import qa.softwaretesting.addressbook.model.Groups;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;

public class AddContactToGroupTests extends TestBase {

  public GroupData group = new GroupData().withName("test1");

  @BeforeMethod
  public void ensurePreconditions() throws InterruptedException {
    if (app.db().groups().size() == 0) {
      app.goTo().groupPage();
      app.group().create(group);
    }

    if (app.db().contactsInGroups() > 0) {
      app.goTo().homePage();
      app.goTo().selectAllContacts();
      app.goTo().submitDeletion();
      app.goTo().acceptInDeletionWindow();
    }

    Thread.sleep(1000);

    if (app.db().contacts().size() == 0) {
      app.goTo().homePage();
      app.goTo().addNewContact();
      app.contact().create(new ContactData()
              .withFirstName("Alex")
              .withHome("111")
              .withMobile("222")
              .withWork("333")
              .withLastName("Popovich"), true);
    }
  }

  @Test
  public void testAddContactToGroup() throws InterruptedException {
    app.goTo().homePage();
    Contacts before = app.db().contacts();
    int beforeTable = app.db().contactsInGroups();
    Groups groupBefore = app.db().conGroup();
    ContactData randomId = before.iterator().next();
    ContactData contact = new ContactData().withId(randomId.getId());
    app.contact().selectContactByCheckbox(contact.getId());
    app.contact().addContactToGroup();
    //Groups group = app.db().conGroup();

    Thread.sleep(1000);

    int afterTable = app.db().contactsInGroups();
    Contacts after = app.db().contacts();
    Groups groupAfter = app.db().conGroup();

    assertThat(groupAfter, equalTo(groupBefore.withAdded(group)));
    assertThat(after.size(), equalTo(before.size()));

    if (beforeTable == 0) {
      Assert.assertEquals(afterTable, beforeTable + 1);
      Assert.assertNotEquals(groupBefore, groupAfter);
    } else {
      Assert.assertEquals(beforeTable, afterTable);
    }
  }
}

@@ -2009,24 +2009,198 @@
 onGroup();%0A%0A
+    int max = 0;%0A    for (GroupData g : groupAfter) %7B%0A      if (g.getId() %3E max) %7B%0A        max = g.getId();%0A      %7D%0A    %7D%0A    group.withId(max);%0A    groupBefore.add(group);%0A%0A
     assertTh

Add 16 max