package com.coronaide.test.ui;

import java.io.IOException;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import javax.inject.Inject;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import com.coronaide.core.model.Project;
import com.coronaide.core.model.ProjectRequest;
import com.coronaide.core.service.IProjectService;
import com.coronaide.ui.CoronaUIApplication;

import javafx.scene.control.ListView;
import javafx.scene.input.MouseButton;

public class ProjectDeleteTest extends CoronaUITest {

    @Inject
    private IProjectService projectService;

    private List<Project> testProjects = new ArrayList<>();

    @Before
    public void setup() throws Exception {
        testProjects.add(projectService.create(new ProjectRequest(Paths.get("testProject"))));
        testProjects.add(projectService.create(new ProjectRequest(Paths.get("testProject2"))));
        launch(CoronaUIApplication.class);
    }

    @Test
    public void projectRemoveTest() {
        ListView<Project> listViewProjects = lookup("#listViewProjects").query();
        clickOn("testProject", MouseButton.SECONDARY).clickOn("Delete").clickOn("Yes");
        listViewProjects = lookup("#listViewProjects").query();
        Assert.assertTrue("Expected project list to not contain project which was deleted.",
                listViewProjects.getItems().stream()
                        .filter(p -> p.getName().equals("testProject"))
                        .collect(Collectors.toList()).isEmpty());
    }

    @Test
    public void projectDeleteTest() {
        ListView<Project> listViewProjects = lookup("#listViewProjects").query();
        clickOn("testProject2", MouseButton.SECONDARY).clickOn("Delete").clickOn("#alertCheckbox").clickOn("Yes");
        listViewProjects = lookup("#listViewProjects").query();
        Assert.assertTrue("Expected project list to not contain project which was deleted.",
                listViewProjects.getItems().stream()
                        .filter(p -> p.getName().equals("testProject2"))
                        .collect(Collectors.toList()).isEmpty());
    }

    @After
    public void teardown() throws IOException {
        for (Project t : testProjects) {
            try {
                projectService.delete(t);
            } catch (IllegalArgumentException e) {
                // don't worry about deleting projects that we already deleted in the test
            }
        }
    }

}

@@ -1151,32 +1151,299 @@
 ects%22).query();%0A
+        Assert.assertTrue(%22Expected project list to contain project which will be deleted.%22,%0A                listViewProjects.getItems().stream()%0A                        .filter(p -%3E p.getName().equals(%22testProject%22))%0A                        .findAny().isPresent());%0A
         clickOn(
@@ -1736,32 +1736,24 @@
             
- 
-       
 .filter(p -%3E
@@ -1784,32 +1784,24 @@
 tProject%22))%0A
-        
             
@@ -1975,32 +1975,284 @@
 ects%22).query();%0A
+        Assert.assertTrue(%22Expected project list to contain project which will be deleted.%22,%0A                listViewProjects.getItems().stream()%0A                .filter(p -%3E p.getName().equals(%22testProject2%22))%0A                .findAny().isPresent());%0A
         clickOn(
@@ -2572,32 +2572,24 @@
             
-        
 .filter(p -%3E
@@ -2621,32 +2621,24 @@
 Project2%22))%0A
-        
             

Determine if the delete tests are passing legitimately or not