package com.javarush.test.level20.lesson07.task03;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/* Externalizable Person
Класс Person должен сериализоваться с помощью интерфейса Externalizable.
Подумайте, какие поля не нужно сериализовать.
Исправьте ошибку сериализации.
Сигнатуры методов менять нельзя.
*/
public class Solution {

    public static void main(String[] args) throws IOException, ClassNotFoundException {
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("20-07-03.txt"));
        Person person = new Person("Ivan", "Sidorov", 28);
        person.setMother(new Person());
        person.setFather(new Person());
        List<Person> children1 = new ArrayList<>();
        children1.add(new Person());
        children1.add(new Person());
        person.setChildren(children1);

        System.out.println(person.firstName);
        System.out.println(person.lastName);
        System.out.println(person.age);
        System.out.println(person.mother);
        System.out.println(person.father);
        System.out.println("children:");
        for (int i = 0; i < person.children.size(); i++) {
            System.out.println(person.children.get(i));
        }
        System.out.printf("%n==================%n");
        person.writeExternal(oos);
//        oos.writeObject(person);
        oos.flush();
        oos.close();

        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("20-07-03.txt"));
        Person person1 = new Person();
        person1.readExternal(ois);
        System.out.println(person1.firstName);
        System.out.println(person1.lastName);
        System.out.println(person1.age);
        System.out.println(person1.mother);
        System.out.println(person1.father);
        System.out.println("children:");
        for (int i = 0; i < person1.children.size(); i++) {
            System.out.println(person1.children.get(i));
        }
        System.out.printf("============%n");
        System.out.println(person.equals(person1));
    }

    public static class Person implements Externalizable {
//        private static final long serialVersionUID = 1931198202215896041L;
        private String firstName;
        private String lastName;
        private int age;
        private Person mother;
        private Person father;
        private List<Person> children;

        public Person() {
            super();
        }

        public Person(String firstName, String lastName, int age) {
            this.firstName = firstName;
            this.lastName = lastName;
            this.age = age;
        }

        public void setMother(Person mother) {
            this.mother = mother;
        }

        public void setFather(Person father) {
            this.father = father;
        }

        public void setChildren(List<Person> children) {
            this.children = children;
        }

        @Override
        public void writeExternal(ObjectOutput out) throws IOException {
            out.writeObject(firstName);
            out.writeObject(lastName);
            out.writeInt(age);
            out.writeObject(mother);
            out.writeObject(father);
            out.writeObject(children);
        }

        @Override
        public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
            firstName = (String) in.readObject();
            lastName = (String) in.readObject();
            age = in.readInt();
            mother = (Person)in.readObject();
            father = (Person)in.readObject();
            children = (List<Person>)in.readObject();
        }
    }
}