package ru.yandex.autotests.directapi.vcards.delete;

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

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.utils.tags.TagDictionary;
import ru.yandex.autotests.directapi.VCardsFeatures;
import ru.yandex.autotests.directapi.VCardsLogins;
import ru.yandex.autotests.directapi.apiclient.config.Semaphore;
import ru.yandex.autotests.directapi.model.Logins;
import ru.yandex.autotests.directapi.model.api5.vcards.VCardAddMap;
import ru.yandex.autotests.directapi.rules.ApiSteps;
import ru.yandex.autotests.directapi.rules.Trashman;
import ru.yandex.autotests.irt.testutils.allure.LogSteps;
import ru.yandex.qatools.Tag;
import ru.yandex.qatools.allure.annotations.Features;
import ru.yandex.qatools.hazelcast.SemaphoreRule;

import static org.hamcrest.Matchers.hasSize;
import static ru.yandex.autotests.irt.testutils.allure.TestSteps.assertThat;

/**
 * Created by chicos on 09.10.14.
 */
@Aqua.Test
@Features(VCardsFeatures.DELETE)
@Tag(TagDictionary.TRUNK)
@RunWith(Parameterized.class)
public class DeleteVCardsRolesAccessClientTest {
    private static LogSteps log = LogSteps.getLogger(DeleteVCardsRolesAccessClientTest.class);

    private static final String singleClient = VCardsLogins.SINGLE_CLIENT_3;

    private static long vCardId;
    private static long campaignId;

    @ClassRule
    public static ApiSteps api = new ApiSteps();

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

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

    @Parameterized.Parameter(value = 0)
    public String operator;

    @Parameterized.Parameter(value = 1)
    public String client;

    @Parameterized.Parameters(name = "operator = {0}")
    public static Collection logins() {
        Object[][] data = new Object[][]{
                {VCardsLogins.SINGLE_CLIENT_3_REP,
                        null},
                {Logins.SUPPORT,
                        singleClient},
        };
        return Arrays.asList(data);
    }

    @BeforeClass
    public static void prepareCampaign() {
        log.info("Создадим кампанию пользователем");
        api.as(singleClient);
        campaignId = api.userSteps.campaignSteps().addDefaultTextCampaign(singleClient);
    }

    @Before
    public void prepareVCards() {
        log.info("Создадим визитку пользователем");
        api.as(singleClient);
        VCardAddMap item = new VCardAddMap().defaultContacts().withCampaignId(campaignId);
        vCardId = api.userSteps.vCardsSteps().vCardsAdd(item).get(0);
    }

    @Test
    @ru.yandex.qatools.allure.annotations.TestCaseId("3995")
    public void deleteVCardByRoleTest() {
        log.info("Удалим визитку ролью");
        api.as(operator);
        List<Long> cardIds;
        if (client != null){
            cardIds = api.userSteps.vCardsSteps().vCardsDelete(client, vCardId);
        }else {
            cardIds = api.userSteps.vCardsSteps().vCardsDelete(vCardId);
        }

        assertThat("размер списка IDs удаленных визиток", cardIds, hasSize(1));
    }
}
