package ru.yandex.autotests.direct.api.bids.set;

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

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.bids.BidsFeatures;
import ru.yandex.autotests.direct.api.bids.BidsLogins;
import ru.yandex.autotests.direct.utils.money.Currency;
import ru.yandex.autotests.direct.utils.money.MoneyCurrency;
import ru.yandex.autotests.direct.utils.money.MoneyFormat;
import ru.yandex.autotests.direct.utils.tags.TagDictionary;
import ru.yandex.autotests.directapi.apiclient.config.ProtocolType;
import ru.yandex.autotests.directapi.apiclient.config.Semaphore;
import ru.yandex.autotests.directapi.apiclient.errors.Api5ErrorDetails;
import ru.yandex.autotests.directapi.model.api5.bids.BidExpectedResult;
import ru.yandex.autotests.directapi.model.api5.bids.BidSetItemMap;
import ru.yandex.autotests.directapi.model.api5.bids.BidsGetItemMap;
import ru.yandex.autotests.directapi.model.api5.bids.SetRequestMap;
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.Tag;
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;

/**
 * Created by ginger on 24.09.14.
 */
@Aqua.Test
@Features(BidsFeatures.SET)
@Tag(TagDictionary.TRUNK)
@RunWith(Parameterized.class)
public class BidLessMinValueTest {

    @ClassRule
    public static ApiSteps api = new ApiSteps();

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

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

    @Parameterized.Parameter(0)
    public String currencyLogin;

    @Parameterized.Parameter(1)
    public Long valueOfBid;

    @Parameterized.Parameter(2)
    public Notification notification;
    private long keywordId;

    @Parameterized.Parameters(name = "currencyLogin = {0}, valueOfBid = {1}")
    public static Collection testData() {
        Object[][] data = new Object[][]{
                {BidsLogins.BIDS_CLIENT_UAH,
                        MoneyCurrency.get(Currency.UAH).getMinPrice().getPrevious().bidLong().longValue(),   //значения меньше минимальной ставки
                        new Notification(5005, Api5ErrorDetails.BID_VALUE_IN_FIELD_MUST_NOT_BE_LESS,
                                capitalize(BidsGetItemMap.BID),
                                (MoneyCurrency.get(Currency.UAH).getNameCasusDativus(api.locale())),
                                MoneyCurrency.get(Currency.UAH).getLongMinPrice().stringValue(MoneyFormat.INTEGER))},
                {BidsLogins.BIDS_CLIENT_KZT,
                        MoneyCurrency.get(Currency.KZT).getMinPrice().getPrevious().bidLong().longValue(),
                        new Notification(5005, Api5ErrorDetails.BID_VALUE_IN_FIELD_MUST_NOT_BE_LESS,
                                capitalize(BidsGetItemMap.BID),
                                (MoneyCurrency.get(Currency.KZT).getNameCasusDativus(api.locale())),
                                MoneyCurrency.get(Currency.KZT).getLongMinPrice().stringValue(MoneyFormat.INTEGER))},
                {BidsLogins.BIDS_CLIENT_EUR,
                        MoneyCurrency.get(Currency.EUR).getMinPrice().getPrevious().bidLong().longValue(),
                        new Notification(5005, Api5ErrorDetails.BID_VALUE_IN_FIELD_MUST_NOT_BE_LESS,
                                capitalize(BidsGetItemMap.BID),
                                (MoneyCurrency.get(Currency.EUR).getAbbreviation(api.locale())),
                                MoneyCurrency.get(Currency.EUR).getLongMinPrice().stringValue(MoneyFormat.INTEGER))},
                {BidsLogins.BIDS_CLIENT_CHF,
                        MoneyCurrency.get(Currency.CHF).getMinPrice().getPrevious().bidLong().longValue(),
                        new Notification(5005, Api5ErrorDetails.BID_VALUE_IN_FIELD_MUST_NOT_BE_LESS,
                                capitalize(BidsGetItemMap.BID),
                                (MoneyCurrency.get(Currency.CHF).getNameCasusDativus(api.locale())),
                                MoneyCurrency.get(Currency.CHF).getLongMinPrice().stringValue(MoneyFormat.INTEGER))},
                {BidsLogins.BIDS_CLIENT_TRY,
                        MoneyCurrency.get(Currency.TRY).getMinPrice().getPrevious().bidLong().longValue(),
                        new Notification(5005, Api5ErrorDetails.BID_VALUE_IN_FIELD_MUST_NOT_BE_LESS,
                                capitalize(BidsGetItemMap.BID),
                                (MoneyCurrency.get(Currency.TRY).getNamePluralI(api.locale())),
                                MoneyCurrency.get(Currency.TRY).getLongMinPrice().stringValue(MoneyFormat.INTEGER))},
                {BidsLogins.BIDS_CLIENT_RUB,
                        MoneyCurrency.get(Currency.RUB).getMinPrice().getPrevious().bidLong().longValue(),
                        new Notification(5005, Api5ErrorDetails.BID_VALUE_IN_FIELD_MUST_NOT_BE_LESS,
                                capitalize(BidsGetItemMap.BID),
                                (MoneyCurrency.get(Currency.RUB).getNamePluralI(api.locale())),
                                MoneyCurrency.get(Currency.RUB).getLongMinPrice().stringValue(MoneyFormat.INTEGER))},
                {BidsLogins.BIDS_CLIENT_USD,
                        MoneyCurrency.get(Currency.USD).getMinPrice().getPrevious().bidLong().longValue(),
                        new Notification(5005, Api5ErrorDetails.BID_VALUE_IN_FIELD_MUST_NOT_BE_LESS,
                                capitalize(BidsGetItemMap.BID),
                                (MoneyCurrency.get(Currency.USD).getNameCasusDativus(api.locale())),
                                MoneyCurrency.get(Currency.USD).getLongMinPrice().stringValue(MoneyFormat.INTEGER))},
                {BidsLogins.CLIENT_BYN,
                        MoneyCurrency.get(Currency.BYN).getMinPrice().getPrevious().bidLong().longValue(),
                        new Notification(5005, Api5ErrorDetails.BID_VALUE_IN_FIELD_MUST_NOT_BE_LESS,
                                capitalize(BidsGetItemMap.BID),
                                (MoneyCurrency.get(Currency.BYN).getNamePluralI(api.locale())),
                                MoneyCurrency.get(Currency.BYN).getLongMinPrice().stringValue(MoneyFormat.INTEGER))}
        };
        return Arrays.asList(data);
    }

    @Before
    @Step("Подготовка данных для теста")
    public void createDate() {
        api.userSteps.clientFakeSteps().fakeClearClientSpentUnits(currencyLogin);
        Long campaignId = api.as(currencyLogin).userSteps.campaignSteps().addDefaultTextCampaign(currencyLogin);
        Long adGroupId = api.userSteps.adGroupsSteps().addDefaultGroup(campaignId);
        api.userSteps.adsSteps().addDefaultTextAd(adGroupId);
        keywordId = api.protocol(ProtocolType.JSON).userSteps.keywordsSteps().addDefaultKeyword(adGroupId);
        api.protocol(ProtocolType.SOAP);
    }

    @Test
    @ru.yandex.qatools.allure.annotations.TestCaseId("1337")
    public void trySetInvalidValueInDifferentCurrency() {
        //testplan: string № 41
        //testplan: string № 42
        //https://st.yandex-team.ru/DIRECT-35279
        api.userSteps.bidsSteps().shouldGetBidResultOnSet(
                new SetRequestMap()
                        .withBids(
                                new BidSetItemMap()
                                        .withKeywordId(keywordId)
                                        .withBid(valueOfBid)
                        ),
                currencyLogin,
                BidExpectedResult.errors(notification));
    }

}
