package io.syndesis.qe.pages.connections.fragments.list;

import static com.codeborne.selenide.Condition.visible;
import static com.codeborne.selenide.Selenide.$;

import io.syndesis.qe.fragments.common.list.CardList;
import io.syndesis.qe.fragments.common.list.actions.ListAction;
import io.syndesis.qe.fragments.common.menu.KebabMenu;
import io.syndesis.qe.pages.ModalDialogPage;
import io.syndesis.qe.utils.TestUtils;

import org.openqa.selenium.By;

import com.codeborne.selenide.SelenideElement;

public class ConnectionsList extends CardList {


    private static final class Element {
        public static final By TECH_PREVIEW = By.xpath("syndesis-card-tech-preview");
        public static final String CONNECTION_CARD = "div[data-testid=\"connection-card-%s-card\"]";
        public static String KEBAB_MENU_SELECTOR = "[id=\"connection-%s-menu\"]";

    }

    public ConnectionsList(By rootElement) {
        super(rootElement);
    }

    @Override
    public void invokeActionOnItem(String title, ListAction action) {

        switch (action) {
            case DELETE:
                KebabMenu kebabMenu = new KebabMenu($(kebabMenu(title)).shouldBe(visible));
                kebabMenu.open();
                kebabMenu.getItemElement("Delete").shouldBe(visible).click();
                TestUtils.sleepForJenkinsDelayIfHigher(3);
                new ModalDialogPage().getButton("Delete").shouldBe(visible).click();
                break;
            default:
                super.invokeActionOnItem(title, action);
        }
    }

    @Override
    public SelenideElement getItem(String title) {
        return $(By.cssSelector(String.format(Element.CONNECTION_CARD, title.toLowerCase().replaceAll(" ", "-"))));
    }

    @Override
    public SelenideElement getTitle(String title) {
        return getItem(title).$(By.cssSelector("data-testid=\"connection-card-title\"")).shouldBe(visible);
    }

    public boolean isConnectionTechPreview(String title) {
        SelenideElement item = getItem(title).parent().$(Element.TECH_PREVIEW);
        return "Technology Preview".equals(item.getText());
    }

    private By kebabMenu(String title) {
        return By.cssSelector(String.format(Element.KEBAB_MENU_SELECTOR, title));
    }
}

@@ -773,16 +773,115 @@
 rd%5C%22%5D%22;%0A
+        public static final String CONNECTION_TITLE = %22h1%5Bdata-testid=%5C%22connection-card-title%5C%22%5D%22;%0A
         
@@ -1549,16 +1549,231 @@
  break;%0A
+            case CLICK:%0A                getItem(title)%0A                    .find(By.cssSelector(Element.CONNECTION_TITLE))%0A                    .shouldBe(visible)%0A                    .click();%0A                break;%0A
         
@@ -1994,16 +1994,28 @@
 ON_CARD,
+%0A           
  title.t

fix gmail (and maybe more) connection creation in firefox on react UI