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

import java.util.Arrays;
import java.util.List;

import com.yandex.direct.api.v5.ads.AdFieldEnum;
import com.yandex.direct.api.v5.ads.AdGetItem;
import com.yandex.direct.api.v5.ads.GetResponse;
import com.yandex.direct.api.v5.general.StateEnum;
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.tags.TagDictionary;
import ru.yandex.autotests.directapi.apiclient.config.Semaphore;
import ru.yandex.autotests.directapi.enums.AdGroupType;
import ru.yandex.autotests.directapi.enums.BannerType;
import ru.yandex.autotests.directapi.enums.CampaignType;
import ru.yandex.autotests.directapi.model.api5.ads.AdGetItemMap;
import ru.yandex.autotests.directapi.model.api5.ads.AdsSelectionCriteriaMap;
import ru.yandex.autotests.directapi.model.api5.ads.GetRequestMap;
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.Tag;
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.allure.annotations.Step;
import ru.yandex.qatools.hazelcast.SemaphoreRule;

import static ru.yandex.autotests.irt.testutils.allure.TestSteps.assertThat;
import static ru.yandex.autotests.irt.testutils.beandiffer.BeanDifferMatcher.beanDiffer;

/**
 * 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")
@Tag(TagDictionary.TRUNK)
@RunWith(Parameterized.class)
public class ArchiveAdsTest {

    @ClassRule
    public static ApiSteps api = new ApiSteps().version(104).as(AdsLogins.CLIENT);

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

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

    private Long campaignId;
    private Long adId1;
    private Long adId2;

    @Parameterized.Parameter(0)
    public CampaignType campaignType;

    @Parameterized.Parameter(1)
    public AdGroupType adGroupType;

    @Parameterized.Parameter(2)
    public BannerType bannerType;

    @Parameterized.Parameters(name = "bannerType = {2}")
    public static Iterable<Object[]> data() {
        return Arrays.asList(new Object[][]{
                {CampaignType.TEXT, AdGroupType.BASE, BannerType.TEXT},
                {CampaignType.MOBILE_CONTENT, AdGroupType.MOBILE_CONTENT, BannerType.MOBILE_CONTENT},
                {CampaignType.DYNAMIC, AdGroupType.DYNAMIC, BannerType.DYNAMIC},
        });
    }

    @BeforeClass
    public static void initTest() {
        api.userSteps.clientFakeSteps().fakeClearClientSpentUnits(AdsLogins.CLIENT);
    }

    @Before
    @Step("Подготовка данных для теста")
    public void createObjects() {
        campaignId = api.userSteps.campaignSteps().addDefaultCampaign(campaignType);
        Long adGroupId = api.userSteps.adGroupsSteps().addDefaultGroup(campaignId, adGroupType);
        List<Long> adIds = api.userSteps.adsSteps().addDefaultAds(adGroupId, 2, bannerType);
        adId1 = adIds.get(0);
        adId2 = adIds.get(1);
        api.userSteps.bannersFakeSteps().makeBannersModerated(adId1, adId2);
    }

    @Test
    @Description("Заархивировать одно объявление")
    public void archiveOneAdTest() {
        api.userSteps.adsSteps()
                .shouldGetResultOnArchive(new IdsCriteriaMap().withIds(adId1), ExpectedResult.success());
        GetResponse getResponse = api.userSteps.adsSteps().adsGet(new GetRequestMap()
                .withFieldNames(AdFieldEnum.ID, AdFieldEnum.STATE)
                .withSelectionCriteria(new AdsSelectionCriteriaMap().withIds(adId1)));
        assertThat("объявление заархивировалось", getResponse.getAds(), beanDiffer(Arrays.asList(
                (AdGetItem) new AdGetItemMap().withId(adId1).withState(StateEnum.ARCHIVED).getBean())));
    }

    @Test
    @Description("Заархивировать два объявления")
    public void archiveTwoAdsTest() {
        api.userSteps.adsSteps()
                .shouldGetResultOnArchive(new IdsCriteriaMap().withIds(adId1, adId2),
                        ExpectedResult.success(), ExpectedResult.success());

        GetResponse getResponse = api.userSteps.adsSteps().adsGet(new GetRequestMap()
                .withFieldNames(AdFieldEnum.ID, AdFieldEnum.STATE)
                .withSelectionCriteria(new AdsSelectionCriteriaMap().withIds(adId1, adId2)));
        assertThat("объявление заархивировалось", getResponse.getAds(), beanDiffer(Arrays.asList(
                (AdGetItem) new AdGetItemMap().withId(adId1).withState(StateEnum.ARCHIVED).getBean(),
                (AdGetItem) new AdGetItemMap().withId(adId2).withState(StateEnum.ARCHIVED).getBean())));
    }
}
