/*-
 * #%L
 * Bobcat
 * %%
 * Copyright (C) 2016 Cognifide Ltd.
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */
package com.cognifide.qa.bb.aem.core.component.dialog.dialogfields;

import static org.openqa.selenium.support.ui.ExpectedConditions.elementToBeClickable;

import java.util.List;

import org.openqa.selenium.Keys;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;

import com.cognifide.qa.bb.qualifier.Global;
import com.cognifide.qa.bb.qualifier.PageObject;
import com.cognifide.qa.bb.wait.BobcatWait;
import com.google.inject.Inject;

/**
 * Default implementation of {@link PathBrowser}
 */
@PageObject(css = Locators.AUTOCOMPLETE_CSS)
public class ContentFragmentPathBrowserImpl implements ContentFragmentPathBrowser {

  @FindBy(css = ".coral3-Textfield")
  private WebElement input;

  @FindBy(xpath = Locators.ALTERNATE_LABEL_XPATH)
  private List<WebElement> label;

  @FindBy(css = ".foundation-picker-buttonlist.coral3-Overlay.is-open")
  private WebElement firstResult;

  @Global
  @FindBy(css = "coral3-Dialog--warning is-open .coral3-Button--primary")
  private WebElement warningConfirmation;

  @Inject
  private BobcatWait bobcatWait;

  @Override
  public void setValue(Object value) {
    input.clear();
    input.sendKeys(String.valueOf(value));
    bobcatWait.until(elementToBeClickable(firstResult));
    input.sendKeys(Keys.ENTER);
    bobcatWait.until(elementToBeClickable(warningConfirmation)).click();
  }

  @Override
  public String getLabel() {
    return label.isEmpty() ? "" : label.get(0).getText();
  }
}

@@ -763,36 +763,17 @@
 ditions.
-elementToBeClickable
+*
 ;%0A%0Aimpor
@@ -789,16 +789,125 @@
 il.List;
+%0Aimport java.util.NoSuchElementException;%0Aimport java.util.stream.Collectors;%0Aimport java.util.stream.Stream;
 %0A%0Aimport
@@ -1154,16 +1154,64 @@
 atWait;%0A
+import com.cognifide.qa.bb.wait.TimingsBuilder;%0A
 import c
@@ -1708,16 +1708,17 @@
 (css = %22
+.
 coral3-D
@@ -1731,17 +1731,17 @@
 -warning
- 
+.
 is-open 
@@ -2059,80 +2059,625 @@
 R);%0A
+%0A
     
-bobcatWait.until(elementToBeClickable(warningConfirmation)).click();
+// Branching logic in tests is not OK, it's true. But this part handles an unpredictable part of%0A    // the application. Sometimes a warning dialog appears and it needs to be handled.%0A    if (bobcatWait%0A        .tweak(new TimingsBuilder().explicitTimeout(1).build())%0A        .ignoring(Stream.of(NoSuchElementException.class).collect(Collectors.toList()))%0A        .isConditionMet(elementToBeClickable(warningConfirmation))) %7B%0A      warningConfirmation.click();%0A      bobcatWait.tweak(new TimingsBuilder().explicitTimeout(1).build())%0A          .isConditionMet(not(elementToBeClickable(warningConfirmation)));%0A    %7D
 %0A  %7D

Fix handling the content fragment warning dialog