package it.com.atlassian.stash;

import com.atlassian.bitbucket.pull.PullRequestState;
import com.atlassian.bitbucket.test.BaseFuncTest;
import com.atlassian.bitbucket.test.DefaultFuncTestData;
import com.atlassian.bitbucket.test.PullRequestTestHelper;
import com.atlassian.pageobjects.TestedProductFactory;
import com.atlassian.pageweight.WebDriverResourceAnalyser;
import com.atlassian.pageweight.WebDriverResourceResult;
import com.atlassian.webdriver.bitbucket.BitbucketTestedProduct;
import com.atlassian.webdriver.bitbucket.page.*;
import org.junit.Before;
import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * This class downloads all referenced resources on a page. This allows each page's
 * resource weight to be tracked and (hopefully) reduced.
 */
public final class PageWeightAnalyserTest extends BaseFuncTest {

    protected static final BitbucketTestedProduct BITBUCKET = TestedProductFactory.create(BitbucketTestedProduct.class);

    private static final String RESULTS_PATH = "target/pageweight-results/";

    private PullRequestTestHelper prTestHelper;

    @Before
    public void before() throws Exception {
        prTestHelper = testContext.pullRequest(new PullRequestTestHelper.Builder(
                DefaultFuncTestData.getAdminUser(),
                DefaultFuncTestData.getAdminPassword(),
                "Page Weight PR", "This is the description",
                DefaultFuncTestData.getProject1(),
                    DefaultFuncTestData.getProject1Repository1(),
                    DefaultFuncTestData.getProject1Repository1BasicBranchingBranch(),
                DefaultFuncTestData.getProject1(),
                    DefaultFuncTestData.getProject1Repository1(),
                    DefaultFuncTestData.getProject1Repository1MasterBranch()));
    }

    @Test
    public void getPageWeights() throws IOException {

        new File(RESULTS_PATH).mkdirs();

        try (PrintWriter overallResults = new PrintWriter(RESULTS_PATH + "overall.csv")) {
            overallResults.println("Page,JS Size,JS Gzipped Size,CSS Size,CSS Gzipped Size");

            BITBUCKET.visit(BitbucketLoginPage.class);
            analyseAndSave("loginPage", overallResults);

            BITBUCKET.visit(BitbucketLoginPage.class).loginAsSysAdmin(ProjectListPage.class);
            analyseAndSave("projectListPage", overallResults);

            BITBUCKET.visit(ProjectOverviewPage.class, DefaultFuncTestData.getProject1());
            analyseAndSave("projectOverviewPage", overallResults);

            BITBUCKET.visit(PullRequestListPage.class,
                    DefaultFuncTestData.getProject1(),
                    DefaultFuncTestData.getProject1Repository1());
            analyseAndSave("pullRequestListPage", overallResults);

            BITBUCKET.visit(CommitListPage.class,
                    DefaultFuncTestData.getProject1(),
                    DefaultFuncTestData.getProject1Repository1());
            analyseAndSave("commitListPage", overallResults);

            BITBUCKET.visit(PullRequestDiffPage.class,
                    DefaultFuncTestData.getProject1(),
                    DefaultFuncTestData.getProject1Repository1(),
                    prTestHelper.getId());
            analyseAndSave("viewPullRequestPage", overallResults);
        }
    }

    private void analyseAndSave(String page, PrintWriter resultsOutput) throws IOException {
        WebDriverResourceAnalyser analyser = new WebDriverResourceAnalyser(BITBUCKET.getTester().getDriver(), new File(RESULTS_PATH, page));
        WebDriverResourceResult result = analyser.analyseResources();

        try (PrintWriter p = new PrintWriter(RESULTS_PATH + page + ".csv")) {
            result.saveCsv(p);
        }

        resultsOutput.println(page + "," + result.getTotals());
    }
}
