package ru.yandex.autotests.direct.api.keywordbids.set.strategy;

import java.util.Arrays;
import java.util.Collection;
import ru.yandex.qatools.allure.annotations.TestCaseId;

import com.google.common.base.Joiner;
import com.yandex.direct.api.v5.general.PriorityEnum;
import com.yandex.direct.api.v5.keywordbids.GetResponse;
import com.yandex.direct.api.v5.keywordbids.KeywordBidFieldEnum;
import com.yandex.direct.api.v5.keywordbids.KeywordBidNetworkFieldEnum;
import com.yandex.direct.api.v5.keywordbids.KeywordBidSearchFieldEnum;
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 org.junit.runners.Parameterized.Parameter;
import org.junit.runners.Parameterized.Parameters;

import ru.yandex.aqua.annotations.project.Aqua;
import ru.yandex.autotests.direct.api.bids.BidsLogins;
import ru.yandex.autotests.direct.api.keywordbids.KeywordBidsFeatures;
import ru.yandex.autotests.direct.utils.money.Currency;
import ru.yandex.autotests.direct.utils.money.MoneyCurrency;
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.apiclient.errors.Api5ErrorDetailsJava;
import ru.yandex.autotests.directapi.model.api5.campaigns.TextCampaignNetworkStrategyAddMap;
import ru.yandex.autotests.directapi.model.api5.campaigns.TextCampaignSearchStrategyAddMap;
import ru.yandex.autotests.directapi.model.api5.general.Notification;
import ru.yandex.autotests.directapi.model.api5.keywordbids.GetRequestMap;
import ru.yandex.autotests.directapi.model.api5.keywordbids.KeywordBidExpectedResult;
import ru.yandex.autotests.directapi.model.api5.keywordbids.KeywordBidsGetItemMap;
import ru.yandex.autotests.directapi.model.api5.keywordbids.KeywordBidsSelectionCriteriaMap;
import ru.yandex.autotests.directapi.model.api5.keywordbids.KeywordBidsSetItemMap;
import ru.yandex.autotests.directapi.model.api5.keywordbids.SetRequestMap;
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;
import static ru.yandex.autotests.directapi.apiclient.errors.Path.path;
import static ru.yandex.autotests.directapi.matchers.beandiffer2.BeanDifferMatcherV5.beanDifferV5;
import static ru.yandex.autotests.irt.testutils.allure.TestSteps.assertThat;

/**
 * Created by ginger on 01.10.14.
 */

@Aqua.Test
@Features(KeywordBidsFeatures.SET)
@Tag(TagDictionary.TRUNK)
@RunWith(Parameterized.class)
public class SetPriorityManualOnSearchAndMaximumCoverageOnContextStrategyTest {

    private static final String CLIENT = BidsLogins.BIDS_CLIENT;
    private static final Long BID1 = MoneyCurrency.get(Currency.RUB).getLongMinPrice().longValue() * 2L;
    private static final Long BID2 = BID1 * 2L;

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

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

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

    @Parameter(0)
    public String description;

    @Parameter(1)
    public TextCampaignSearchStrategyAddMap searchStrategy;

    private Long campaignID;
    private Long keywordId;
    private Long autoTargetingId;

    @Parameters(name = "{0}")
    public static Collection data() {
        return Arrays.asList(new Object[][]{
                {"HIGHEST_POSITION + MAXIMUM_COVERAGE",
                        new TextCampaignSearchStrategyAddMap().defaultHighestPosition()},
        });
    }


    @Before
    @Step("Подготовка данных для теста")
    public void createData() {
        api.userSteps.clientFakeSteps().fakeClearClientSpentUnits(CLIENT);
        campaignID = api.userSteps.campaignSteps().addDefaultTextCampaignWithStrategies(
                searchStrategy,
                new TextCampaignNetworkStrategyAddMap().defaultMaximumCoverage()
        );
        Long adGroupId = api.userSteps.adGroupsSteps().addDefaultGroup(campaignID);
        api.userSteps.adsSteps().addDefaultTextAd(adGroupId);
        keywordId = api.userSteps.keywordsSteps().addDefaultKeyword(adGroupId);
        autoTargetingId = api.userSteps.keywordsSteps().addAutotargeting(adGroupId);
        api.protocol(ProtocolType.SOAP);
    }

    @Test
    @TestCaseId("2953")
    public void setStrategyPriorityTest() {
        //DIRECT-35445,  DIRECT-35478
        api.userSteps.keywordBidsSteps().shouldGetKeywordBidResultOnSet(
                new SetRequestMap()
                        .withKeywordBids(
                                new KeywordBidsSetItemMap()
                                        .withCampaignId(campaignID)
                                        .withStrategyPriority(PriorityEnum.HIGH)
                        ),

                KeywordBidExpectedResult.warningWithError(
                        new Notification(10161,
                                Api5ErrorDetails.FIELD_WILL_NOT_CHANGE_BECAUSE_OF_MANUAL_STRATEGY,
                                path(KeywordBidsSetItemMap.STRATEGY_PRIORITY)),
                        new Notification(9600,
                                Api5ErrorDetailsJava.ONE_OF_FIELDS_SHOULD_BE_SPECIFIED_FOR_STRATEGY,
                                Joiner.on(", ").join(
                                        path(KeywordBidsSetItemMap.SEARCH_BID),
                                        path(KeywordBidsSetItemMap.NETWORK_BID))))
        );
    }

    @Test
    @TestCaseId("2954")
    public void setBidAndContextBidAndStrategyPriorityTest() {
        // DIRECT-35448
        api.userSteps.keywordBidsSteps().shouldGetKeywordBidResultOnSet(
                new SetRequestMap()
                        .withKeywordBids(
                                new KeywordBidsSetItemMap()
                                        .withCampaignId(campaignID)
                                        .withSearchBid(BID1)
                                        .withNetworkBid(BID2)
                                        .withStrategyPriority(PriorityEnum.HIGH)
                        ),
                KeywordBidExpectedResult.warningsWithCampaignID(
                        new Notification(10161, Api5ErrorDetails.FIELD_WILL_NOT_CHANGE_BECAUSE_OF_MANUAL_STRATEGY,
                                capitalize(KeywordBidsSetItemMap.STRATEGY_PRIORITY))
                )
        );

        GetResponse getResponse = api.userSteps.keywordBidsSteps().keywordBidsGet(
                new GetRequestMap()
                        .withSelectionCriteria(
                                new KeywordBidsSelectionCriteriaMap().withCampaignIds(campaignID)
                        )
                        .withFieldNames(KeywordBidFieldEnum.KEYWORD_ID, KeywordBidFieldEnum.STRATEGY_PRIORITY)
                        .withNetworkFieldNames(KeywordBidNetworkFieldEnum.BID)
                        .withSearchFieldNames(KeywordBidSearchFieldEnum.BID)
        );
        assertThat("получили правильные значения Bid, ContextBid и StrategyPriority",
                getResponse.getKeywordBids(),
                beanDifferV5(
                        Arrays.asList(
                                new KeywordBidsGetItemMap().withKeywordId(keywordId).withSearchBid(BID1)
                                        .withNetworkBid(BID2)
                                        .withStrategyPriority(
                                                PriorityEnum.NORMAL
                                        ).getBean(),
                                new KeywordBidsGetItemMap().withKeywordId(autoTargetingId).withSearchBid(BID1)
                                        .withNetworkBid(BID2).withStrategyPriority(
                                        PriorityEnum.NORMAL
                                ).getBean()
                        )
                )
        );
    }
}
