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

import java.math.BigDecimal;
import ru.yandex.qatools.allure.annotations.TestCaseId;

import com.yandex.direct.api.v5.bids.BidFieldEnum;
import com.yandex.direct.api.v5.bids.GetResponse;
import com.yandex.direct.api.v5.bids.ObjectFactory;
import com.yandex.direct.api.v5.general.PriorityEnum;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;

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.db.models.jooq.ppc.tables.records.BidsRecord;
import ru.yandex.autotests.direct.db.steps.DirectJooqDbSteps;
import ru.yandex.autotests.direct.utils.money.Currency;
import ru.yandex.autotests.direct.utils.money.MoneyCurrency;
import ru.yandex.autotests.directapi.apiclient.config.Semaphore;
import ru.yandex.autotests.directapi.darkside.model.CampaignsType;
import ru.yandex.autotests.directapi.model.api5.bids.BidsGetItemMap;
import ru.yandex.autotests.directapi.model.api5.bids.BidsSelectionCriteriaMap;
import ru.yandex.autotests.directapi.model.api5.bids.GetRequestMap;
import ru.yandex.autotests.directapi.rules.ApiSteps;
import ru.yandex.autotests.directapi.rules.Trashman;
import ru.yandex.qatools.allure.annotations.Features;
import ru.yandex.qatools.allure.annotations.Issue;
import ru.yandex.qatools.allure.annotations.TestCaseId;
import ru.yandex.qatools.hazelcast.SemaphoreRule;

import static java.util.Collections.singletonList;
import static org.hamcrest.Matchers.hasSize;
import static ru.yandex.autotests.directapi.matchers.beans.version5.BeanDifferMatcherV5.beanDifferV5;
import static ru.yandex.autotests.irt.testutils.allure.TestSteps.assertThat;

@Aqua.Test
@Features(BidsFeatures.GET)
@Issue("https://st.yandex-team.ru/DIRECT-83722")
public class BidsGetCpmBannerTest {

    private static final String CLIENT = BidsLogins.CLIENT_FOR_RUB;
    private static long adGroupId;
    private static long keywordId;
    private static 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);

    private static DirectJooqDbSteps dbSteps = api.userSteps.getDirectJooqDbSteps().useShardForLogin(CLIENT);

    @BeforeClass
    public static void prepare() {
        api.userSteps.clientFakeSteps().fakeClearClientSpentUnits(CLIENT);
        campaignId = api.userSteps.campaignSteps().addDefaultTextCampaign();
        api.userSteps.campaignFakeSteps().setType(campaignId, CampaignsType.CPM_BANNER);
        adGroupId = api.userSteps.adGroupsSteps().addDefaultCpmBannerKeywordsAdGroup(campaignId);
        keywordId = api.userSteps.keywordsSteps().addDefaultKeyword(adGroupId);
    }

    @Test
    @TestCaseId("2805")
    public void getByCampaignId() {
        GetResponse response = api.userSteps.bidsSteps().bidsGet(
                new GetRequestMap().withSelectionCriteria(
                        new BidsSelectionCriteriaMap()
                                .withCampaignIds(campaignId))
                        .withAllFieldNames());

        assertThat("фраза вернулась", response.getBids(), hasSize(1));
    }

    @Test
    @TestCaseId("2806")
    public void getByAdgroupId() {
        GetResponse response = api.userSteps.bidsSteps().bidsGet(
                new GetRequestMap().withSelectionCriteria(
                        new BidsSelectionCriteriaMap()
                                .withAdGroupIds(adGroupId))
                        .withAllFieldNames());

        assertThat("фраза вернулась", response.getBids(), hasSize(1));
    }

    @Test
    @TestCaseId("2807")
    public void getByKeywordId() {
        GetResponse response = api.userSteps.bidsSteps().bidsGet(
                new GetRequestMap().withSelectionCriteria(
                        new BidsSelectionCriteriaMap()
                                .withKeywordIds(keywordId))
                        .withAllFieldNames());
        assertThat("фраза вернулась", response.getBids(), hasSize(1));
    }

    @Test
    @TestCaseId("3046")
    public void testDefaultBids() {
        long defaultBid = MoneyCurrency.get(Currency.RUB).getMinCpmPrice().bidLong().longValue();

        BidsRecord bidsRecord = dbSteps.bidsSteps().getBidById(keywordId);
        bidsRecord.setPrice(BigDecimal.ZERO).setPriceContext(BigDecimal.ZERO);
        dbSteps.bidsSteps().updateBids(bidsRecord);

        GetResponse response = api.userSteps.bidsSteps().bidsGet(
                new GetRequestMap().withSelectionCriteria(
                        new BidsSelectionCriteriaMap()
                                .withKeywordIds(keywordId))
                        .withFieldNames(BidFieldEnum.BID, BidFieldEnum.CONTEXT_BID, BidFieldEnum.STRATEGY_PRIORITY));

        assertThat("ставки по умолчанию установились верно", response.getBids(), beanDifferV5(singletonList(
                new BidsGetItemMap()
                        .withBid(defaultBid)
                        .withContextBid(defaultBid)
                        .withStrategyPriority(new ObjectFactory().createBidGetItemStrategyPriority(PriorityEnum.NORMAL))
                        .getBean())));
    }
}
