package ru.yandex.autotests.direct.api.bidmodifiers.get;


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

import com.yandex.direct.api.v5.bidmodifiers.BidModifierFieldEnum;
import com.yandex.direct.api.v5.bidmodifiers.BidModifierGetItem;
import com.yandex.direct.api.v5.bidmodifiers.BidModifierTypeEnum;
import org.junit.AfterClass;
import org.junit.Before;
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.bidmodifiers.BidModifiersFeatures;
import ru.yandex.autotests.direct.api.bidmodifiers.BidModifiersLogins;
import ru.yandex.autotests.directapi.apiclient.config.Semaphore;
import ru.yandex.autotests.directapi.model.api5.bidmodifiers.BidModifierAddMap;
import ru.yandex.autotests.directapi.model.api5.bidmodifiers.BidModifiersSelectionCriteriaMap;
import ru.yandex.autotests.directapi.model.api5.bidmodifiers.DemographicsAdjustmentMap;
import ru.yandex.autotests.directapi.model.api5.bidmodifiers.GetRequestMap;
import ru.yandex.autotests.directapi.model.api5.bidmodifiers.RegionalAdjustmentMap;
import ru.yandex.autotests.directapi.model.api5.bidmodifiers.RetargetingAdjustmentMap;
import ru.yandex.autotests.directapi.model.api5.bidmodifiers.SerpLayoutAdjustmentMap;
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.equalTo;
import static org.hamcrest.Matchers.hasSize;
import static ru.yandex.autotests.irt.testutils.allure.TestSteps.assertThat;
import static ru.yandex.autotests.irt.testutils.allure.TestSteps.assumeThat;

/**
 * Created by chicos on 10.08.15.
 */
@Aqua.Test
@Features(BidModifiersFeatures.GET)
@Description("Проверка возвращаемых значений поля Type")
@RunWith(Parameterized.class)
public class GetBidModifiersTypeFieldTest {
    private static final String client = BidModifiersLogins.SINGLE_CLIENT_GET;

    private static Long campaignID;
    private static Long groupID;
    private static Long bmID;
    private static Integer conditionID;

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

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

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

    @Parameterized.Parameter(value = 0)
    public String description;

    @Parameterized.Parameter(value = 1)
    public BidModifierAddMap bmAddMap;

    @Parameterized.Parameter(value = 2)
    public BidModifierTypeEnum expectedType;

    @Parameterized.Parameters(name = "test = {0}")
    public static Collection testData() {
        prepareGroup();
        Object[][] data = new Object[][]{
                {"Демографическая корректировка на группе",
                        new BidModifierAddMap()
                                .withAdGroupId(groupID)
                                .withDemographicsAdjustment(new DemographicsAdjustmentMap()
                                        .defaultDemographicsAdjustment()),
                        BidModifierTypeEnum.DEMOGRAPHICS_ADJUSTMENT},
                {"Ретаргетинговая корректировка на группе",
                        new BidModifierAddMap()
                                .withAdGroupId(groupID)
                                .withRetargetingAdjustment(new RetargetingAdjustmentMap()
                                        .defaultRetargetingAdjustment()
                                        .withRetargetingConditionId(conditionID)),
                        BidModifierTypeEnum.RETARGETING_ADJUSTMENT},
                {"Демографическая корректировка на кампании",
                        new BidModifierAddMap()
                                .withCampaignId(campaignID)
                                .withDemographicsAdjustment(new DemographicsAdjustmentMap()
                                        .defaultDemographicsAdjustment()),
                        BidModifierTypeEnum.DEMOGRAPHICS_ADJUSTMENT},
                {"Ретаргетинговая корректировка на кампании",
                        new BidModifierAddMap()
                                .withCampaignId(campaignID)
                                .withRetargetingAdjustment(new RetargetingAdjustmentMap()
                                .defaultRetargetingAdjustment()
                                .withRetargetingConditionId(conditionID)),
                        BidModifierTypeEnum.RETARGETING_ADJUSTMENT},
                {"Региональная корректировка на кампании",
                        new BidModifierAddMap()
                                .withCampaignId(campaignID)
                                .withRegionalAdjustment(new RegionalAdjustmentMap()
                                .defaultRegionalAdjustment()),
                        BidModifierTypeEnum.REGIONAL_ADJUSTMENT},
                {"Корректировка на позицию на кампании",
                        new BidModifierAddMap()
                                .withCampaignId(campaignID)
                                .withSerpLayoutAdjustment(new SerpLayoutAdjustmentMap()
                                .defaultSerpLayoutAdjustment()),
                        BidModifierTypeEnum.SERP_LAYOUT_ADJUSTMENT}
        };
        return Arrays.asList(data);
    }

    private static void prepareGroup() {
        api.userSteps.clientFakeSteps().fakeClearClientSpentUnits(client);
        campaignID = api.userSteps.campaignSteps().addDefaultTextCampaign(client);
        groupID = api.userSteps.adGroupsSteps().addDefaultGroup(campaignID);
        conditionID = api.userSteps.retargetingSteps().addRandomRetargetingCondition(client);
    }

    @Before
    public void prepareBidModifier() {
        api.userSteps.clientFakeSteps().fakeClearClientSpentUnits(client);
        List<Long> bms = api.userSteps.bidModifiersSteps().bidModifiersAdd(bmAddMap);
        assumeThat("создали корректировку ставок", bms, hasSize(1));
        bmID = bms.get(0);
    }

    @Test
    public void bidModifiersTypeTest() {
        List<BidModifierGetItem> bms = api.userSteps.bidModifiersSteps().bidModifiersGet(
                new GetRequestMap()
                        .withSelectionCriteria(new BidModifiersSelectionCriteriaMap()
                                .withIds(bmID)
                                .withAllTypes()
                                .withAllLevels())
                        .withFieldNames(BidModifierFieldEnum.ID, BidModifierFieldEnum.TYPE));
        assumeThat("получили корректировку ставок", bms, hasSize(1));

        assertThat("вернулся корректный тип корректировки", bms.get(0).getType(), equalTo(expectedType));
    }

    @AfterClass
    public static void putCampaignToRemove() {
        //workaround для корректроного создания кампаний в параметризованном тесте
        api.userSteps.campaignSteps().putCampaignToQueue(client, campaignID);
    }
}
