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

import java.util.List;

import com.yandex.direct.api.v5.campaigns.CampaignFieldEnum;
import com.yandex.direct.api.v5.campaigns.CampaignGetItem;
import com.yandex.direct.api.v5.campaigns.CampaignStateGetEnum;
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.SuspendRequestMap;
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 ch.lambdaj.Lambda.extract;
import static ch.lambdaj.Lambda.on;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.everyItem;
import static ru.yandex.autotests.irt.testutils.allure.TestSteps.assertThat;

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

    private static final String client = CampaignsLogins.SINGLE_CLIENT_SUSPEND;

    @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 campaignIdText;
    private static Long campaignIdCpmBanner;
    private static Long campaignId;
    private static Long campaignId1;
    private static Long campaignId2;

    @BeforeClass
    public static void prepareCampaigns() {
        campaignIdText = api.userSteps.campaignSteps().addDefaultTextCampaign();
        campaignIdCpmBanner = api.userSteps.campaignSteps().addDefaultCpmBannerCampaign();
        campaignId = api.userSteps.campaignSteps().addDefaultCpmBannerCampaign();
        campaignId1 = api.userSteps.campaignSteps().addDefaultCpmBannerCampaign();
        campaignId2 = api.userSteps.campaignSteps().addDefaultCpmBannerCampaign();
    }

    @Test
    public void suspendSingleCpmBannerCampaignTest() {
        api.userSteps.campaignSteps().shouldGetResultOn(
                Action.SUSPEND,
                new SuspendRequestMap().withSelectionCriteria(new IdsCriteriaMap()
                        .withIds(campaignId)),
                ExpectedResult.success(campaignId));
        CampaignGetItem campaign = api.userSteps.campaignSteps().campaignsGet(campaignId, CampaignFieldEnum.STATE);
        assertThat("корректное значение поля State после остановки кампании", campaign.getState(),
                equalTo(CampaignStateGetEnum.SUSPENDED));
    }

    @Test
    public void suspendTwoCpmBannerCampaignsTest() {
        api.userSteps.campaignSteps().shouldGetResultOn(
                Action.SUSPEND,
                new SuspendRequestMap().withSelectionCriteria(new IdsCriteriaMap()
                        .withIds(campaignId1, campaignId2)),
                ExpectedResult.success(campaignId1),
                ExpectedResult.success(campaignId2));
        List<CampaignGetItem> campaigns = api.userSteps.campaignSteps().getCampaigns(campaignId1, campaignId2);
        List<CampaignStateGetEnum> states = extract(campaigns, on(CampaignGetItem.class).getState());
        assertThat("корректное значение поля State после остановки кампании", states,
                everyItem(equalTo(CampaignStateGetEnum.SUSPENDED)));
    }

    @Test
    public void suspendCampaignsDifferentTypesTest() {
        api.userSteps.campaignSteps().shouldGetResultOn(
                Action.SUSPEND,
                new SuspendRequestMap().withSelectionCriteria(new IdsCriteriaMap()
                        .withIds(campaignIdText, campaignIdCpmBanner)),
                ExpectedResult.success(campaignIdText),
                ExpectedResult.success(campaignIdCpmBanner));
        List<CampaignGetItem> campaigns =
                api.userSteps.campaignSteps().getCampaigns(campaignIdText, campaignIdCpmBanner);
        List<CampaignStateGetEnum> states = extract(campaigns, on(CampaignGetItem.class).getState());
        assertThat("корректное значение поля State после остановки кампании", states,
                everyItem(equalTo(CampaignStateGetEnum.SUSPENDED)));
    }
}
