package it.com.atlassian.stash.visual;

import com.atlassian.bitbucket.test.DatabaseTestHelper;
import com.atlassian.bitbucket.test.DefaultFuncTestData;
import com.atlassian.bitbucket.test.SetupTestHelper;
import com.atlassian.bitbucket.test.StashTestLicense;
import com.atlassian.selenium.visualcomparison.utils.BoundingBox;
import com.atlassian.webdriver.bitbucket.page.LogoutPage;
import com.atlassian.webdriver.bitbucket.page.BitbucketLoginPage;
import com.atlassian.webdriver.bitbucket.page.admin.AdminHomePage;
import com.atlassian.webdriver.bitbucket.page.setup.SetupAdminUserPage;
import com.atlassian.webdriver.bitbucket.page.setup.SetupDatabasePage;
import com.atlassian.webdriver.bitbucket.page.setup.SetupJiraIntegrationPage;
import com.atlassian.webdriver.bitbucket.page.setup.SetupSettingsPage;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;

import static com.google.common.base.Preconditions.checkState;

public class VisualRegressionSetupTest extends BaseVisualRegressionTest {

    /** 1 Hour timebomb license for 2 users */
    private static final StashTestLicense LICENSE = StashTestLicense.builder()
        .whenNonClustered(
            "AAABHg0ODAoPeNptkEFrwkAQhe/+ioWeI0mMhwoL1RipYEwwtgVv0+2kWYi7YWYj9d9X43qw9Pref\n" +
            "O/NzNMHfokcSESJiKazcDKbPous2os4jOIRO+BmnJ2g7cFpa2QNLaOXl8iKdDfoe2QnWq3QMIrak\n" +
            "nANigpd34lVb5S4+ixsLaor6gNSwiF1CQ7ltS8IJ0GU3F1rHCi3hSPKUiPRvTcHbRwaMAqzn07Te\n" +
            "eDL6NX7F0ifUDrq8TEqu5Ct7LCzmqx7AdcCswYzVvboJze3E/bnDofitMjzbJeu55tRhXRCWi/lI\n" +
            "jmsgrDK1kG6WxyC92I79XQJ5AzSQHqpoG8wmm/fm98bvbntj59IRf3GSCzjxx3+va3sSTXA+Pdlv\n" +
            "zUInXUwLQIVAJPALS44YIOLoi1gP9UzA6y0IU1rAhQCZCUupoxQuFvWoD1nt9f2QUGQ+g==X02ei")
        .build();

    // Ignore areas
    // BoundingBox instances are mutable, but these *should not* be manipulated
    private static final BoundingBox DATABASE_NAME_AREA = new BoundingBox(381, 511, 631, 541);
    private static final BoundingBox SERVER_ID_AREA = new BoundingBox(274, 341, 483, 371);

    @Before
    public void setUp() throws Exception {
        checkState(!SetupTestHelper.isSetup(), "This test requires an instance which is not setup yet.");
    }

    @Test
    public void testSetupScreens() throws Exception {
        MultipleUIMatcher errors = new MultipleUIMatcher();
        SetupDatabasePage databasePage = STASH.visit(SetupDatabasePage.class);

        // Setup external database form
        databasePage = databasePage.clickExternal().selectMySQL()
                        .enterHost(DatabaseTestHelper.getTestDatabaseHost())
                        .enterPort(DatabaseTestHelper.getTestDatabasePort())
                        .enterDatabaseName(DatabaseTestHelper.getTestDatabaseName())
                        .enterUsername(DatabaseTestHelper.getTestDatabaseUser())
                        .enterPassword(DatabaseTestHelper.getTestDatabasePassword());
        resetVisualComparer();
        visualComparer.getIgnoreAreas().add(DATABASE_NAME_AREA); // ignore the database name which changes with every run on Bamboo
        errors.assertUIMatches("setup-database-form");

        // Setup settings form
        SetupSettingsPage setupSettingsPage = databasePage.clickNext();
        resetVisualComparer(By.id("baseUrl"));
        visualComparer.getIgnoreAreas().add(SERVER_ID_AREA); // ignore the serverID
        errors.assertUIMatches("setup-settings-form");

        // Setup admin user form
        setupSettingsPage.enterBaseURL(DefaultFuncTestData.getBaseURL());
        SetupAdminUserPage setupAdminPage = setupSettingsPage.enterLicense(LICENSE.get()).clickNext();
        resetVisualComparer();
        errors.assertUIMatches("setup-admin-form");

        // enter correct info
        SetupJiraIntegrationPage setupJiraIntegrationPage = setupAdminPage
                .setUsername(DefaultFuncTestData.getAdminUser())
                .setFullname("Admin Setup Test")
                .setPassword(DefaultFuncTestData.getAdminPassword())
                .setConfirmPassword(DefaultFuncTestData.getAdminPassword())
                .setEmail("admin-setup@example.com")
                .setupJira();
        resetVisualComparer(By.id("stashBaseUrl"));
        errors.assertUIMatches("setup-jira-integration-form");

        // We just wanted to take a screenshot of form, so skip JIRA integration and go to the login page
        BitbucketLoginPage loginPage = setupJiraIntegrationPage.skipJiraSuccess();
        resetVisualComparer();
        removeStpBanner();
        errors.assertUIMatches("login-page");

         // check that we can login with the admin we just created
        loginPage.login(DefaultFuncTestData.getAdminUser(), DefaultFuncTestData.getAdminPassword(), AdminHomePage.class);
        resetVisualComparer();
        errors.assertUIMatches("admin-overview");

        // check the logout page
        STASH.visit(LogoutPage.class);
        resetVisualComparer();
        errors.assertUIMatches("logout-page");

        errors.assertNoError();
    }
}
