package ru.yandex.autotests.direct.api.campaigns.update.mobilecampaign;

import com.yandex.direct.api.v5.campaigns.DailyBudgetModeEnum;
import com.yandex.direct.api.v5.campaigns.MobileAppCampaignNetworkStrategyTypeEnum;
import com.yandex.direct.api.v5.campaigns.MobileAppCampaignSearchStrategyTypeEnum;
import com.yandex.direct.api.v5.general.YesNoEnum;
import org.joda.time.DateTime;
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.campaigns.CampaignsFeatures;
import ru.yandex.autotests.direct.api.campaigns.CampaignsLogins;
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.Semaphore;
import ru.yandex.autotests.directapi.darkside.datacontainers.jsonrpc.fake.CampaignFakeInfo;
import ru.yandex.autotests.directapi.model.api5.campaigns.CampaignAddItemMap;
import ru.yandex.autotests.directapi.model.api5.campaigns.CampaignUpdateItemMap;
import ru.yandex.autotests.directapi.model.api5.campaigns.DailyBudgetMap;
import ru.yandex.autotests.directapi.model.api5.campaigns.EmailSettingsMap;
import ru.yandex.autotests.directapi.model.api5.campaigns.MobileAppCampaignAddItemMap;
import ru.yandex.autotests.directapi.model.api5.campaigns.MobileAppCampaignNetworkStrategyAddMap;
import ru.yandex.autotests.directapi.model.api5.campaigns.MobileAppCampaignNetworkStrategyMap;
import ru.yandex.autotests.directapi.model.api5.campaigns.MobileAppCampaignSearchStrategyAddMap;
import ru.yandex.autotests.directapi.model.api5.campaigns.MobileAppCampaignSearchStrategyMap;
import ru.yandex.autotests.directapi.model.api5.campaigns.MobileAppCampaignStrategyAddMap;
import ru.yandex.autotests.directapi.model.api5.campaigns.MobileAppCampaignStrategyMap;
import ru.yandex.autotests.directapi.model.api5.campaigns.MobileAppCampaignUpdateItemMap;
import ru.yandex.autotests.directapi.model.api5.campaigns.NotificationMap;
import ru.yandex.autotests.directapi.model.api5.campaigns.SmsSettingsMap;
import ru.yandex.autotests.directapi.model.api5.campaigns.TimeTargetingMap;
import ru.yandex.autotests.directapi.model.api5.campaigns.TimeTargetingOnPublicHolidaysMap;
import ru.yandex.autotests.directapi.model.api5.campaigns.UpdateRequestMap;
import ru.yandex.autotests.directapi.model.api5.general.ExpectedResult;
import ru.yandex.autotests.directapi.model.campaigns.TimeTargetMap;
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.Description;
import ru.yandex.qatools.allure.annotations.Features;
import ru.yandex.qatools.allure.annotations.Issue;
import ru.yandex.qatools.hazelcast.SemaphoreRule;

import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.not;
import static ru.yandex.autotests.irt.testutils.allure.TestSteps.assumeThat;

/**
 * Created by pavryabov on 01.10.15.
 * https://st.yandex-team.ru/TESTIRT-7291
 */
@Aqua.Test
@Features(CampaignsFeatures.UPDATE)
@Description("Поверка LastChange после обновления кампании РМП")
@Issue("https://st.yandex-team.ru/DIRECT-43063")
@Tag(TagDictionary.TRUNK)
public class CheckLastChangeAfterUpdateMobileAppCampaignTest {

    private static final String SSP_PLATFORM_DEFAULT = "Rubicon";
    private static final String DOMAIN = "qwerty.ru";
    private static final String CLIENT = CampaignsLogins.UPDATE_CLIENT;

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

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

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

    private static Long id;
    private String lastChange;

    @BeforeClass
    public static void initTest() {
        api.userSteps.getDirectJooqDbSteps().sspPlatformsSteps().saveSspPlatformsIfNotExist(SSP_PLATFORM_DEFAULT);
        api.userSteps.clientFakeSteps().fakeClearClientSpentUnits(CLIENT);
        id = api.userSteps.campaignSteps().addCampaign(new CampaignAddItemMap()
                .withName("name")
                .withStartDate(DateTime.now().toLocalDate().toString())
                .withMobileAppCampaign(new MobileAppCampaignAddItemMap()
                        .withBiddingStrategy(new MobileAppCampaignStrategyAddMap()
                                .withSearch(new MobileAppCampaignSearchStrategyAddMap()
                                        .withBiddingStrategyType(
                                                MobileAppCampaignSearchStrategyTypeEnum.HIGHEST_POSITION))
                                .withNetwork(new MobileAppCampaignNetworkStrategyAddMap()
                                        .withBiddingStrategyType(
                                                MobileAppCampaignNetworkStrategyTypeEnum.MAXIMUM_COVERAGE)))));
    }

    @Before
    public void setLastChange() {
        lastChange = DateTime.now().minusMinutes(10).toString("yyyy-MM-dd hh:mm:ss");
        api.userSteps.campaignFakeSteps().setLastChange(id, lastChange);
        CampaignFakeInfo campaignFakeInfo = api.userSteps.campaignFakeSteps().fakeGetCampaignParams(id);
        assumeThat("LastChange у кампании сдвинут в прошлое", campaignFakeInfo.getLastChange(), equalTo(lastChange));
    }

    @Test
    @ru.yandex.qatools.allure.annotations.TestCaseId("763")
    public void updateClientInfo() {
        api.userSteps.campaignSteps().shouldGetResultOnUpdate(new UpdateRequestMap()
                        .withCampaigns(new CampaignUpdateItemMap()
                                .withId(id)
                                .withClientInfo("someNewClientInfo")),
                ExpectedResult.success(id));
        CampaignFakeInfo campaignFakeInfo = api.userSteps.campaignFakeSteps().fakeGetCampaignParams(id);
        assumeThat("LastChange у кампании обновился",
                campaignFakeInfo.getLastChange(), not(equalTo(lastChange)));
    }

    @Test
    @ru.yandex.qatools.allure.annotations.TestCaseId("764")
    public void updateNotification() {
        api.userSteps.campaignSteps().shouldGetResultOnUpdate(new UpdateRequestMap()
                        .withCampaigns(new CampaignUpdateItemMap()
                                .withId(id)
                                .withNotification(new NotificationMap()
                                        .withSmsSettings(new SmsSettingsMap()
                                                .withTimeFrom("20:00")
                                                .withTimeTo("20:15"))
                                        .withEmailSettings(new EmailSettingsMap().withEmail("somenewemail@mail.ru")))),
                ExpectedResult.success(id));
        CampaignFakeInfo campaignFakeInfo = api.userSteps.campaignFakeSteps().fakeGetCampaignParams(id);
        assumeThat("LastChange у кампании обновился",
                campaignFakeInfo.getLastChange(), not(equalTo(lastChange)));
    }

    @Test
    @ru.yandex.qatools.allure.annotations.TestCaseId("765")
    public void updateEndDate() {
        api.userSteps.campaignSteps().shouldGetResultOnUpdate(new UpdateRequestMap()
                        .withCampaigns(new CampaignUpdateItemMap()
                                .withId(id)
                                .withEndDate(DateTime.now().plusDays(23).toLocalDate().toString())),
                ExpectedResult.success(id));
        CampaignFakeInfo campaignFakeInfo = api.userSteps.campaignFakeSteps().fakeGetCampaignParams(id);
        assumeThat("LastChange у кампании обновился",
                campaignFakeInfo.getLastChange(), not(equalTo(lastChange)));
    }

    @Test
    @ru.yandex.qatools.allure.annotations.TestCaseId("766")
    public void updateStartDate() {
        api.userSteps.campaignSteps().shouldGetResultOnUpdate(new UpdateRequestMap()
                        .withCampaigns(new CampaignUpdateItemMap()
                                .withId(id)
                                .withStartDate(DateTime.now().plusDays(1).toLocalDate().toString())),
                ExpectedResult.success(id));
        CampaignFakeInfo campaignFakeInfo = api.userSteps.campaignFakeSteps().fakeGetCampaignParams(id);
        assumeThat("LastChange у кампании обновился",
                campaignFakeInfo.getLastChange(), not(equalTo(lastChange)));
    }

    @Test
    @ru.yandex.qatools.allure.annotations.TestCaseId("767")
    public void updateTimeTargeting() {
        //DIRECT-45952
        api.userSteps.campaignSteps().shouldGetResultOnUpdate(new UpdateRequestMap()
                        .withCampaigns(new CampaignUpdateItemMap()
                                .withId(id)
                                .withTimeTargeting(new TimeTargetingMap()
                                        .withConsiderWorkingWeekends(YesNoEnum.YES)
                                        .withHolidaysSchedule(new TimeTargetingOnPublicHolidaysMap()
                                                .withSuspendOnHolidays(YesNoEnum.NO)
                                                .withStartHour(15)
                                                .withEndHour(16)))),
                ExpectedResult.success(id));
        CampaignFakeInfo campaignFakeInfo = api.userSteps.campaignFakeSteps().fakeGetCampaignParams(id);
        assumeThat("LastChange у кампании обновился",
                campaignFakeInfo.getLastChange(), not(equalTo(lastChange)));
    }

    @Test
    @ru.yandex.qatools.allure.annotations.TestCaseId("768")
    public void updateTimeZone() {
        api.userSteps.campaignSteps().shouldGetResultOnUpdate(new UpdateRequestMap()
                        .withCampaigns(new CampaignUpdateItemMap()
                                .withId(id)
                                .withTimeZone(TimeTargetMap.TimeZone.ASIA_VIENTIANE)),
                ExpectedResult.success(id));
        CampaignFakeInfo campaignFakeInfo = api.userSteps.campaignFakeSteps().fakeGetCampaignParams(id);
        assumeThat("LastChange у кампании обновился",
                campaignFakeInfo.getLastChange(), not(equalTo(lastChange)));
    }

    @Test
    @ru.yandex.qatools.allure.annotations.TestCaseId("769")
    public void updateNegativeKeywords() {
        api.userSteps.campaignSteps().shouldGetResultOnUpdate(new UpdateRequestMap()
                        .withCampaigns(new CampaignUpdateItemMap()
                                .withId(id)
                                .withNegativeKeywords("newNegativeKeyword")),
                ExpectedResult.success(id));
        CampaignFakeInfo campaignFakeInfo = api.userSteps.campaignFakeSteps().fakeGetCampaignParams(id);
        assumeThat("LastChange у кампании обновился",
                campaignFakeInfo.getLastChange(), not(equalTo(lastChange)));
    }

    @Test
    @ru.yandex.qatools.allure.annotations.TestCaseId("770")
    public void updateBlockedIps() {
        api.userSteps.campaignSteps().shouldGetResultOnUpdate(new UpdateRequestMap()
                        .withCampaigns(new CampaignUpdateItemMap()
                                .withId(id)
                                .withBlockedIps("12.32.32.21")),
                ExpectedResult.success(id));
        CampaignFakeInfo campaignFakeInfo = api.userSteps.campaignFakeSteps().fakeGetCampaignParams(id);
        assumeThat("LastChange у кампании обновился",
                campaignFakeInfo.getLastChange(), not(equalTo(lastChange)));
    }

    @Test
    @ru.yandex.qatools.allure.annotations.TestCaseId("771")
    public void updateExcludedSitesDomain() {
        api.userSteps.campaignSteps().shouldGetResultOnUpdate(new UpdateRequestMap()
                        .withCampaigns(new CampaignUpdateItemMap()
                                .withId(id)
                                .withExcludedSites(DOMAIN)),
                ExpectedResult.success(id));
        CampaignFakeInfo campaignFakeInfo = api.userSteps.campaignFakeSteps().fakeGetCampaignParams(id);
        assumeThat("LastChange у кампании обновился",
                campaignFakeInfo.getLastChange(), not(equalTo(lastChange)));
    }

    @Test
    @ru.yandex.qatools.allure.annotations.TestCaseId("772")
    public void updateExcludedSitesSSPPlatform() {
        api.userSteps.campaignSteps().shouldGetResultOnUpdate(new UpdateRequestMap()
                        .withCampaigns(new CampaignUpdateItemMap()
                                .withId(id)
                                .withExcludedSites(SSP_PLATFORM_DEFAULT)),
                ExpectedResult.success(id));
        CampaignFakeInfo campaignFakeInfo = api.userSteps.campaignFakeSteps().fakeGetCampaignParams(id);
        assumeThat("LastChange у кампании обновился",
                campaignFakeInfo.getLastChange(), not(equalTo(lastChange)));
    }

    @Test
    @ru.yandex.qatools.allure.annotations.TestCaseId("773")
    public void updateName() {

        api.userSteps.campaignSteps().shouldGetResultOnUpdate(new UpdateRequestMap()
                        .withCampaigns(new CampaignUpdateItemMap()
                                .withId(id)
                                .withName("someNewName")),
                ExpectedResult.success(id));
        CampaignFakeInfo campaignFakeInfo = api.userSteps.campaignFakeSteps().fakeGetCampaignParams(id);
        assumeThat("LastChange у кампании обновился",
                campaignFakeInfo.getLastChange(), not(equalTo(lastChange)));
    }

    @Test
    @ru.yandex.qatools.allure.annotations.TestCaseId("774")
    public void updateDailyBudget() {
        api.userSteps.campaignSteps().shouldGetResultOnUpdate(new UpdateRequestMap()
                        .withCampaigns(new CampaignUpdateItemMap()
                                .withId(id)
                                .withDailyBudget(new DailyBudgetMap()
                                        .withAmount(
                                                MoneyCurrency.get(Currency.RUB).getMinDayBudgetAmount().bidLong().longValue())
                                        .withMode(DailyBudgetModeEnum.DISTRIBUTED))),
                ExpectedResult.success(id));
        CampaignFakeInfo campaignFakeInfo = api.userSteps.campaignFakeSteps().fakeGetCampaignParams(id);
        assumeThat("LastChange у кампании обновился",
                campaignFakeInfo.getLastChange(), not(equalTo(lastChange)));
    }

    @Test
    @ru.yandex.qatools.allure.annotations.TestCaseId("775")
    public void updateNetworkStrategy() {
        api.userSteps.campaignSteps().shouldGetResultOnUpdate(new UpdateRequestMap()
                        .withCampaigns(new CampaignUpdateItemMap()
                                .withId(id)
                                .withMobileAppCampaign(new MobileAppCampaignUpdateItemMap()
                                        .withBiddingStrategy(new MobileAppCampaignStrategyMap()
                                                .withSearch(new MobileAppCampaignSearchStrategyMap()
                                                        .withBiddingStrategyType(
                                                                MobileAppCampaignSearchStrategyTypeEnum.HIGHEST_POSITION))
                                                .withNetwork(new MobileAppCampaignNetworkStrategyMap()
                                                        .withBiddingStrategyType(
                                                                MobileAppCampaignNetworkStrategyTypeEnum.SERVING_OFF))))),
                ExpectedResult.success(id));
        CampaignFakeInfo campaignFakeInfo = api.userSteps.campaignFakeSteps().fakeGetCampaignParams(id);
        assumeThat("LastChange у кампании обновился",
                campaignFakeInfo.getLastChange(), not(equalTo(lastChange)));
    }
}
