package it.com.atlassian.stash.admin;

import com.atlassian.pageobjects.elements.query.Poller;
import com.atlassian.webdriver.bitbucket.element.AppSwitcherDropdown;
import com.atlassian.webdriver.bitbucket.element.AppSwitcherDropdownItems;
import com.atlassian.webdriver.bitbucket.page.admin.AdminHomePage;
import com.atlassian.webdriver.bitbucket.page.admin.NavLinksPage;
import it.com.atlassian.stash.BaseBrowserFuncTest;
import org.junit.Test;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

public class NavLinksTest extends BaseBrowserFuncTest {

    private static final String NAV_LINK_NAME = "TEST_NAV_LINK_NAME!";

    @Test
    public void testNavLinks() {
        NavLinksPage navLinksPage = loginAsAdmin(NavLinksPage.class);

        String url = BITBUCKET.getProductInstance().getBaseUrl() + AdminHomePage.URL_SUFFIX;
        navLinksPage.addNavLink(NAV_LINK_NAME, url);

        assertTrue("Nav link not found in table", navLinksPage.hasNavLink(NAV_LINK_NAME));

        // reload the page to update the hotdog menu
        navLinksPage = loginAsAdmin(NavLinksPage.class);
        AppSwitcherDropdown appSwitcher = navLinksPage.getHeader().getAppSwitcher();
        Poller.waitUntilTrue("App Switcher did not show on reload", appSwitcher.isPresent());

        // test nav link
        AppSwitcherDropdownItems appSwitcherItems = appSwitcher.clickTrigger();
        int appSwitcherIndex = appSwitcherItems.getAppList().indexOf(NAV_LINK_NAME);

        assertFalse("Nav link not found in app switcher", appSwitcherIndex == -1);

        AdminHomePage adminPage = appSwitcherItems.clickLink(AdminHomePage.class, NAV_LINK_NAME);

        // clean up
        navLinksPage = adminPage.goToNavLinksPage();
        navLinksPage.deleteNavLink(NAV_LINK_NAME);

        assertFalse("Nav link found in table after deletion", navLinksPage.hasNavLink(NAV_LINK_NAME));
    }
}
