package io.syndesis.qe.steps.integrations.editor.add;

import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;

import static org.hamcrest.Matchers.is;

import static com.codeborne.selenide.CollectionCondition.sizeGreaterThanOrEqual;
import static com.codeborne.selenide.Condition.visible;

import org.openqa.selenium.By;

import com.codeborne.selenide.ElementsCollection;

import java.util.ArrayList;
import java.util.List;

import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
import io.syndesis.qe.fragments.common.list.actions.ListAction;
import io.syndesis.qe.pages.connections.fragments.list.ConnectionsList;
import io.syndesis.qe.pages.integrations.editor.AddToIntegration;
import io.syndesis.qe.pages.integrations.editor.Editor;
import io.syndesis.qe.pages.integrations.editor.add.ChooseStep;
import io.syndesis.qe.pages.integrations.editor.add.steps.getridof.AbstractStep;
import io.syndesis.qe.pages.integrations.editor.add.steps.getridof.StepFactory;
import io.syndesis.qe.pages.integrations.fragments.IntegrationFlowView;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class ChooseConnectionSteps {

    private ConnectionsList connectionsList = new ConnectionsList(By.xpath("//syndesis-connections-list"));
    private IntegrationFlowView integrationFlowView = new IntegrationFlowView();
    private AddToIntegration addToIntegration = new AddToIntegration();
    private ChooseStep chooseStep = new ChooseStep();
    private StepFactory stepFactory = new StepFactory();
    private Editor editor = new Editor();

    @Then("^she adds \"(\\d+)\" random steps and then checks the structure$")
    public void addRandomStepsAndCheckRest(Integer numberOfSteps) {
        log.info("Adding random phases");
        List<String> list = integrationFlowView.getStepsArray();
        addToIntegration.getButton("Add a Step").shouldBe(visible).click();
        ElementsCollection links = addToIntegration.getLinks("Add a step");
        Integer count = links.size();
        List<Integer> randomIndexes = new ArrayList<>();
        for (int i = 0; i < numberOfSteps; i++) {
            randomIndexes.add((int) Math.floor((Math.random() * count)));
        }
        for (int randomIndex : randomIndexes) {
            links.get(randomIndex).click();
            String stepType = "Basic Filter";
            String stepParameter = "ANY of the following, pathx " + randomIndex + ", Contains, valuex " + randomIndex;
            chooseStep.chooseStep(stepType);
            AbstractStep stepComponent = stepFactory.getStep(stepType, stepParameter);
            stepComponent.fillConfiguration();
            editor.getButton("Next").shouldBe(visible).click();
            addToIntegration.getButton("Add a Step").shouldBe(visible).click();
            list.add(randomIndex, stepParameter);
        }
        List<String> list2 = integrationFlowView.getStepsArray();
        for (int i = 0; i < list2.size(); i++) {
            log.info("assserting {} and {}", list.get(i), list2.get(i));
            assertThat(list.get(i), is(list2.get(i)));
        }
    }

    //what rest??
    @Then("^she deletes \"(\\d+)\" random integration steps and checks the rest$")
    public void deleteRandomStepsAndCheckRest(Integer numberOfSteps) {
        log.info("Deleting random phases");
        List<String> list = integrationFlowView.getStepsArray();
        ElementsCollection deletes = integrationFlowView.getAllTrashes().shouldBe(sizeGreaterThanOrEqual(1));
        int count = deletes.size();
        List<Integer> randomIndexes = new ArrayList<>();
        for (int i = 0; i < numberOfSteps; i++) {
            randomIndexes.add((int) Math.floor(Math.random() * (count - 2 - i)));
        }
        for (Integer randomIndex : randomIndexes) {
            deletes.get(randomIndex + 1).click();
            integrationFlowView.getFirstVisibleButton("OK").shouldBe(visible).click();
            list.remove(randomIndex);
        }
        List<String> list2 = integrationFlowView.getStepsArray();
        for (int i = 0; i < list.size(); i++) {
            log.info("assserting {} and {", list.get(i), list2.get(i));
            assertThat(list.get(i), is(list2.get(i)));
        }
    }

    //what rest???
    @Then("^she deletes step on position \"(\\d+)\" and checks the rest$")
    public void deleteStepOnPositionAndCheckRest(Integer positionOfStep) {
        log.info("Deleting step on position {}", positionOfStep);
        List<String> list = integrationFlowView.getStepsArray();
        ElementsCollection deletes = integrationFlowView.getAllTrashes().shouldBe(sizeGreaterThanOrEqual(1));
        Integer indexOfStep = positionOfStep + 1;
        deletes.get(indexOfStep).click();
        editor.getFirstVisibleButton("OK");
        list.remove(positionOfStep);
        //NOW CHECK:
        List<String> list2 = integrationFlowView.getStepsArray();
        for (int i = 0; i < list.size(); i++) {
            log.info("assserting {} and {}", list.get(i), list2.get(i));
            assertThat(list.get(i), is(list2.get(i)));
        }
    }

    /**
     * whether it's start or finish connection
     *
     * @param position
     */
    @Then("^she is prompted to select a \"([^\"]*)\" connection from a list of available connections$")
    public void verifyTypeOfConnection(String position) {
        log.info("{} connection must be active", position);
        assertTrue("There was no active icon found for position " + position, integrationFlowView.verifyActivePosition(position));
    }

    @When("^.*selects? the \"([^\"]*)\" connection$")
    public void selectConnection(String connectionName) {
        connectionsList.invokeActionOnItem(connectionName, ListAction.CLICK);
    }

}

@@ -1344,3751 +1344,8 @@
 w();
-%0A    private AddToIntegration addToIntegration = new AddToIntegration();%0A    private ChooseStep chooseStep = new ChooseStep();%0A    private StepFactory stepFactory = new StepFactory();%0A    private Editor editor = new Editor();%0A%0A    @Then(%22%5Eshe adds %5C%22(%5C%5Cd+)%5C%22 random steps and then checks the structure$%22)%0A    public void addRandomStepsAndCheckRest(Integer numberOfSteps) %7B%0A        log.info(%22Adding random phases%22);%0A        List%3CString%3E list = integrationFlowView.getStepsArray();%0A        addToIntegration.getButton(%22Add a Step%22).shouldBe(visible).click();%0A        ElementsCollection links = addToIntegration.getLinks(%22Add a step%22);%0A        Integer count = links.size();%0A        List%3CInteger%3E randomIndexes = new ArrayList%3C%3E();%0A        for (int i = 0; i %3C numberOfSteps; i++) %7B%0A            randomIndexes.add((int) Math.floor((Math.random() * count)));%0A        %7D%0A        for (int randomIndex : randomIndexes) %7B%0A            links.get(randomIndex).click();%0A            String stepType = %22Basic Filter%22;%0A            String stepParameter = %22ANY of the following, pathx %22 + randomIndex + %22, Contains, valuex %22 + randomIndex;%0A            chooseStep.chooseStep(stepType);%0A            AbstractStep stepComponent = stepFactory.getStep(stepType, stepParameter);%0A            stepComponent.fillConfiguration();%0A            editor.getButton(%22Next%22).shouldBe(visible).click();%0A            addToIntegration.getButton(%22Add a Step%22).shouldBe(visible).click();%0A            list.add(randomIndex, stepParameter);%0A        %7D%0A        List%3CString%3E list2 = integrationFlowView.getStepsArray();%0A        for (int i = 0; i %3C list2.size(); i++) %7B%0A            log.info(%22assserting %7B%7D and %7B%7D%22, list.get(i), list2.get(i));%0A            assertThat(list.get(i), is(list2.get(i)));%0A        %7D%0A    %7D%0A%0A    //what rest??%0A    @Then(%22%5Eshe deletes %5C%22(%5C%5Cd+)%5C%22 random integration steps and checks the rest$%22)%0A    public void deleteRandomStepsAndCheckRest(Integer numberOfSteps) %7B%0A        log.info(%22Deleting random phases%22);%0A        List%3CString%3E list = integrationFlowView.getStepsArray();%0A        ElementsCollection deletes = integrationFlowView.getAllTrashes().shouldBe(sizeGreaterThanOrEqual(1));%0A        int count = deletes.size();%0A        List%3CInteger%3E randomIndexes = new ArrayList%3C%3E();%0A        for (int i = 0; i %3C numberOfSteps; i++) %7B%0A            randomIndexes.add((int) Math.floor(Math.random() * (count - 2 - i)));%0A        %7D%0A        for (Integer randomIndex : randomIndexes) %7B%0A            deletes.get(randomIndex + 1).click();%0A            integrationFlowView.getFirstVisibleButton(%22OK%22).shouldBe(visible).click();%0A            list.remove(randomIndex);%0A        %7D%0A        List%3CString%3E list2 = integrationFlowView.getStepsArray();%0A        for (int i = 0; i %3C list.size(); i++) %7B%0A            log.info(%22assserting %7B%7D and %7B%22, list.get(i), list2.get(i));%0A            assertThat(list.get(i), is(list2.get(i)));%0A        %7D%0A    %7D%0A%0A    //what rest???%0A    @Then(%22%5Eshe deletes step on position %5C%22(%5C%5Cd+)%5C%22 and checks the rest$%22)%0A    public void deleteStepOnPositionAndCheckRest(Integer positionOfStep) %7B%0A        log.info(%22Deleting step on position %7B%7D%22, positionOfStep);%0A        List%3CString%3E list = integrationFlowView.getStepsArray();%0A        ElementsCollection deletes = integrationFlowView.getAllTrashes().shouldBe(sizeGreaterThanOrEqual(1));%0A        Integer indexOfStep = positionOfStep + 1;%0A        deletes.get(indexOfStep).click();%0A        editor.getFirstVisibleButton(%22OK%22);%0A        list.remove(positionOfStep);%0A        //NOW CHECK:%0A        List%3CString%3E list2 = integrationFlowView.getStepsArray();%0A        for (int i = 0; i %3C list.size(); i++) %7B%0A            log.info(%22assserting %7B%7D and %7B%7D%22, list.get(i), list2.get(i));%0A            assertThat(list.get(i), is(list2.get(i)));%0A        %7D%0A    %7D
 %0A%0A  

Remove unused cucumber steps