package ru.stqa.pft.addressbook.tests;


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

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

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

/**
 * Created by Alesya on 05/06/2017.
 */
public class ContactDetailsTests extends TestBase {
  @BeforeMethod()
  public void ensurePreconditions() {
    app.goTo().homePage();
    if (app.contact().all().size() == 0) {
      app.contact().create(new ContactData().
              withFirstname("Olga02").withLastname("Test").withHomephone("111").withMobilephone("222").withWorkphone("333")
              .withEmai1("anna@gmail.com").withEmail2("test").withEmail3("test2")
              .withGroup("[none]"), true);
    }
  }

//  @Test
//  public void testContactDetailsSplit() {
//    ContactData contact = app.contact().all().iterator().next();
//    ContactData contactView = app.contact().infoContactDetailsSplit(contact);
//    ContactData contactInfoEditForm = app.contact().infoContactEditForm(contact);
//
//    assertThat(contactView.getFirstname(), equalTo(contactInfoEditForm.getFirstname()));
//    assertThat(contactView.getLastname(), equalTo(contactInfoEditForm.getLastname()));
//    assertThat(contactView.getAddress(), equalTo(contactInfoEditForm.getAddress()));
//    assertThat(cleaned(contactView.getHomephone()), equalTo(cleaned(contactInfoEditForm.getHomephone())));
//    assertThat(cleaned(contactView.getMobilephone()), equalTo(cleaned(contactInfoEditForm.getMobilephone())));
//    assertThat(cleaned(contactView.getWorkphone()), equalTo(cleaned(contactInfoEditForm.getWorkphone())));
//    assertThat(contactView.getEmail(), equalTo(contactInfoEditForm.getEmail()));
//    assertThat(contactView.getEmail2(), equalTo(contactInfoEditForm.getEmail2()));
//    assertThat(contactView.getEmail3(), equalTo(contactInfoEditForm.getEmail3()));
//  }

//  @Test
//  public void testContactDetailsMerge() {
//    ContactData contact = app.contact().all().iterator().next();
//    ContactData contactView = app.contact().infoContactDetailsMerge(contact);
//    ContactData contactInfoEditForm = app.contact().infoContactEditForm(contact);
//
//    System.out.println(cleaned(contactView.getFirstname()));
//    System.out.println(mergedDetails(contactInfoEditForm));
//
//    assertThat(cleaned(contactView.getFirstname()), equalTo(mergedDetails(contactInfoEditForm)));
//
//  }
//
//  public static String cleaned(String phone) {
//    return phone.replaceAll("\\s", "")
//            .replaceAll("(M:)", "").replaceAll("(H:)", "").replaceAll("(W:)", "");
//  }
//
//  private String mergedDetails(ContactData contact) {
//    return Arrays.asList(contact.getFirstname(), contact.getLastname(), contact.getAddress(),
//            contact.getHomephone(), contact.getMobilephone(), contact.getWorkphone(),
//            contact.getEmail(), contact.getEmail2(), contact.getEmail3())
//            .stream().filter((s) -> !s.equals(""))
//            .map(ContactDetailsTests::cleaned)
//            .collect(Collectors.joining(""));
//  }


  @Test
  public void testContactDetailsMergeNew() {
    ContactData contact = app.contact().all().iterator().next();
    ContactData contactView = app.contact().infoContactDetailsMerge(contact);
    ContactData contactInfoEditForm = app.contact().infoContactEditForm(contact);

    System.out.println(cleaned(contactView.getFirstname()));
    System.out.println(mergedDetails(contactInfoEditForm));

    assertThat(cleaned(contactView.getFirstname()), equalTo(mergedDetails(contactInfoEditForm)));
  }


  public static String cleaned(String phone) {
    return phone.replaceAll("\\s", "");
  }

  private String mergedDetails(ContactData contact) {
    return Arrays.asList(contact.getFirstname(), contact.getLastname(), contact.getAddress(),
            contact.getHomephone(), contact.getMobilephone(), contact.getWorkphone(),
            contact.getEmail(), contact.getEmail2(), contact.getEmail3())
            .stream().filter((s) -> !s.equals(""))
            .map(ContactDetailsTests::cleaned)
            .collect(Collectors.joining(""));
  }






}

@@ -862,2340 +862,8 @@
  %7D%0A%0A
-//  @Test%0A//  public void testContactDetailsSplit() %7B%0A//    ContactData contact = app.contact().all().iterator().next();%0A//    ContactData contactView = app.contact().infoContactDetailsSplit(contact);%0A//    ContactData contactInfoEditForm = app.contact().infoContactEditForm(contact);%0A//%0A//    assertThat(contactView.getFirstname(), equalTo(contactInfoEditForm.getFirstname()));%0A//    assertThat(contactView.getLastname(), equalTo(contactInfoEditForm.getLastname()));%0A//    assertThat(contactView.getAddress(), equalTo(contactInfoEditForm.getAddress()));%0A//    assertThat(cleaned(contactView.getHomephone()), equalTo(cleaned(contactInfoEditForm.getHomephone())));%0A//    assertThat(cleaned(contactView.getMobilephone()), equalTo(cleaned(contactInfoEditForm.getMobilephone())));%0A//    assertThat(cleaned(contactView.getWorkphone()), equalTo(cleaned(contactInfoEditForm.getWorkphone())));%0A//    assertThat(contactView.getEmail(), equalTo(contactInfoEditForm.getEmail()));%0A//    assertThat(contactView.getEmail2(), equalTo(contactInfoEditForm.getEmail2()));%0A//    assertThat(contactView.getEmail3(), equalTo(contactInfoEditForm.getEmail3()));%0A//  %7D%0A%0A//  @Test%0A//  public void testContactDetailsMerge() %7B%0A//    ContactData contact = app.contact().all().iterator().next();%0A//    ContactData contactView = app.contact().infoContactDetailsMerge(contact);%0A//    ContactData contactInfoEditForm = app.contact().infoContactEditForm(contact);%0A//%0A//    System.out.println(cleaned(contactView.getFirstname()));%0A//    System.out.println(mergedDetails(contactInfoEditForm));%0A//%0A//    assertThat(cleaned(contactView.getFirstname()), equalTo(mergedDetails(contactInfoEditForm)));%0A//%0A//  %7D%0A//%0A//  public static String cleaned(String phone) %7B%0A//    return phone.replaceAll(%22%5C%5Cs%22, %22%22)%0A//            .replaceAll(%22(M:)%22, %22%22).replaceAll(%22(H:)%22, %22%22).replaceAll(%22(W:)%22, %22%22);%0A//  %7D%0A//%0A//  private String mergedDetails(ContactData contact) %7B%0A//    return Arrays.asList(contact.getFirstname(), contact.getLastname(), contact.getAddress(),%0A//            contact.getHomephone(), contact.getMobilephone(), contact.getWorkphone(),%0A//            contact.getEmail(), contact.getEmail2(), contact.getEmail3())%0A//            .stream().filter((s) -%3E !s.equals(%22%22))%0A//            .map(ContactDetailsTests::cleaned)%0A//            .collect(Collectors.joining(%22%22));%0A//  %7D%0A%0A%0A
   @T
@@ -907,11 +907,8 @@
 erge
-New
 () %7B
@@ -1138,130 +1138,8 @@
 );%0A%0A
-    System.out.println(cleaned(contactView.getFirstname()));%0A    System.out.println(mergedDetails(contactInfoEditForm));%0A%0A
     
@@ -1237,17 +1237,16 @@
 );%0A  %7D%0A%0A
-%0A
   public
@@ -1379,24 +1379,421 @@
  contact) %7B%0A
+%0A    String homephone = %22%22;%0A    String mobilephone = %22%22;%0A    String workphone = %22%22;%0A%0A    if (!contact.getHomephone().equals(%22%22)) %7B%0A      homephone = %22H:%22 + contact.getHomephone();%0A    %7D%0A%0A    if (!contact.getMobilephone().equals(%22%22)) %7B%0A      mobilephone = %22M:%22 + contact.getMobilephone();%0A    %7D%0A%0A    if (!contact.getWorkphone().equals(%22%22)) %7B%0A      workphone = %22W:%22 + contact.getWorkphone();%0A    %7D%0A%0A
     return A
@@ -1890,78 +1890,41 @@
     
-contact.getHomephone(), contact.getMobilephone(), contact.getW
+homephone, mobilephone, w
 orkphone
 (),%0A
@@ -1919,18 +1919,16 @@
 orkphone
-()
 ,%0A      
@@ -2145,14 +2145,10 @@
 ;%0A  %7D%0A%0A%0A
-%0A%0A%0A%0A
 %7D%0A

Fix for ContactDetailsTest.