package ru.yandex.autotests.directintapi.bstransport.main.mcbanner.transmit;

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

import org.junit.Before;
import org.junit.BeforeClass;
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.db.models.jooq.ppc.enums.BannersBannerType;
import ru.yandex.autotests.direct.db.models.jooq.ppc.enums.ImagesStatusmoderate;
import ru.yandex.autotests.direct.db.models.jooq.ppc.enums.PhrasesAdgroupType;
import ru.yandex.autotests.direct.db.models.jooq.ppc.tables.records.CampOptionsRecord;
import ru.yandex.autotests.direct.utils.tags.TagDictionary;
import ru.yandex.autotests.directapi.darkside.Logins;
import ru.yandex.autotests.directapi.darkside.connection.Semaphore;
import ru.yandex.autotests.directapi.darkside.model.CampaignsType;
import ru.yandex.autotests.directapi.darkside.model.RunBsTransportScriptResponse;
import ru.yandex.autotests.directapi.darkside.model.bslogs.clientdata.Banner;
import ru.yandex.autotests.directapi.darkside.model.bslogs.clientdata.Campaign;
import ru.yandex.autotests.directapi.darkside.model.bslogs.clientdata.Context;
import ru.yandex.autotests.directapi.enums.AdGroupType;
import ru.yandex.autotests.directapi.enums.CampaignLanguage;
import ru.yandex.autotests.directapi.enums.CampaignType;
import ru.yandex.autotests.directapi.model.api5.campaigns.CampaignAddItemMap;
import ru.yandex.autotests.directapi.model.api5.campaigns.TextCampaignAddItemMap;
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.campaigns.TextCampaignStrategyAddMap;
import ru.yandex.autotests.directapi.rules.ApiSteps;
import ru.yandex.autotests.directapi.rules.Trashman;
import ru.yandex.autotests.directintapi.bstransport.FeatureNames;
import ru.yandex.autotests.directintapi.bstransport.StoriesNames;
import ru.yandex.qatools.Tag;
import ru.yandex.qatools.allure.annotations.Features;
import ru.yandex.qatools.allure.annotations.Issue;
import ru.yandex.qatools.allure.annotations.Step;
import ru.yandex.qatools.allure.annotations.Stories;
import ru.yandex.qatools.allure.annotations.Title;
import ru.yandex.qatools.hazelcast.SemaphoreRule;

import static org.hamcrest.core.IsEqual.equalTo;
import static ru.yandex.autotests.irt.testutils.allure.TestSteps.assertThat;

@Aqua.Test
@Tag(TagDictionary.TRUNK)
@Tag(TagDictionary.RELEASE)
@Title("Отправка поля Banner.Lang для ГО на поиске с учетом настройки языка на кампанию")
@Stories(StoriesNames.MCBANNER_ACTION_SEND)
@Features(FeatureNames.MCBANNER)
@Issue("https://st.yandex-team.ru/DIRECT-68242")
@RunWith(Parameterized.class)
public class BsTransportMcbannerResourcesBodyLangTest {

    private static final String LOGIN = Logins.LOGIN_TRANSPORT_IMG_AD;

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

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

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

    private static int shard;
    private Long cid;
    private Long pid;
    private Long bid;

    @Parameterized.Parameter(0)
    public CampaignType campaignType;

    @Parameterized.Parameter(1)
    public AdGroupType adGroupType;

    @Parameterized.Parameter(2)
    public String imageText;

    @Parameterized.Parameter(3)
    public CampaignLanguage campaignLanguage;

    @Parameterized.Parameter(4)
    public CampaignLanguage expectedLang;

    @Parameterized.Parameters(name = "Тип кампаниии: {0}, тип группы: {1}, image_text: {2}, camp_lang: {3}, " +
            "ожидаемый язык: {4}")
    public static Collection<Object[]> data() {
        Object[][] data = new Object[][]{
                {CampaignType.TEXT, AdGroupType.BASE, "длинное и скучное описание картинки, выставленное для того " +
                        "чтобы отправились и тайтл, и бади",
                        null, CampaignLanguage.RUSSIAN},
                {CampaignType.MOBILE_CONTENT, AdGroupType.MOBILE_CONTENT, "длинное и скучное описание картинки, " +
                        "выставленное для того чтобы отправились и тайтл, и бади",
                        null, CampaignLanguage.RUSSIAN},
                {CampaignType.TEXT, AdGroupType.BASE, "long and boring image description for body and title to be sent",
                        null, CampaignLanguage.RUSSIAN},
                {CampaignType.MOBILE_CONTENT, AdGroupType.MOBILE_CONTENT,
                        "long and boring image description for body and title to be sent",
                        null, CampaignLanguage.RUSSIAN},
                {CampaignType.TEXT, AdGroupType.BASE, "long and boring image description for body and title to be sent",
                        CampaignLanguage.GERMAN, CampaignLanguage.GERMAN},
                {CampaignType.MOBILE_CONTENT, AdGroupType.MOBILE_CONTENT,
                        "long and boring image description for body and title to be sent",
                        CampaignLanguage.GERMAN, CampaignLanguage.GERMAN},
                {CampaignType.TEXT, AdGroupType.BASE, "Başlık türkçe Burada reklam olabilir",
                        null, CampaignLanguage.RUSSIAN},
                {CampaignType.MOBILE_CONTENT, AdGroupType.MOBILE_CONTENT,
                        "Başlık türkçe Burada reklam olabilir",
                        null, CampaignLanguage.RUSSIAN},
                {CampaignType.TEXT, AdGroupType.BASE, "Başlık türkçe Burada reklam olabilir",
                        CampaignLanguage.TURKISH, CampaignLanguage.TURKISH},
                {CampaignType.MOBILE_CONTENT, AdGroupType.MOBILE_CONTENT,
                        "Başlık türkçe Burada reklam olabilir",
                        CampaignLanguage.TURKISH, CampaignLanguage.TURKISH},
        };
        return Arrays.asList(data);
    }

    @BeforeClass
    public static void beforeClass() {
        shard = api.userSteps.clientFakeSteps().getUserShard(LOGIN);
    }

    @Before
    @Step("Подготовка тестовых данных")
    public void init() {
        api.userSteps.clientFakeSteps().fakeClearClientSpentUnits(LOGIN);
        String imageHash = api.userSteps.imagesStepsV5().addImageAdAdImage(1); //240x400
        cid = api.userSteps.campaignSteps().addCampaign(
                new CampaignAddItemMap().defaultCampaignAddItem()
                        .withTextCampaign(new TextCampaignAddItemMap().defaultTextCampaign()
                                .withBiddingStrategy(new TextCampaignStrategyAddMap()
                                        .withNetwork(new TextCampaignNetworkStrategyAddMap().defaultServingOff())
                                        .withSearch(new TextCampaignSearchStrategyAddMap().defaultHighestPosition()))));
        pid = api.userSteps.adGroupsSteps().addDefaultGroup(cid);
        bid = api.userSteps.adsSteps().addDefaultTextImageAd(pid, imageHash);
        Long keywordId = api.userSteps.keywordsSteps().addDefaultKeyword(pid);
        api.userSteps.getDirectJooqDbSteps().useShard(shard);
        CampOptionsRecord campOptions = api.userSteps.getDirectJooqDbSteps().campaignsSteps().getCampOptionsById(cid);
        campOptions.setContentLang(campaignLanguage == null ? null : campaignLanguage.value());
        api.userSteps.getDirectJooqDbSteps().campaignsSteps().updateCampOptions(campOptions);

        api.userSteps.campaignFakeSteps().makeNewCampaignReadyForSendingToBS(cid);
        api.userSteps.groupFakeSteps().makeGroupFullyModerated(pid);
        api.userSteps.bannersFakeSteps().makeBannerFullyModerated(bid);
        api.userSteps.phrasesFakeSteps().makeKeywordModerated(keywordId);
        api.userSteps.getDarkSideSteps().getDirectJooqDbSteps().useShard(shard).imagesSteps()
                .setImageStatusModerate(bid, imageHash, ImagesStatusmoderate.Yes);

        api.userSteps.campaignFakeSteps().setType(cid, CampaignsType.MCBANNER);
        api.userSteps.getDirectJooqDbSteps().useShard(shard).adGroupsSteps()
                .setType(pid, PhrasesAdgroupType.mcbanner);
        api.userSteps.getDirectJooqDbSteps().useShard(shard).bannersSteps()
                .setBannersBannerType(bid, BannersBannerType.mcbanner);
//
//        api.userSteps.getDirectJooqDbSteps().useShard(shard).imagesSteps().setImageText(bid, imageText);
//
    }

    @Test
    public void testSendingBannerLang() {
        RunBsTransportScriptResponse resp
                = api.userSteps.getDarkSideSteps().getTransportSteps().sendNewCampaign(shard, cid);
        Campaign campaign = api.userSteps.getDarkSideSteps().getTransportSteps().getClientDataRequestCampaign(resp, 0, cid);
        Context context = campaign != null ? campaign.getContext(pid) : null;
        Banner banner = context != null ? context.getBanner(bid) : null;
        assertThat("значение Banner.Lang для графического баннера отправлено верно"
                , banner.getLang()
                , equalTo(expectedLang.toString()));
    }
}

