package ru.yandex.autotests.direct.api.ads.archive;

import java.util.Arrays;
import java.util.Collection;

import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

import ru.yandex.aqua.annotations.project.Aqua;
import ru.yandex.autotests.direct.api.ads.AdsFeatures;
import ru.yandex.autotests.direct.api.ads.AdsLogins;
import ru.yandex.autotests.direct.utils.model.ShardNumbers;
import ru.yandex.autotests.directapi.apiclient.config.Semaphore;
import ru.yandex.autotests.directapi.apiclient.errors.Api5ErrorDetails;
import ru.yandex.autotests.directapi.model.api5.general.ExpectedResult;
import ru.yandex.autotests.directapi.model.api5.general.IdsCriteriaMap;
import ru.yandex.autotests.directapi.model.api5.general.Notification;
import ru.yandex.autotests.directapi.rules.ApiSteps;
import ru.yandex.autotests.directapi.rules.Trashman;
import ru.yandex.qatools.allure.annotations.Description;
import ru.yandex.qatools.allure.annotations.Features;
import ru.yandex.qatools.allure.annotations.Issue;
import ru.yandex.qatools.hazelcast.SemaphoreRule;

/**
 * Created by semkagtn on 16.04.15.
 * https://st.yandex-team.ru/TESTIRT-5211
 */
@Aqua.Test
@Features(AdsFeatures.ARCHIVE)
@Description("Проверка прав доступа агентства к объявлению при его архивации")
@Issue("https://st.yandex-team.ru/DIRECT-34573")
@RunWith(Parameterized.class)
public class AgencyCheckRightsToIdTest {

    @ClassRule
    public static ApiSteps api = new ApiSteps();

    @ClassRule
    public static SemaphoreRule semaphore = Semaphore.getSemaphore();

    @Rule
    public Trashman trasher = new Trashman(api);

    private static String client = AdsLogins.CLIENT;
    private static String agency = AdsLogins.AGENCY;
    private static String agencyRep = AdsLogins.AGENCY_REP;
    private static String agencyRepCl = AdsLogins.AGENCY_REP_CL;
    private static String subclient = AdsLogins.SUBCLIENT;
    private static String subclientElse = AdsLogins.SUBCLIENT_ELSE;

    private static Long clientAdId;
    private static Long subclientAdId;
    private static Long subclientElseAdId;

    @Parameterized.Parameter(0)
    public String loginForAuthorization;

    @Parameterized.Parameters(name = "loginForAuthorization = {0}")
    public static Collection testData() {
        Object[][] data = new Object[][]{
                {agency},
                {agencyRep},
                {agencyRepCl}
        };
        return Arrays.asList(data);
    }

    @BeforeClass
    public static void createNotMineCampaign() {
        api.userSteps.clientFakeSteps().fakeClearClientSpentUnits(client);
        api.userSteps.clientFakeSteps().fakeClearClientSpentUnits(agency);
        api.userSteps.clientFakeSteps().fakeClearClientSpentUnits(subclient);
        api.userSteps.clientFakeSteps().reshardUserWithLock(subclient, ShardNumbers.DEFAULT_SHARD);
        api.userSteps.clientFakeSteps().reshardUserWithLock(subclientElse, ShardNumbers.DEFAULT_SHARD);
        api.as(client);
        Long clientCampaignId = api.userSteps.campaignSteps().addDefaultTextCampaign(client);
        Long clientAdGroupId = api.userSteps.adGroupsSteps().addDefaultGroup(clientCampaignId);
        clientAdId = api.userSteps.adsSteps().addDefaultTextAd(clientAdGroupId);
        api.as(agency);
        Long subclientCampaignId = api.userSteps.campaignSteps().addDefaultTextCampaign(subclient);
        Long subclientAdGroupId = api.userSteps.adGroupsSteps().addDefaultGroup(subclientCampaignId, subclient);
        subclientAdId = api.userSteps.adsSteps().addDefaultTextAd(subclientAdGroupId, subclient);
        Long subclientElseCampaignId = api.userSteps.campaignSteps().addDefaultTextCampaign(subclientElse);
        Long subclientElseAdGroupId =
                api.userSteps.adGroupsSteps().addDefaultGroup(subclientElseCampaignId, subclientElse);
        subclientElseAdId = api.userSteps.adsSteps().addDefaultTextAd(subclientElseAdGroupId, subclientElse);
        api.userSteps.bannersFakeSteps().makeBannersModerated(
                clientAdId, subclientAdId, subclientElseAdId);
    }

    @Before
    public void unarchiveAds() {
        api.as(loginForAuthorization);
        api.userSteps.adsSteps().adsUnarchive(subclient, clientAdId, subclientAdId, subclientElseAdId);
    }

    @Test
    public void callArchiveWithSubclientId() {
        api.userSteps.bannersFakeSteps().makeBannersModerated(subclientAdId);
        api.userSteps.adsSteps().shouldGetResultOnArchive(
                new IdsCriteriaMap().withIds(subclientAdId),
                subclient,
                ExpectedResult.success()
        );
    }

    @Test
    public void callArchiveWithSubclientElseId() {
        api.userSteps.adsSteps().shouldGetResultOnArchive(
                new IdsCriteriaMap().withIds(subclientElseAdId),
                subclient,
                ExpectedResult.errors(new Notification(8800, Api5ErrorDetails.AD_NOT_FOUND))
        );
    }

    @Test
    public void callArchiveWithIdsOfBothSubclients() {
        api.userSteps.bannersFakeSteps().makeBannersModerated(subclientAdId);
        api.userSteps.adsSteps().shouldGetResultOnArchive(
                new IdsCriteriaMap().withIds(subclientAdId, subclientElseAdId),
                subclient,
                ExpectedResult.success(),
                ExpectedResult.errors(new Notification(8800, Api5ErrorDetails.AD_NOT_FOUND))
        );
    }

    @Test
    public void callArchiveWithClientId() {
        api.userSteps.adsSteps().shouldGetResultOnArchive(
                new IdsCriteriaMap().withIds(clientAdId),
                subclient,
                ExpectedResult.errors(new Notification(8800, Api5ErrorDetails.AD_NOT_FOUND))
        );
    }
}
