package ru.yandex.autotests.direct.api.campaigns.update.mobilecampaign.strategy.maximumclicks;

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

import com.yandex.direct.api.v5.campaigns.MobileAppCampaignSearchStrategyTypeEnum;
import com.yandex.direct.api.v5.campaigns.TextCampaignSettingsEnum;
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.campaigns.CampaignsFeatures;
import ru.yandex.autotests.direct.api.campaigns.CampaignsLogins;
import ru.yandex.autotests.direct.utils.money.Currency;
import ru.yandex.autotests.direct.utils.money.Money;
import ru.yandex.autotests.direct.utils.money.MoneyCurrency;
import ru.yandex.autotests.direct.utils.money.MoneyFormat;
import ru.yandex.autotests.directapi.apiclient.config.Semaphore;
import ru.yandex.autotests.directapi.apiclient.errors.Api5ErrorDetails;
import ru.yandex.autotests.directapi.model.api5.campaigns.CampaignUpdateItemMap;
import ru.yandex.autotests.directapi.model.api5.campaigns.MobileAppCampaignNetworkStrategyMap;
import ru.yandex.autotests.directapi.model.api5.campaigns.MobileAppCampaignSearchStrategyMap;
import ru.yandex.autotests.directapi.model.api5.campaigns.MobileAppCampaignStrategyMap;
import ru.yandex.autotests.directapi.model.api5.campaigns.MobileAppCampaignUpdateItemMap;
import ru.yandex.autotests.directapi.model.api5.campaigns.StrategyMaximumClicksMap;
import ru.yandex.autotests.directapi.model.api5.campaigns.StrategyNetworkDefaultAddMap;
import ru.yandex.autotests.directapi.model.api5.campaigns.UpdateRequestMap;
import ru.yandex.autotests.directapi.model.api5.general.ExpectedResult;
import ru.yandex.autotests.directapi.model.api5.general.JavaOrPerlExpectedResult;
import ru.yandex.autotests.directapi.model.api5.general.Notification;
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.allure.annotations.Step;
import ru.yandex.qatools.hazelcast.SemaphoreRule;

import static org.apache.commons.lang3.StringUtils.capitalize;
import static org.hamcrest.Matchers.nullValue;

/**
 * Created by chicos on 06.10.2015.
 */
@Aqua.Test
@Features(CampaignsFeatures.UPDATE)
@RunWith(Parameterized.class)
@Description("Негативные сценарии проверки параметра BidCeiling при обновлении мобильной кампании со стратегией " +
        "MaximumClicks")
public class StrategyMaximumClicksBidCeilingNegativeTest {
    private static final String client = CampaignsLogins.CLIENT;
    private static final Currency currency = Currency.RUB;
    private static MoneyCurrency moneyCurrency = MoneyCurrency.get(currency);

    private static final Money minValue = moneyCurrency.getMinPrice();
    private static final Money maxValue = moneyCurrency.getMaxPrice();
    private static final Money weeklyLimit = maxValue.multiply(2L).bidLong();

    private Long campaignId;

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

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

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

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

    @Parameterized.Parameter(value = 1)
    public Long bidCeiling;

    @Parameterized.Parameter(value = 2)
    public JavaOrPerlExpectedResult expectedResult;

    @Parameterized.Parameters(name = "test = {0}")
    public static Collection strategies() {
        Object[][] data = new Object[][]{
                {"Нулевое значение", 0L,
                        JavaOrPerlExpectedResult
                                .java(ExpectedResult.errorsIgnoringDetails(
                                        new Notification(4000, Api5ErrorDetails.MAX_BID_SHOULD_BE_MORE_THAN,
                                                minValue.stringValue(MoneyFormat.TWO_DIGITS_POINT_SEPARATED),
                                                moneyCurrency.getApiDescription())
                                ))
                                .perl(ExpectedResult.create(nullValue(),
                                Arrays.asList(
                                        new Notification(10165, Api5ErrorDetails.PARAM_WILL_NOT_BE_CHANGED, capitalize(
                                                StrategyNetworkDefaultAddMap.LIMIT_PERCENT)),
                                        new Notification(10163, Api5ErrorDetails.SETTING_WILL_NOT_BE_CHANGED,
                                                TextCampaignSettingsEnum.MAINTAIN_NETWORK_CPC)),
                                Arrays.asList(new Notification(4000, Api5ErrorDetails.MAX_BID_SHOULD_BE_MORE_THAN,
                                        minValue.stringValue(MoneyFormat.TWO_DIGITS_POINT_SEPARATED),
                                        moneyCurrency.getApiDescription()))))},
                {"Отрицательное значение", -1L,
                        JavaOrPerlExpectedResult
                                .java(ExpectedResult.errorsIgnoringDetails(
                                        new Notification(4000, Api5ErrorDetails.MAX_BID_SHOULD_BE_MORE_THAN,
                                                minValue.stringValue(MoneyFormat.TWO_DIGITS_POINT_SEPARATED),
                                                moneyCurrency.getApiDescription())
                                ))
                                .perl(ExpectedResult.create(nullValue(),
                                Arrays.asList(
                                        new Notification(10165, Api5ErrorDetails.PARAM_WILL_NOT_BE_CHANGED, capitalize(
                                                StrategyNetworkDefaultAddMap.LIMIT_PERCENT)),
                                        new Notification(10163, Api5ErrorDetails.SETTING_WILL_NOT_BE_CHANGED,
                                                TextCampaignSettingsEnum.MAINTAIN_NETWORK_CPC)),
                                Arrays.asList(new Notification(4000, Api5ErrorDetails.MAX_BID_SHOULD_BE_MORE_THAN,
                                        minValue.stringValue(MoneyFormat.TWO_DIGITS_POINT_SEPARATED),
                                        moneyCurrency.getApiDescription()))))},
                {"Значение меньше минимального", minValue.getPrevious().bidLong().longValue(),
                        JavaOrPerlExpectedResult
                                .java(ExpectedResult.errorsIgnoringDetails(
                                        new Notification(4000, Api5ErrorDetails.MAX_BID_SHOULD_BE_MORE_THAN,
                                                minValue.stringValue(MoneyFormat.TWO_DIGITS_POINT_SEPARATED),
                                                moneyCurrency.getApiDescription())
                                ))
                                .perl(ExpectedResult.create(nullValue(),
                                Arrays.asList(
                                        new Notification(10165, Api5ErrorDetails.PARAM_WILL_NOT_BE_CHANGED, capitalize(
                                                StrategyNetworkDefaultAddMap.LIMIT_PERCENT)),
                                        new Notification(10163, Api5ErrorDetails.SETTING_WILL_NOT_BE_CHANGED,
                                                TextCampaignSettingsEnum.MAINTAIN_NETWORK_CPC)),
                                Arrays.asList(new Notification(4000, Api5ErrorDetails.MAX_BID_SHOULD_BE_MORE_THAN,
                                        minValue.stringValue(MoneyFormat.TWO_DIGITS_POINT_SEPARATED),
                                        moneyCurrency.getApiDescription()))))},
                {"Значение больше максимального", maxValue.getNext().bidLong().longValue(),
                        JavaOrPerlExpectedResult
                                .java(ExpectedResult.errorsIgnoringDetails(
                                        new Notification(4000, Api5ErrorDetails.MAX_BID_SHOULD_BE_LESS_THAN,
                                                minValue.stringValue(MoneyFormat.TWO_DIGITS_POINT_SEPARATED),
                                                moneyCurrency.getApiDescription())
                                ))
                                .perl(ExpectedResult.create(nullValue(),
                                Arrays.asList(
                                        new Notification(10165, Api5ErrorDetails.PARAM_WILL_NOT_BE_CHANGED, capitalize(
                                                StrategyNetworkDefaultAddMap.LIMIT_PERCENT)),
                                        new Notification(10163, Api5ErrorDetails.SETTING_WILL_NOT_BE_CHANGED,
                                                TextCampaignSettingsEnum.MAINTAIN_NETWORK_CPC)),
                                Arrays.asList(new Notification(4000, Api5ErrorDetails.MAX_BID_SHOULD_BE_LESS_THAN,
                                        maxValue.stringValue(MoneyFormat.TWO_DIGITS_POINT_SEPARATED),
                                        moneyCurrency.getApiDescription()))))},
        };
        return Arrays.asList(data);
    }

    @Before
    @Step("Подготовим данные для теста")
    public void clearUnits() {
        api.userSteps.clientFakeSteps().fakeClearClientSpentUnits(client);
        campaignId = api.userSteps.campaignSteps().addDefaultMobileAppCampaign();
    }

    @Test
    @ru.yandex.qatools.allure.annotations.TestCaseId("1059")
    public void bidCeilingNegativeTest() {
        api.userSteps.campaignSteps().shouldGetResultOnUpdate(
                new UpdateRequestMap().withCampaigns(new CampaignUpdateItemMap()
                        .withId(campaignId)
                        .withMobileAppCampaign(new MobileAppCampaignUpdateItemMap()
                                .withBiddingStrategy(new MobileAppCampaignStrategyMap()
                                        .withSearch(new MobileAppCampaignSearchStrategyMap()
                                                .withBiddingStrategyType(
                                                        MobileAppCampaignSearchStrategyTypeEnum.WB_MAXIMUM_CLICKS)
                                                .withWbMaximumClicks(new StrategyMaximumClicksMap()
                                                        .withWeeklySpendLimit(weeklyLimit.longValue())
                                                        .withBidCeiling(bidCeiling)))
                                        .withNetwork(
                                                new MobileAppCampaignNetworkStrategyMap().defaultNetworkDefault())))),
                expectedResult);
    }
}
