import javafx.scene.control.CheckBox;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.testfx.framework.junit5.ApplicationTest;

import javafx.application.Application;
import javafx.scene.control.Button;
import javafx.scene.control.Label;

public class FrontendDeveloperTests extends ApplicationTest {

    @BeforeEach
    public void setup() throws Exception {
        Frontend frontend = new Frontend();
        frontend.setBackend(new BackendPlaceholder(new GraphPlaceholder()));
        ApplicationTest.launch(Frontend.class);
    }

    @Test
    public void testButtonsExist() {

        Button submitButton = lookup("#searchButton").query();
        Assertions.assertEquals("Search Button", submitButton.getText());
        Button furthestFromButton = lookup("#furthestFromButton").query();
        Assertions.assertEquals("Find Most Distant Location", furthestFromButton.getText());
        Button about = lookup("#about").query();
        Assertions.assertEquals("About", about.getText());
        Button quit = lookup("#quit").query();
        Assertions.assertEquals("Quit", quit.getText());
    }

    @Test
    public void testLabelsExist() {
        Label srcLabel = lookup("#srcLabel").query();
        Assertions.assertEquals("Path Start Selector:", srcLabel.getText());
        Label dstLabel = lookup("#dstLabel").query();
        Assertions.assertEquals("Path End Selector:", dstLabel.getText());
        Label path = lookup("#path").query();
        Assertions.assertEquals("Results List:", path.getText());
        Label locationSelectorLabel = lookup("#locationLabel").query();
        Assertions.assertEquals("Location Selector:", locationSelectorLabel.getText());
        Label furthestFromLabel = lookup("#furthestFromLabel").query();
        Assertions.assertEquals("Most Distant Location:", furthestFromLabel.getText());
    }

    @Test
    public void testButtonsWork() {
        clickOn("#searchButton");
        Label path = lookup("#path").query();
        Assertions.assertTrue(path.getText().contains("Union South"));
        Assertions.assertTrue(path.getText().contains("Computer Sciences and Statistics"));
        Assertions.assertTrue(path.getText().contains("Atmospheric, Oceanic and Space Sciences"));

        clickOn("#furthestFromButton");
        Label furthestLocation = lookup("#furthestFromLabel").query();
        Assertions.assertTrue(furthestLocation.getText().contains("Atmospheric, Oceanic and Space Sciences"));
        Assertions.assertEquals(
                "Most Distant Location:\nAtmospheric, Oceanic and Space Sciences",
                furthestLocation.getText());
        Button about = lookup("#about").query();
        clickOn("#about");
        Label aboutLabel = lookup("#aboutLabel").query();
        Assertions.assertEquals("Here to help UW students who hate Google Maps!", aboutLabel.getText());
        Button quit = lookup("#quit").query();
        Assertions.assertEquals("Quit", quit.getText());
    }

    @Test
    public void testCheckBox() {
        CheckBox showTimesBox = lookup("#showTimesBox").query();
        Assertions.assertEquals("Show Walking Times", showTimesBox.getText());
        showTimesBox.fire();
        Assertions.assertTrue(showTimesBox.isSelected());
        clickOn("#searchButton");
        Label path = lookup("#path").query();
        Assertions.assertTrue(path.getText().contains("Results List (with walking times):"));
    }



}
