package guitests;

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 {
        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(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(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(3, countIssuesShown());
    }

    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();
    }
}

@@ -12,16 +12,51 @@
 tests;%0A%0A
+import javafx.scene.control.Label;%0A
 import j
@@ -593,32 +593,72 @@
 tionException %7B%0A
+        Label apiBox = find(%22#apiBox%22);%0A
         resetRep
@@ -1137,16 +1137,120 @@
  shown.%0A
+        assertEquals(3496, getApiCount(apiBox.getText())); // 2 calls for Issue 10, 2 calls for Issue 9%0A
         
@@ -1572,16 +1572,98 @@
 sue 10.%0A
+        assertEquals(3494, getApiCount(apiBox.getText())); // 2 calls for Issue 5%0A
         
@@ -2058,38 +2058,346 @@
 Equals(3
-, countIssuesShown());
+492, getApiCount(apiBox.getText())); // 2 calls for Issue 9%0A        assertEquals(3, countIssuesShown());%0A%0A        click(%22#dummy/dummy_col0_filterTextField%22);%0A        push(KeyCode.ENTER);%0A        sleep(EVENT_DELAY);%0A        assertEquals(3492, getApiCount(apiBox.getText())); // No change to issues, so no additional API quota spent
 %0A    %7D%0A%0A
@@ -3117,11 +3117,150 @@
 );%0A    %7D
+%0A%0A    private int getApiCount(String apiBoxText) %7B%0A        return Integer.parseInt(apiBoxText.substring(0, apiBoxText.indexOf(%22/%22)));%0A    %7D
 %0A%7D%0A

Add regression tests for issue ETag use