package javaschool.app;

import asg.cliche.Command;
import asg.cliche.Shell;
import asg.cliche.ShellDependent;
import asg.cliche.ShellFactory;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class PhoneBook implements ShellDependent {

    private List<Record> list = new ArrayList<>();
    private Shell theShell;

    @Override
    public void cliSetShell(Shell theShell) {
        this.theShell = theShell;
    }

    @Command(description = "Add a new person")
    public void addPerson(String name) {
        Record record = new Person(name);
        list.add(record);
    }

    @Command(description = "Add a new note")
    public void addNote(String name) {
        Record record = new Note(name);
        list.add(record);
    }

    @Command(description = "Edit a record by id")
    public void edit(Integer id) throws IOException {
        Record record = lookup(id);
        if (record == null) {
            System.out.printf("Record with id \"%d\" not found.\n", id);
        } else {
            ShellFactory.createSubshell(record.getName(), theShell, "Editing " + record.getName(), record)
                    .commandLoop();
        }
    }

    @Command
    public List<Record> list() {
        return list;
    }

    @Command
    public void clear() {
        list.clear();
    }


    @Command(description = "Search in records")
    public List<Record> search(String criteria) {
        return list.stream().filter((r) -> r.contains(criteria.toLowerCase())).collect(Collectors.toList());
    }

    private Record lookup(Integer id) {
        for (Record record : list) {
            if (record.getId().equals(id)) {
                return record;
            }
        }
        return null;
    }
}

@@ -222,24 +222,52 @@
 util.List;%0D%0A
+import java.util.Optional;%0D%0A
 import java.
@@ -638,66 +638,33 @@
     
-Record record = new Person(name);%0D%0A        list.add(record
+list.add(new Person(name)
 );%0D%0A
@@ -770,64 +770,31 @@
     
-Record record = new Note(name);%0D%0A        list.add(record
+list.add(new Note(name)
 );%0D%0A
@@ -916,22 +916,32 @@
         
+Optional%3C
 Record
+%3E
  record 
@@ -977,109 +977,21 @@
 cord
- == null) %7B%0D%0A            System.out.printf(%22Record with id %5C%22%25d%5C%22 not found.%5Cn%22, id);%0D%0A        %7D else
+.isPresent())
  %7B%0D%0A
@@ -1033,24 +1033,30 @@
 hell(record.
+get().
 getName(), t
@@ -1084,16 +1084,22 @@
  record.
+get().
 getName(
@@ -1143,24 +1143,116 @@
 andLoop();%0D%0A
+        %7D else %7B%0D%0A            System.out.printf(%22Record with id %5C%22%25d%5C%22 not found.%5Cn%22, id);%0D%0A
         %7D%0D%0A 
@@ -1411,18 +1411,16 @@
    %7D%0D%0A%0D%0A
-%0D%0A
     @Com
@@ -1642,22 +1642,32 @@
 private 
+Optional%3C
 Record
+%3E
  lookup(
@@ -1693,161 +1693,76 @@
     
-for (Record record : list) %7B%0D%0A            if (record.getId().equals(id)) %7B%0D%0A                return record;%0D%0A            %7D%0D%0A        %7D%0D%0A        return null
+return list.stream().filter((r) -%3E r.getId().equals(id)).findFirst()
 ;%0D%0A 

lookup should return Optional