package ru.matevosyan.start;

import org.junit.Test;
import ru.matevosyan.models.Comments;
import ru.matevosyan.models.Item;
import ru.matevosyan.models.Task;

import static junit.framework.TestCase.assertNull;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;

/**
 * Class StartUITest created for testing.
 * Created on 07.12.2016.
 * @since 1.0
 * @version 1.0
 * @author Matevosyan Vardan
 */

public class StartUITest {

    @Test
    public void whenCreateItemThenReturnItem() {

        String[] answers = {

                "1", // select menu 1. add item
                "task 1", // items name
                "task desc", // items desc
                "n" // exit
        };

        Tracker tracker = new Tracker();
        Input stub = new StubInput(answers);
        new StartUI(stub, tracker).init();
        Item item = tracker.findByName("task 1");
        assertThat(item, is(tracker.getAll()[0]));
    }

    @Test
    public void whenCreateItemThenEditTracker() {
        Tracker tracker = new Tracker();
        Item item = new Task("task 1", "task desc 1");
        tracker.add(item);
        String[] answers = {
                "2",
                item.getId(),
                "task 2",
                "task desc 2",
                "n" // exit
        };

        Input stub = new StubInput(answers);
        new StartUI(stub, tracker).init();

        for (Item items : tracker.getAll()){
            assertThat(items.getName(), is("task 2"));
        }


    }

    @Test
    public void whenCommentItemThenReturnItemComments() {

        Tracker tracker = new Tracker();
        Item item = new Task("task 1", "task desc 1");
        tracker.add(item);
        String[] answers = {
                "4",
                item.getId(),
                "comment1",
                "y",
                "4",
                item.getId(),
                "comment2",
                "y",
                "4",
                item.getId(),
                "comment3",
                "y",
                "4",
                item.getId(),
                "comment4",
                "y",
                "4",
                item.getId(),
                "comment5",
                "n"
        };

        Input stub = new StubInput(answers);
        new StartUI(stub, tracker).init();
        final int commentSize = 5;
        Comments[] expComment = new Comments[commentSize];
        expComment[0] = new Comments("comment1");
        expComment[1] = new Comments("comment2");
        expComment[2] = new Comments("comment3");
        expComment[3] = new Comments("comment4");
        expComment[4] = new Comments("comment5");
        assertNotNull(item.getComments()[3]);
        assertThat(item.getComments()[3].getCommentName(), is(expComment[3].getCommentName()));
    }

    @Test
    public void whenCreateItemThenFindByNameInTracker() {

        String[] answers = {

            "1", // select menu 1. add item
            "task 1", // items name
            "task desc", // items desc
            "n" // exit
        };

        Input stub = new StubInput(answers);
        Tracker tracker = new Tracker();
        new StartUI(stub, tracker).init();
        Item item = tracker.findByName("task 1");

        assertThat(item.getName(), is("task 1"));
    }

    @Test
    public void whenCreateItemThenFindByIdTracker() {

        String[] answer = {
                "1",
                "task 2",
                "task desc",
                "n"
        };

        Input stub = new StubInput(answer);
        Tracker tracker = new Tracker();
        new StartUI(stub, tracker).init();

        for (Item item : tracker.getAll()){
            item = tracker.findById(item.getId());

            assertThat(item, is(tracker.getAll()[0]));
        }


    }

    @Test
    public void whenCreateItemThenFindByDateTracker() {

        String[] answer = {
                "1",
                "task 3",
                "task desc",
                "n"
        };

        Input stub = new StubInput(answer);
        Tracker tracker = new Tracker();
        new StartUI(stub, tracker).init();

        for (Item item : tracker.getAll()){
            item = tracker.findByDate(item.getCreate());

            assertThat(item, is(tracker.getAll()[0]));
        }


    }

    @Test
    public void whenCreateItemThenDeleteItem() {

        Tracker tracker = new Tracker();
        Item item = new Task("task 1", "task desc 1");
        Item item2 = new Task("task 2", "task desc 2");
        tracker.add(item);
        tracker.add(item2);

        String[] answer = {
                "3",
                item.getId(),
                "n"
        };

        Input stub = new StubInput(answer);
        new StartUI(stub, tracker).init();
        assertThat(item2,is(tracker.getAll()[0]));
        assertNull(tracker.getAll()[1]);
        assertNotNull(tracker.getAll()[0]);
    }

    @Test
    public void whenCreateItemThenAllItem() {

        String[] answer = {
                "1",
                "task 3",
                "task desc 3",
                "y",
                "1",
                "task 4",
                "task desc 4",
                "n"
        };

        Input stub = new StubInput(answer);
        Tracker tracker = new Tracker();
        new StartUI(stub, tracker).init();

    }

}
@@ -153,16 +153,108 @@
 .Task;%0A%0A
+import java.io.ByteArrayOutputStream;%0Aimport java.io.PrintStream;%0Aimport java.util.Arrays;%0A%0A
 import s
@@ -5125,16 +5125,19 @@
 ItemThen
+Get
 AllItem(
@@ -5133,32 +5133,184 @@
 GetAllItem() %7B%0A%0A
+        Tracker tracker = new Tracker();%0A        ByteArrayOutputStream out = new ByteArrayOutputStream();%0A        System.setOut(new PrintStream(out));%0A%0A
         String%5B%5D
@@ -5441,66 +5441,9 @@
    %22
-1%22,%0A                %22task 4%22,%0A                %22task desc 4
+8
 %22,%0A 
@@ -5521,49 +5521,8 @@
 r);%0A
-        Tracker tracker = new Tracker();%0A
     
@@ -5556,21 +5556,449 @@
 er).init();%0A
+        Item%5B%5D item = tracker.getAll();%0A        String s = System.getProperty(%22line.separator%22);%0A%0A        assertThat(out.toString(), is(%22___M_E_N_U___ %22 + s + %221. Add Item %22 + s + %222. Edit Item %22 + s +%0A                %223. Remove Item %22 + s + %224. Add comment%22 + s + %225. Find by id %22 + s + %226. Find by name %22 +%0A                s + %227. Find by date %22 + s + %228. Get all items%22 + s + %229. Exit%22 + s + s +  Arrays.toString(item) + s));
 %0A    %7D%0A%0A%7D

edit StartUITest.java