package com.atlassian.bitbucket.test;

import com.jayway.restassured.RestAssured;
import com.jayway.restassured.http.ContentType;
import net.sf.json.JSONObject;
import org.hamcrest.Matchers;

import static javax.ws.rs.core.Response.Status.OK;

public class BannerTestHelper {

    private static final int STATUS_OK = OK.getStatusCode();

    public static void dismissBanners(String baseUrl) {
        dismissAnalyticsPrivacyPolicyBanner(baseUrl);
        dismissNpsPrivacyPolicyBanner(baseUrl);
        dismissStpPluginBanner(baseUrl);
    }

    private static void dismissStpPluginBanner(String baseUrl) {
        RestAssured.given()
                .auth().preemptive()
                .basic(DefaultFuncTestData.getAdminUser(), DefaultFuncTestData.getAdminPassword())
                .header(DefaultFuncTestData.HDR_XSRF_NO_CHECK)
                .expect().statusCode(STATUS_OK).log().ifStatusCodeMatches(Matchers.not(STATUS_OK))
                .post(baseUrl + "/rest/stp/1.0/license/remindMeNever");
    }

    private static void dismissAnalyticsPrivacyPolicyBanner(String baseUrl) {
        RestAssured.given()
                .auth().preemptive()
                .basic(DefaultFuncTestData.getAdminUser(), DefaultFuncTestData.getAdminPassword())
                .expect().statusCode(STATUS_OK).log().ifStatusCodeMatches(Matchers.not(STATUS_OK))
                .put(baseUrl + "/rest/analytics/1.0/config/acknowledge");
    }

    private static void dismissNpsPrivacyPolicyBanner(String baseUrl) {
        String body = new JSONObject().accumulate("npsAcknowledged", true).toString();

        RestAssured.given()
                .auth().preemptive()
                .basic(DefaultFuncTestData.getAdminUser(), DefaultFuncTestData.getAdminPassword())
                .contentType(ContentType.JSON).body(body)
                .expect().statusCode(STATUS_OK).log().ifStatusCodeMatches(Matchers.not(STATUS_OK))
                .put(baseUrl + "/rest/nps/1.0/status/acknowledge");
    }
}
