package guitests;

import javafx.scene.control.Label;
import javafx.scene.input.KeyCode;
import org.junit.Test;
import ui.UI;
import ui.listpanel.ListPanel;
import util.PlatformEx;
import util.events.testevents.UILogicRefreshEvent;
import util.events.testevents.UpdateDummyRepoEvent;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

import static org.junit.Assert.assertEquals;

public class UpdateIssuesTest extends UITest {

    private static final int EVENT_DELAY = 500;

    @Test
    public void updateIssues() throws InterruptedException, ExecutionException {
        Label apiBox = find("#apiBox");
        resetRepo();

        click("#dummy/dummy_col0_filterTextField");
        type("updated");
        press(KeyCode.SHIFT).press(KeyCode.SEMICOLON).release(KeyCode.SEMICOLON).release(KeyCode.SHIFT);
        type("24");
        push(KeyCode.ENTER);
        sleep(EVENT_DELAY);

        // Updated view should contain Issue 10, which was commented on recently (as part of default test dataset)
        // Issue 9 was also commented on recently, but by the current HT user, so it is not shown.
        assertEquals(3496, getApiCount(apiBox.getText())); // 2 calls for Issue 10, 2 calls for Issue 9
        assertEquals(1, countIssuesShown());

        // After updating, issue with ID 5 should have title Issue 5.1
        updateIssue(5, "Issue 5.1");
        click("#dummy/dummy_col0_filterTextField");
        push(KeyCode.ENTER);
        sleep(EVENT_DELAY);

        // Updated view should now contain Issue 5.1 and Issue 10.
        assertEquals(3494, getApiCount(apiBox.getText())); // 2 calls for Issue 5
        assertEquals(2, countIssuesShown());

        // Then have a non-self comment for Issue 9.
        UI.events.triggerEvent(UpdateDummyRepoEvent.addComment("dummy/dummy", 9, "Test comment", "test-nonself"));
        UI.events.triggerEvent(new UILogicRefreshEvent());
        click("#dummy/dummy_col0_filterTextField");
        push(KeyCode.ENTER);
        sleep(EVENT_DELAY);
        assertEquals(3492, getApiCount(apiBox.getText())); // 2 calls for Issue 9
        assertEquals(3, countIssuesShown());

        click("#dummy/dummy_col0_filterTextField");
        push(KeyCode.ENTER);
        sleep(EVENT_DELAY);
        assertEquals(3492, getApiCount(apiBox.getText())); // No change to issues, so no additional API quota spent
    }

    public void resetRepo() {
        UI.events.triggerEvent(UpdateDummyRepoEvent.resetRepo("dummy/dummy"));
        sleep(EVENT_DELAY);
    }

    public void updateIssue(int issueId, String newIssueTitle) {
        UI.events.triggerEvent(UpdateDummyRepoEvent.updateIssue("dummy/dummy", issueId, newIssueTitle));
        UI.events.triggerEvent(new UILogicRefreshEvent());
        sleep(EVENT_DELAY);
    }

    @SuppressWarnings("unchecked")
    public int countIssuesShown() throws InterruptedException, ExecutionException {
        FutureTask countIssues = new FutureTask(((ListPanel) find("#dummy/dummy_col0"))::getIssueCount);
        PlatformEx.runAndWait(countIssues);
        return (int) countIssues.get();
    }

    private int getApiCount(String apiBoxText) {
        return Integer.parseInt(apiBoxText.substring(0, apiBoxText.indexOf("/")));
    }
}

@@ -902,33 +902,42 @@
         
-sleep(EVENT_DELAY
+PlatformEx.waitOnFxThread(
 );%0A%0A    
@@ -1493,33 +1493,42 @@
         
-sleep(EVENT_DELAY
+PlatformEx.waitOnFxThread(
 );%0A%0A    
@@ -2034,33 +2034,42 @@
         
-sleep(EVENT_DELAY
+PlatformEx.waitOnFxThread(
 );%0A     
@@ -2280,33 +2280,42 @@
         
-sleep(EVENT_DELAY
+PlatformEx.waitOnFxThread(
 );%0A     
@@ -2549,33 +2549,42 @@
         
-sleep(EVENT_DELAY
+PlatformEx.waitOnFxThread(
 );%0A    %7D
@@ -2826,25 +2826,34 @@
     
-sleep(EVENT_DELAY
+PlatformEx.waitOnFxThread(
 );%0A 

Change sleep(EVENT_DELAY) to waitOnFxThread