package ru.yandex.autotests.direct.api.campaigns.delete;

import java.util.List;

import com.yandex.direct.api.v5.campaigns.CampaignGetItem;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;

import ru.yandex.aqua.annotations.project.Aqua;
import ru.yandex.autotests.direct.api.campaigns.CampaignsFeatures;
import ru.yandex.autotests.direct.api.campaigns.CampaignsLogins;
import ru.yandex.autotests.directapi.apiclient.config.Semaphore;
import ru.yandex.autotests.directapi.model.api5.Action;
import ru.yandex.autotests.directapi.model.api5.campaigns.DeleteRequestMap;
import ru.yandex.autotests.directapi.model.api5.general.ExpectedResult;
import ru.yandex.autotests.directapi.model.api5.general.IdsCriteriaMap;
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.hazelcast.SemaphoreRule;

import static org.hamcrest.Matchers.emptyIterable;
import static ru.yandex.autotests.irt.testutils.allure.TestSteps.assertThat;

/**
 * Created by gerdler on 23.09.18.
 */
@Aqua.Test
@Features(CampaignsFeatures.DELETE)
@Description("Проверка позитивных сценариев удаления медийных кампаний")
public class DeleteCpmBannerCampaignsPositiveTest {

    private static final String client = CampaignsLogins.SINGLE_CLIENT_DELETE;
    private static final String clientAccount = CampaignsLogins.SINGLE_CLIENT_DELETE_ACCOUNT;

    @ClassRule
    public static ApiSteps api = new ApiSteps().as(client);

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

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

    private static Long campaignAccount;

    private static Long campaignIdCpmBanner;
    private static Long campaignIdText;
    private static Long campaignID;
    private static Long campaignID1;
    private static Long campaignID2;

    @BeforeClass
    public static void prepareCampaigns() {
        campaignIdCpmBanner = api.as(client).userSteps.campaignSteps().addDefaultCpmBannerCampaign();
        campaignIdText = api.userSteps.campaignSteps().addDefaultTextCampaign();
        campaignID = api.userSteps.campaignSteps().addDefaultCpmBannerCampaign();
        campaignID1 = api.userSteps.campaignSteps().addDefaultCpmBannerCampaign();
        campaignID2 = api.userSteps.campaignSteps().addDefaultCpmBannerCampaign();
        campaignAccount = api.as(clientAccount).userSteps.campaignSteps().addDefaultCpmBannerCampaign();
    }

    @Test
    public void deleteSingleCpmBannerCampaignTest() {
        api.as(client).userSteps.campaignSteps().shouldGetResultOn(
                Action.DELETE,
                new DeleteRequestMap().withSelectionCriteria(new IdsCriteriaMap()
                        .withIds(campaignID)),
                ExpectedResult.success(campaignID));
        List<CampaignGetItem> campaigns = api.userSteps.campaignSteps().getCampaigns(campaignID);
        assertThat("кампания удалена", campaigns, emptyIterable());
    }

    @Test
    public void deleteTwoCpmBannerCampaignsTest() {
        api.as(client).userSteps.campaignSteps().shouldGetResultOn(
                Action.DELETE,
                new DeleteRequestMap().withSelectionCriteria(new IdsCriteriaMap()
                        .withIds(campaignID1, campaignID2)),
                ExpectedResult.success(campaignID1),
                ExpectedResult.success(campaignID2));
        List<CampaignGetItem> campaigns = api.userSteps.campaignSteps().getCampaigns(campaignID1, campaignID2);
        assertThat("кампании удалены", campaigns, emptyIterable());
    }

    @Test
    @Description("Удалим медийную кампанию клиента с подключенным общим счётом")
    public void deleteSingleCpmBannerCampaignAccountTest() {
        api.as(clientAccount).userSteps.campaignSteps().shouldGetResultOn(
                Action.DELETE,
                new DeleteRequestMap().withSelectionCriteria(new IdsCriteriaMap()
                        .withIds(campaignAccount)),
                ExpectedResult.success(campaignAccount));
        List<CampaignGetItem> campaigns = api.userSteps.campaignSteps().getCampaigns(campaignAccount);
        assertThat("кампания удалена", campaigns, emptyIterable());
    }

    @Test
    public void deleteCampaignsDifferentTypesTest() {
        api.as(client).userSteps.campaignSteps().shouldGetResultOn(
                Action.DELETE,
                new DeleteRequestMap().withSelectionCriteria(new IdsCriteriaMap()
                        .withIds(campaignIdText, campaignIdCpmBanner)),
                ExpectedResult.success(campaignIdText),
                ExpectedResult.success(campaignIdCpmBanner));
        List<CampaignGetItem> campaigns =
                api.userSteps.campaignSteps().getCampaigns(campaignIdText, campaignIdCpmBanner);
        assertThat("кампании удалены", campaigns, emptyIterable());
    }
}
