package com.zelory.kace.adressbook.data.model;

import com.google.gson.JsonSyntaxException;
import com.google.gson.reflect.TypeToken;
import com.zelory.kace.adressbook.util.JsonParser;
import lombok.Getter;
import lombok.Setter;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

@Getter
@Setter
public class AddressBook {
    private String name;
    private List<Person> persons;
    private String path;

    public AddressBook() {
        this("Untitled", new ArrayList<>());
    }

    public AddressBook(File file) throws IOException, JsonSyntaxException {
        StringBuilder data = new StringBuilder();
        Files.lines(Paths.get(file.getAbsolutePath()), StandardCharsets.UTF_8).forEach(data::append);
        name = file.getName();
        persons = JsonParser.getInstance().getParser().fromJson(data.toString(), new TypeToken<List<Person>>() {}.getType());
        path = file.getAbsolutePath();
    }

    public AddressBook(String name, List<Person> persons) {
        this.name = name;
        this.persons = persons;
    }
}

@@ -465,16 +465,112 @@
 sBook %7B%0A
+%0A    public static final int PERSON_ADDED = 1;%0A    public static final int PERSON_UPDATED = 2;%0A%0A
     priv
@@ -1279,11 +1279,300 @@
 s;%0A    %7D
+%0A%0A    public int addOrUpdatePerson(Person person) %7B%0A        int pos = persons.indexOf(person);%0A        if (pos %3E 0) %7B%0A            persons.set(pos, person);%0A            return PERSON_UPDATED;%0A        %7D else %7B%0A            persons.add(person);%0A            return PERSON_ADDED;%0A        %7D%0A    %7D
 %0A%7D%0A

Add method for handling add or update person