package ru.stqa.sch.addressbook.tests;

import org.testng.annotations.Test;
import ru.stqa.sch.addressbook.model.ContactData;

import java.util.Arrays;
import java.util.stream.Collectors;

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

public class ContactFullInfoTest extends TestBase {

  @Test
  public void testFullInfo() {
    app.contact().checkContact(new ContactData()
            .withFirstname("firstName2").withLastname("lastName2").withAddress("address2")
            .withHomePhone("111").withMobilePhone("222").withWorkPhone("333")
            .withEmail("test2@test.ru").withEmail2("test @   test").withEmail3("@"), true);
    app.goTo().homePage();
    ContactData contact = app.contact().all().iterator().next();
    String contactInfoFromFullForm = app.contact().infoFromFullForm(contact);

    assertThat(mergeData(contact), equalTo(cleaned(contactInfoFromFullForm)));
  }

  private String mergeData(ContactData contact) {
    return Arrays.asList(contact.getFirstname(), contact.getLastname(),
            contact.getAddress(), contact.getAllPhones(), contact.getAllEmails())
            .stream().filter((s) -> ! s.equals(""))
            .map(ContactFullInfoTest::cleaned)
            .collect(Collectors.joining());
  }

  public static String cleaned(String data) {
    return data.replaceAll("\\s", "").replace("M:", "").replace("H:", "").replace("W:", "").replaceAll("\\([^)]+\\)", "");
  }
}

@@ -775,24 +775,107 @@
 r().next();%0A
+    ContactData contactInfoFromEditForm = app.contact().infoFromEditForm(contact);%0A
     String c
@@ -945,16 +945,18 @@
 );%0A%0A    
+//
 assertTh
@@ -1018,24 +1018,119 @@
 ullForm)));%0A
+    assertThat(mergeData(contactInfoFromEditForm), equalTo(cleaned(contactInfoFromFullForm)));%0A
   %7D%0A%0A  priva
@@ -1243,16 +1243,38 @@
 tname(),
+ contact.getAddress(),
 %0A       
@@ -1278,16 +1278,18 @@
         
+//
 contact.
@@ -1292,22 +1292,24 @@
 act.getA
-ddr
+llPhon
 es
-s
 (), cont
@@ -1310,38 +1310,74 @@
 , contact.getAll
+Emails())%0A            contact.getHome
 Phone
-s
 (), contact.getA
@@ -1371,33 +1371,133 @@
  contact.get
-AllEmails
+MobilePhone(), contact.getWorkPhone(),%0A            contact.getEmail(), contact.getEmail2(), contact.getEmail3
 ())%0A        

correct understanding of hw13