package ru.stqa.pft.addressbook.tests;

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 oleg on 24.03.16.
 */
public class ContactDetailsTests extends TestBase {

    @Test
    public void testContactDetails() {
        app.goTo().home();
        ContactData contact =  app.contact().all().iterator().next();
        ContactData contactInfoFormEditForm = app.contact().infoFromEditForm(contact);
        assertThat(app.contact().infoDetailsContact(contact),equalTo(mergeAll(contactInfoFormEditForm)));

    }

    private String mergePersonalData(ContactData contact) {
        return Arrays.asList(mergeFirstNameLastName(contact),contact.getUsername(),contact.getCompany(),contact.getHomeadress())
                .stream()
                .filter((s)->!s.equals(""))
                .map(ContactDetailsTests::cleaned)
                .collect(Collectors.joining("\n"));
    }

    private String mergePhones(ContactData contact) {
        return Arrays.asList("H: "+ contact.getHomeNumber(),"M: " + contact.getMobilenumber(),"W: " + contact.getWorkNumber())
                .stream()
                .filter((s)->!s.equals(""))
                .map(ContactDetailsTests::cleaned)
                .collect(Collectors.joining("\n"));
    }


    private String mergeEmails(ContactData contact) {
             String[] getEmail = contact.getEmail().split("@");
             String[] getEmail1 = contact.getEmail1().split("@");
             String[] getEmail2 = contact.getEmail2().split("@");
        return Arrays.asList(contact.getEmail() + " (www."+getEmail[1]+ ")",
                             contact.getEmail1() + " (www."+getEmail1[1]+ ")",
                             contact.getEmail2() + " (www."+getEmail2[1]+ ")")
                .stream()
                .filter((s)->!s.equals(""))
                .map(ContactDetailsTests::cleaned)
                .collect(Collectors.joining("\n"));
    }

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


    private String mergeAll(ContactData contact) {
        return Arrays.asList(mergePersonalData(contact),mergePhones(contact),mergeEmails(contact))
                .stream()
                .filter((s)->!s.equals(""))
                .collect(Collectors.joining("\n\n"));
    }



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




}

@@ -717,32 +717,352 @@
 orm)));%0A%0A    %7D%0A%0A
+%0A    private String mergeFirstNameLastName(ContactData contact) %7B%0A        return Arrays.asList(contact.getFirstname(),contact.getLastname())%0A                .stream()%0A                .filter((s)-%3E!s.equals(%22%22))%0A                .map(ContactDetailsTests::cleaned)%0A                .collect(Collectors.joining(%22 %22));%0A    %7D%0A%0A
     private Stri
@@ -2445,326 +2445,8 @@
  %7D%0A%0A
-    private String mergeFirstNameLastName(ContactData contact) %7B%0A        return Arrays.asList(contact.getFirstname(),contact.getLastname())%0A                .stream()%0A                .filter((s)-%3E!s.equals(%22%22))%0A                .map(ContactDetailsTests::cleaned)%0A                .collect(Collectors.joining(%22 %22));%0A    %7D%0A
 %0A%0A  

modify tests