package ru.job4j.tracker;

import java.text.SimpleDateFormat;

/**.
 * Class StartUI entry point
 * @author Mikhail Vlasov
 * @since 6.4.2017
 * @version 1
 */
public class StartUI {
    /**.
     * Variable for Interface
     */
    private Input input;
    /**.
     * Variable for Tracker
     */
    private Tracker tracker;
    /**.
     * Constructor for StartUI class.
     * @param input interface.
     */
    public StartUI(Input input) {
        this.input = input;
        this.tracker = new Tracker();
    }

    /**.
     * Entry point.
     * @param args string array
     */
    public static void main(String[] args) {
        /**.
         * ConsoleInput class object.
         */
        Input input = new ConsoleInput();
        /**.
         * Starts questionnaire function.
         */
        new StartUI(input).init();
    }

    /**.
     * Main menu
     */
    public void init() {
        /**.
         * Simple date and time of item creation pattern.
         */
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z");
        /**.
         * Main menu string
         */
        String question = "0. Add new Item" + System.lineSeparator()
                        + "1. Show all items" + System.lineSeparator()
                        + "2. Edit item" + System.lineSeparator()
                        + "3. Delete item" + System.lineSeparator()
                        + "4. Find item by Id" + System.lineSeparator()
                        + "5. Find items by name" + System.lineSeparator()
                        + "6. Exit Program" + System.lineSeparator();
        /**.
         * Contains user's choice.
         */
        String choise = input.ask(question);

        while (!"6".equals(choise)) {
            if ("0".equals(choise)) {
                String name = input.ask("Enter name: ");
                String description = input.ask("Enter description: ");
                tracker.add(new Item(name, description));

            } else if ("1".equals(choise)) {
                Item[] items = tracker.findAll();
                if (items == null) {
                    System.out.println("No tasks available.");
                } else {
                    for (Item item : items) {
                        System.out.println(String.format("Name of task: %s"
                                        + System.lineSeparator()
                                        + "Description: %s"
                                        + System.lineSeparator()
                                        + "ID: %s"
                                        + System.lineSeparator()
                                        + "Date of creation: %s",
                                item.getName(), item.getDescription(), item.getId(), simpleDateFormat.format(item.getCreated())
                        ));
                        System.lineSeparator();
                    }
                }

            } else if ("2".equals(choise)) {
                String id = input.ask("Enter id of the task you want to edit: ");
                Item item = tracker.findById(id);

                if (item == null) {
                    System.out.println("No task found");

                } else {
                    System.out.println(String.format("Name of task: %s"
                                                    + System.lineSeparator()
                                                    + "Description: %s"
                                                    + System.lineSeparator()
                                                    + "ID: %s" + System.lineSeparator()
                                                    + "Date of creation: %s",
                            item.getName(), item.getDescription(), item.getId(), simpleDateFormat.format(item.getCreated())
                    ));
                    String desc = input.ask("Enter new description: ");
                    item.setDescription(desc);
                    tracker.update(item);
                }

            } else if ("3".equals(choise)) {
                String id = input.ask("Enter id of the task you want to edit: ");
                Item item = tracker.findById(id);

                if (item == null) {
                    System.out.println("No task found");

                } else {
                    tracker.delete(item);
                    System.out.println("The task was deleted.");
                }

            } else if ("4".equals(choise)) {
                String id = input.ask("Enter id of the task you want to find: ");
                Item item = tracker.findById(id);

                if (item == null) {
                    System.out.println("No task found");

                } else {
                    System.out.println(String.format("Name of task: %s"
                                                    + System.lineSeparator()
                                                    + "Description: %s" + System.lineSeparator()
                                                    + "ID: %s" + System.lineSeparator()
                                                    + "Date of creation: %s",
                            item.getName(), item.getDescription(), item.getId(), simpleDateFormat.format(item.getCreated())
                    ));
                }

            } else if ("5".equals(choise)) {
                String name = input.ask("Enter name: ");
                Item[] items = tracker.findByName(name);

                if (items == null) {
                    System.out.println("No task found");

                } else {
                    for (Item item : items) {
                        System.out.println(String.format("Name of task: %s"
                                                        + System.lineSeparator()
                                                        + "Description: %s" + System.lineSeparator()
                                                        + "ID: %s"
                                                        + System.lineSeparator()
                                                        + "Date of creation: %s",
                                item.getName(), item.getDescription(), item.getId(), simpleDateFormat.format(item.getCreated())
                        ));
                    }
                }
            }
            choise = input.ask(question);
        }
    }
}

@@ -327,24 +327,172 @@
 r;%0A    /**.%0A
+     * Constants for menu choice.%0A     */%0A    private static final int ZERO = 0, ONE = 1, TWO = 2, THREE = 3, FOUR = 4, FIVE = 5, SIX = 6;%0A    /**.%0A
      * Const
@@ -1840,24 +1840,38 @@
     
-Str
 in
-g
+t
  choi
-s
+c
 e = 
+Integer.parseInt(
 inpu
@@ -1885,16 +1885,17 @@
 uestion)
+)
 ;%0A%0A     
@@ -1908,27 +1908,21 @@
 le (
-!%226%22.equals(
+SIX != 
 choi
-se)
+ce
 ) %7B%0A
@@ -1941,26 +1941,22 @@
 if (
-%220%22.equals(
+ZERO == 
 choi
-se)
+ce
 ) %7B%0A
@@ -2169,26 +2169,21 @@
 if (
-%221%22.equals(
+ONE == 
 choi
-se)
+ce
 ) %7B%0A
@@ -3123,26 +3123,21 @@
 if (
-%222%22.equals(
+TWO == 
 choi
-se)
+ce
 ) %7B%0A
@@ -3740,32 +3740,84 @@
       + %22ID: %25s%22
+%0A                                                   
  + System.lineSe
@@ -3894,32 +3894,109 @@
 of creation: %25s%22
+%0A                                                    + System.lineSeparator()
 ,%0A              
@@ -4336,26 +4336,23 @@
 if (
-%223%22.equals(
+THREE == 
 choi
-se)
+ce
 ) %7B%0A
@@ -4756,26 +4756,22 @@
 if (
-%224%22.equals(
+FOUR == 
 choi
-se)
+ce
 ) %7B%0A
@@ -5628,26 +5628,22 @@
 if (
-%225%22.equals(
+FIVE == 
 choi
-se)
+ce
 ) %7B%0A
@@ -6641,13 +6641,30 @@
 choi
-s
+c
 e = 
+Integer.parseInt(
 inpu
@@ -6678,16 +6678,17 @@
 uestion)
+)
 ;%0A      

Task 784