package ru.stqa.pft.addressbook.generators;

import com.beust.jcommander.JCommander;
import com.beust.jcommander.Parameter;
import com.beust.jcommander.ParameterException;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.thoughtworks.xstream.XStream;
import ru.stqa.pft.addressbook.model.ContactData;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;

public class ContactDataGenerator {

    @Parameter(names = "-c", description = "Group count")
    public int count;

    @Parameter(names = "-f", description = "Target file")
    public String file;

    public static void main(String[] args) throws IOException {
        ContactDataGenerator contactDataGenerator = new ContactDataGenerator();
        JCommander jCommander = new JCommander(contactDataGenerator);
        try {
            jCommander.parse(args);
        }catch (ParameterException ex){
            jCommander.usage();
            return;
        }
        contactDataGenerator.run();
    }

    private void run() throws IOException {
        List<ContactData> contacts = generateContacts(count);
        saveAsJson(contacts, new File(file));
    }

    private List<ContactData> generateContacts(int count) {
        List<ContactData> contacts = new ArrayList<ContactData>();
        for (int i = 0; i < count; i++){
            contacts.add(new ContactData()
                    .withFirstName(String.format("firstname %s", i))
                    .withMiddleName(String.format("middlename %s", i))
                    .withLastName(String.format("lastname %s", i))
                    .withNickName(String.format("nickname %s", i))
                    .withTitle(String.format("title %s", i))
                    .withCompany(String.format("company %s", i))
                    .withFirstAddress(String.format("firstaddress %s", i))
                    .withPhoneNumber(String.format("0636773421 %s", i))
                    .withMobilePhoneNumber(String.format("04454543232 %s", i))
                    .withWorkPhoneNumber(String.format("097463232 %s", i))
                    .withFax(String.format(" %s", i))
                    .withMainEmail(String.format("email%s@gmail.com", i))
                    .withSecondEmail(String.format("secondEmail%s@gmail.com", i))
                    .withThirdEmail(String.format("thirdEmail%s@gmail.com", i))
                    .withHomePage(String.format("myhomepage%s.com", i))
                    .withBirthDay("22")
                    .withBirthMonth("06")
                    .withBirthDay("2000")
                    .withAnniverDay("22")
                    .withAnniverMonth("06")
                    .withAnniverYear("2007")
                    .withSecondAddress(String.format("My second address %s", i))
                    .withSecondHomePhoneNumber(String.format("0987673420 %s", i))
                    .withNotes(String.format("Some notes %s", i)));

        }
        return contacts;
    }
    public void saveAsXml(List<ContactData> contacts, File file) throws IOException {
        XStream xstream = new XStream();
        xstream.processAnnotations(ContactData.class);
        String xml = xstream.toXML(contacts);
        try (Writer writer = new FileWriter(file)) {
            writer.write(xml);
        }
    }

    public void saveAsJson(List<ContactData> contacts, File file) throws IOException {
        Gson gson = new GsonBuilder().setPrettyPrinting().excludeFieldsWithoutExposeAnnotation().create();
        String json = gson.toJson(contacts);
        try (Writer writer = new FileWriter(file)) {
            writer.write(json);
        }
    }


}

@@ -674,16 +674,101 @@
  file;%0A%0A
+    @Parameter(names = %22-d%22, description = %22Data format%22)%0A    public String format;%0A%0A
     publ
@@ -815,32 +815,32 @@
 s IOException %7B%0A
-
         ContactD
@@ -1032,16 +1032,17 @@
        %7D
+ 
 catch (P
@@ -1062,16 +1062,17 @@
 tion ex)
+ 
 %7B%0A      
@@ -1288,45 +1288,270 @@
     
-saveAsJson(contacts, new File(file));
+if (format.equals(%22xml%22)) %7B%0A            saveAsXml(contacts, new File(file));%0A        %7D else if (format.equals(%22json%22)) %7B%0A            saveAsJson(contacts, new File(file));%0A        %7D else %7B%0A            System.out.println(%22Unrecognized format %22 + format);%0A        %7D
 %0A   
@@ -1677,24 +1677,24 @@
 actData%3E();%0A
-
         for 
@@ -1720,16 +1720,17 @@
 nt; i++)
+ 
 %7B%0A      
@@ -3353,24 +3353,25 @@
 acts;%0A    %7D%0A
+%0A
     public v

extend contactdatagenerator