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

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

import com.yandex.direct.api.v5.general.GenderEnum;
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.apiclient.errors.Api5ErrorDetails;
import ru.yandex.autotests.directapi.darkside.model.CampaignsType;
import ru.yandex.autotests.directapi.model.api5.Action;
import ru.yandex.autotests.directapi.model.api5.bidmodifiers.AddRequestMap;
import ru.yandex.autotests.directapi.model.api5.bidmodifiers.BidModifierAddMap;
import ru.yandex.autotests.directapi.model.api5.bidmodifiers.DemographicsAdjustmentMap;
import ru.yandex.autotests.directapi.model.api5.bidmodifiers.DesktopAdjustmentMap;
import ru.yandex.autotests.directapi.model.api5.bidmodifiers.MobileAdjustmentMap;
import ru.yandex.autotests.directapi.model.api5.bidmodifiers.RetargetingAdjustmentMap;
import ru.yandex.autotests.directapi.model.api5.general.MultiIdsExpectedResult;
import ru.yandex.autotests.directapi.model.api5.general.Notification;
import ru.yandex.autotests.directapi.model.retargeting.RetargetingGoalType;
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;

@Aqua.Test
@Features(BidModifiersFeatures.ADD)
@Description("Проверка недоступности корректировок для cpm_banner группы с профилем пользователей")
@RunWith(Parameterized.class)
public class AddBidModifiersForCpmBannerWithUserProfileTest {

    private static final String LOGIN = BidModifiersLogins.SINGLE_CLIENT_RETARGETING_LISTS;

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

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

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

    @Parameterized.Parameter(0)
    public String description;

    @Parameterized.Parameter(1)
    public BidModifierAddMap bidModifier;

    @Parameterized.Parameter(2)
    public boolean isSupported;

    @Parameterized.Parameters(name = "test = {0}")
    public static Collection<Object[]> data() {
        int mobileBidModifier = (MobileAdjustmentMap.MOBILE_ADJUSTMENT_MAX -
                MobileAdjustmentMap.MOBILE_ADJUSTMENT_MIN) / 2;
        int demographicsBidModifier = (DemographicsAdjustmentMap.DEMOGRAPHICS_ADJUSTMENT_MAX -
                DemographicsAdjustmentMap.DEMOGRAPHICS_ADJUSTMENT_MIN) / 2;
        int retargetingBidModifier = (RetargetingAdjustmentMap.RETARGETING_ADJUSTMENT_MAX -
                RetargetingAdjustmentMap.RETARGETING_ADJUSTMENT_MIN) / 2;

        api.userSteps.clientFakeSteps().fakeClearClientSpentUnits(LOGIN);
        Long campaignId = api.userSteps.campaignSteps().addDefaultTextCampaign();
        api.userSteps.campaignFakeSteps().setType(campaignId, CampaignsType.CPM_BANNER);
        Long adGroupId = api.userSteps.adGroupsSteps().addDefaultCpmBannerUserProfileAdGroup(campaignId);
        Long goalId =
                api.userSteps.retargetingSteps().getRetargetingGoalIDsByType(LOGIN, RetargetingGoalType.GOAL).get(0);
        Long retargetingListsId = api.userSteps.retargetingListsSteps().addDefaultRetargetingLists(goalId, LOGIN);

        return Arrays.asList(new Object[][]{
                {"Демография", new BidModifierAddMap()
                        .withDemographicsAdjustment(new DemographicsAdjustmentMap()
                                .withBidModifier(demographicsBidModifier)
                                .withGender(GenderEnum.GENDER_FEMALE))
                        .withAdGroupId(adGroupId),
                        false
                },
                {"Ретаргетинг", new BidModifierAddMap()
                        .withRetargetingAdjustment(new RetargetingAdjustmentMap()
                                .withBidModifier(retargetingBidModifier)
                                .withRetargetingConditionId(retargetingListsId))
                        .withAdGroupId(adGroupId),
                        false
                },
                {"Мобильные", new BidModifierAddMap()
                        .withMobileAdjustment(new MobileAdjustmentMap()
                                .withBidModifier(mobileBidModifier))
                        .withAdGroupId(adGroupId),
                        true
                },
                {"Десктопные", new BidModifierAddMap()
                        .withDesktopAdjustment(new DesktopAdjustmentMap().defaultDesktopAdjustment())
                        .withAdGroupId(adGroupId),
                        true
                },
        });
    }

    @Test
    public void bidModifierNotSupportedTest() {
        api.userSteps.bidModifiersSteps().shouldGetResultOn(
                Action.ADD,
                new AddRequestMap().withBidModifiers(bidModifier),
                isSupported ?
                        MultiIdsExpectedResult.success()
                        : MultiIdsExpectedResult.errors(new Notification(3500,
                                Api5ErrorDetails.ADJUSTMENT_OF_THIS_TYPE_IS_NOT_SUPPORTED)));
    }
}
