package ru.yandex.autotests.direct.api.adgroups.add.dynamictextadgroup;

import org.junit.Before;
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.adgroups.AdGroupsFeatures;
import ru.yandex.autotests.direct.api.adgroups.AdGroupsLogins;
import ru.yandex.autotests.direct.api.adgroups.AdGroupsStories;
import ru.yandex.autotests.directapi.apiclient.config.Semaphore;
import ru.yandex.autotests.directapi.model.api5.adgroups.AdGroupAddItemMap;
import ru.yandex.autotests.directapi.model.api5.adgroups.AddRequestMap;
import ru.yandex.autotests.directapi.model.api5.adgroups.DynamicTextAdGroupMap;
import ru.yandex.autotests.directapi.model.api5.general.ExpectedResult;
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.Issue;
import ru.yandex.qatools.allure.annotations.Stories;
import ru.yandex.qatools.hazelcast.SemaphoreRule;

/**
 * Created by buhter on 25.11.15.
 * https://st.yandex-team.ru/TESTIRT-7819
 */
@Aqua.Test
@Features(AdGroupsFeatures.ADD)
@Stories(AdGroupsStories.DYNAMIC_TEXT)
@Description("Создание групп ДТО")
@Issue("https://st.yandex-team.ru/DIRECT-41334")
public class DynamicTextAdGroupAddTest {

    private static final String client = AdGroupsLogins.CLIENT;

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

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

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

    static Integer campaignId;
    static Integer campaignIdElse;

    @BeforeClass
    public static void initTest() {
        campaignId = api.userSteps.campaignSteps().addDefaultDynamicTextCampaign().intValue();
        campaignIdElse = api.userSteps.campaignSteps().addDefaultDynamicTextCampaign().intValue();
    }

    @Before
    public void clearSpentUnits() {
        api.userSteps.clientFakeSteps().fakeClearClientSpentUnits(client);
    }

    @Test
    public void addOneGroup() {
        api.userSteps.adGroupsSteps().shouldGetResultOnAdd(
                new AddRequestMap().withAdGroups(
                        new AdGroupAddItemMap().defaultAdGroupAddItem(campaignId.longValue())
                                .withDynamicTextAdGroup(new DynamicTextAdGroupMap().defaultGroup())
                ),
                ExpectedResult.success()
        );
    }

    @Test
    public void addTwoGroupsInOneCampaign() {
        api.userSteps.adGroupsSteps().shouldGetResultOnAdd(
                new AddRequestMap().withAdGroups(
                        new AdGroupAddItemMap().defaultAdGroupAddItem(campaignId.longValue())
                                .withDynamicTextAdGroup(new DynamicTextAdGroupMap().defaultGroup()),
                        new AdGroupAddItemMap().defaultAdGroupAddItem(campaignId.longValue())
                                .withDynamicTextAdGroup(new DynamicTextAdGroupMap().defaultGroup())
                ),
                ExpectedResult.success(), ExpectedResult.success()
        );
    }

    @Test
    public void addTwoGroupsInTwoCampaigns() {
        api.userSteps.adGroupsSteps().shouldGetResultOnAdd(
                new AddRequestMap().withAdGroups(
                        new AdGroupAddItemMap().defaultAdGroupAddItem(campaignId.longValue())
                                .withDynamicTextAdGroup(new DynamicTextAdGroupMap().defaultGroup()),
                        new AdGroupAddItemMap().defaultAdGroupAddItem(campaignIdElse.longValue())
                                .withDynamicTextAdGroup(new DynamicTextAdGroupMap().defaultGroup())
                ),
                ExpectedResult.success(), ExpectedResult.success()
        );
    }
}
