package ru.yandex.autotests.direct.api.campaigns.update.textcampaign.settings;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.util.streamex.StreamEx;

import com.yandex.direct.api.v5.campaigns.CampaignFieldEnum;
import com.yandex.direct.api.v5.campaigns.GetResponse;
import com.yandex.direct.api.v5.campaigns.TextCampaignFieldEnum;
import com.yandex.direct.api.v5.campaigns.TextCampaignSettingGet;
import com.yandex.direct.api.v5.campaigns.TextCampaignSettingsEnum;
import com.yandex.direct.api.v5.campaigns.TextCampaignSettingsGetEnum;
import com.yandex.direct.api.v5.general.YesNoEnum;
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 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.directapi.apiclient.config.Semaphore;
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.CampaignsSelectionCriteriaMap;
import ru.yandex.autotests.directapi.model.api5.campaigns.GetRequestMap;
import ru.yandex.autotests.directapi.model.api5.campaigns.TextCampaignAddItemMap;
import ru.yandex.autotests.directapi.model.api5.campaigns.TextCampaignSettingMap;
import ru.yandex.autotests.directapi.model.api5.campaigns.TextCampaignUpdateItemMap;
import ru.yandex.autotests.directapi.model.api5.campaigns.UpdateRequestMap;
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.Step;
import ru.yandex.qatools.hazelcast.SemaphoreRule;

import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.notNullValue;
import static ru.yandex.autotests.direct.api.campaigns.CampaignsDeprecatedSettings.TEXT_CAMPAIGN_DEPRECATED_SETTINGS;
import static ru.yandex.autotests.direct.api.campaigns.CampaignsDeprecatedSettings.TEXT_CAMPAIGN_DEPRECATED_SETTINGS_GET;
import static ru.yandex.autotests.directapi.matchers.beandiffer2.BeanDifferMatcherV5.beanDifferV5;
import static ru.yandex.autotests.irt.testutils.allure.TestSteps.assertThat;
import static ru.yandex.autotests.irt.testutils.allure.TestSteps.assumeThat;

/**
 * Created by buhter on 04.02.16.
 * https://st.yandex-team.ru/TESTIRT-8435
 */
@Aqua.Test
@Issue("https://st.yandex-team.ru/DIRECT-50402")
@Features(CampaignsFeatures.UPDATE)
@Description("Проверка поля Settings. Независимость изменений с NO на YES")
@RunWith(Parameterized.class)
public class UpdateAndGetSettingsAllNoTest {
    @ClassRule
    public static SemaphoreRule semaphore = Semaphore.getSemaphore();

    private static final String client = CampaignsLogins.GET_SERV_CLIENT;

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

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

    @Parameterized.Parameter(0)
    public TextCampaignSettingsEnum option;

    private Long campaignID;
    private Map<TextCampaignSettingsGetEnum, YesNoEnum> expectedSettings;

    @Parameterized.Parameters(name = "option = {0}")
    public static Collection<Object[]> testData() {
        List<Object[]> data = new ArrayList<>();
        for (TextCampaignSettingsEnum settingsEnum : TextCampaignSettingsEnum.values()) {
            if (TEXT_CAMPAIGN_DEPRECATED_SETTINGS.contains(settingsEnum)) {
                continue;
            }
            data.add(new Object[]{settingsEnum});
        }
        return data;
    }

    @Before
    @Step("Подготовка данных для теста")
    public void prepareCampaign() {
        expectedSettings = new HashMap<>();
        for (TextCampaignSettingsGetEnum settingsEnum : TextCampaignSettingsGetEnum.values()) {
            if (TEXT_CAMPAIGN_DEPRECATED_SETTINGS_GET.contains(settingsEnum)) {
                continue;
            }
            YesNoEnum value = (settingsEnum.value().equals(option.value()) ||
                    settingsEnum.equals(TextCampaignSettingsGetEnum.DAILY_BUDGET_ALLOWED))
                    ? YesNoEnum.YES : YesNoEnum.NO;
            expectedSettings.put(settingsEnum, value);
        }

        ArrayList<TextCampaignSettingMap> settings = new ArrayList<>();
        for (TextCampaignSettingsEnum settingsEnum : TextCampaignSettingsEnum.values()) {
            if (TEXT_CAMPAIGN_DEPRECATED_SETTINGS.contains(settingsEnum)) {
                continue;
            }
            settings.add(new TextCampaignSettingMap().withOption(settingsEnum).withValue(YesNoEnum.NO));
        }
        campaignID = api.userSteps.campaignSteps().addCampaign(
                new CampaignAddItemMap()
                        .defaultCampaignAddItem()
                        .withTextCampaign(new TextCampaignAddItemMap()
                                .defaultTextCampaign()
                                .withSettings(settings.toArray(new TextCampaignSettingMap[settings.size()]))));
    }

    @Test
    @ru.yandex.qatools.allure.annotations.TestCaseId("1104")
    public void checkUpdateSettingsTest() {
        api.userSteps.campaignSteps().campaignsUpdate(new UpdateRequestMap().withCampaigns(new CampaignUpdateItemMap()
                .withId(campaignID)
                .withTextCampaign(new TextCampaignUpdateItemMap()
                        .withSettings(new TextCampaignSettingMap()
                                .withOption(option)
                                .withValue(YesNoEnum.YES)))
        ));

        GetResponse getResponse = api.as(client).userSteps.campaignSteps().campaignsGet(
                new GetRequestMap()
                        .withSelectionCriteria(new CampaignsSelectionCriteriaMap()
                                .withIds(campaignID))
                        .withFieldNames(CampaignFieldEnum.ID)
                        .withTextCampaignFieldNames(TextCampaignFieldEnum.SETTINGS));
        assumeThat("получили созданную кампанию", getResponse.getCampaigns(), hasSize(1));

        List<TextCampaignSettingGet> settings = getResponse.getCampaigns().get(0).getTextCampaign().getSettings();
        assumeThat("получили настройки кампании", settings, notNullValue());
        Map<TextCampaignSettingsGetEnum, YesNoEnum> settingsActual = StreamEx.of(settings)
                .filter(s -> !TEXT_CAMPAIGN_DEPRECATED_SETTINGS_GET.contains(s.getOption()))
                .toMap(TextCampaignSettingGet::getOption, TextCampaignSettingGet::getValue);

        assertThat("вернулся ожидаемый набор настроек", settingsActual, beanDifferV5(expectedSettings));
    }
}
