public static void fillShippingInformation(List<List<String>> shippingInfo) {
    if (!SeleniumController.Instance.Driver().findElement(By.id("address-list")).isDisplayed()) {
      for (int i = 0; i < shippingInfo.size(); i++) {
        SeleniumController.Instance.Driver()
            .findElement(By.id("addressFields"))
            .findElement(By.className(shippingInfo.get(i).get(0)))
            .sendKeys(shippingInfo.get(i).get(1));
        if (shippingInfo.get(i).get(0).equals("country")) {
          SeleniumController.Instance.Driver()
              .findElement(By.id("addressFields"))
              .findElement(By.className(shippingInfo.get(i).get(0)))
              .sendKeys(Keys.ENTER);
          if (shippingInfo.get(i).get(1).contains("United States")) {
            wait.until(
                ExpectedConditions.textToBePresentInElement(
                    SeleniumController.Instance.Driver()
                        .findElement(By.className("shipMethods"))
                        .findElement(By.id("shipMethods")),
                    "U.S."));

          } else
            wait.until(
                ExpectedConditions.textToBePresentInElement(
                    SeleniumController.Instance.Driver()
                        .findElement(By.className("shipMethods"))
                        .findElement(By.id("shipMethods"))
                        .findElement(By.className("shipDescription")),
                    "International"));
        }
      }
    }
  }
 public static void continueToPaymentPage() {
   wait.until(
       ExpectedConditions.visibilityOf(
           SeleniumController.Instance.Driver().findElement(By.id("shipMethods"))));
   WebElement element =
       SeleniumController.Instance.Driver()
           .findElement(
               By.cssSelector(
                   "html.no-js body.rfk_wbh div#pageContainer div#main d"
                       + "iv#checkout div.buttonWrapper.action button.button.primary.fullwidth"));
   element.click();
 }
 public static void selectUseEnteredAddressButton() {
   if (!SeleniumController.Instance.Driver().findElement(By.id("address-list")).isDisplayed()) {
     WebElement element =
         wait.until(
             ExpectedConditions.visibilityOf(
                 SeleniumController.Instance.Driver()
                     .findElement(
                         By.cssSelector(
                             "html.no-js body.rfk_wbh div#pageContainer div#main div#checkout div.buttonWrapper.action button#useAddress.foldContent.button.fullwidth.address-not-found-s.address-not-"
                                 + "found-b.qas-missing-apt-s.qas-missing-apt-b.qas-recommendations-s.qas-recommendations-b"))));
     element.click();
   }
 }
  public static boolean verifyCurrentErrorMessages(List<String> errorMessagesExpected) {
    wait.until(
        ExpectedConditions.visibilityOf(
            SeleniumController.Instance.Driver().findElement(By.className("cartItem"))));
    List<WebElement> elements =
        SeleniumController.Instance.Driver()
            .findElement(By.id("addressFields"))
            .findElements(By.className("alert"));

    for (int i = 0; i < errorMessagesExpected.size(); i++) {
      int j;
      for (j = 0; j < elements.size(); j++) {
        if (elements.get(j).getText().contains(errorMessagesExpected.get(i))) break;
      }
      if (j == elements.size()) return false;
    }
    return true;
  }
  public static boolean verifyErrorMessageFunctionality(List<List<String>> fieldsToFill) {
    wait.until(
        ExpectedConditions.visibilityOf(
            SeleniumController.Instance.Driver().findElement(By.className("cartItem"))));
    for (int i = 0; i < fieldsToFill.size(); i++) {

      for (int j = 0; j < fieldsToFill.size(); j++) {
        if (j != i) {
          SeleniumController.Instance.Driver()
              .findElement(By.id("addressFields"))
              .findElement(By.className(fieldsToFill.get(j).get(0)))
              .sendKeys(fieldsToFill.get(j).get(2));
        }
      }
      continueToPaymentPage();
      int size =
          SeleniumController.Instance.Driver()
              .findElement(By.id("addressFields"))
              .findElements(By.className("alert"))
              .size();
      if (size != 1) {
        return false;
      }
      List<String> test = new ArrayList<String>();
      test.add(fieldsToFill.get(i).get(1));
      if (!verifyCurrentErrorMessages(test)) return false;
      for (int j = 0; j < fieldsToFill.size(); j++) {
        WebElement field =
            SeleniumController.Instance.Driver()
                .findElement(By.id("addressFields"))
                .findElement(By.className(fieldsToFill.get(j).get(0)));
        while (!field.getAttribute("value").equals("")) {
          field.sendKeys(Keys.BACK_SPACE);
        }
      }
    }

    return true;
  }
  public static boolean verifyCountrySelection(List<List<String>> countries) {
    for (int i = 0; i < countries.size(); i++) {
      SeleniumController.Instance.Driver()
          .findElement(By.className("country"))
          .sendKeys(countries.get(i).get(0) + Keys.ENTER);
      wait.until(
          ExpectedConditions.visibilityOf(
              SeleniumController.Instance.Driver()
                  .findElement(By.className("shipMethods"))
                  .findElement(By.id("shipMethods"))));

      List<WebElement> elements =
          SeleniumController.Instance.Driver().findElements(By.className("possibleState"));
      for (WebElement element : elements) {
        if (element.getAttribute("class").contains("selectedField")) {
          String elementExpression = element.getAttribute("data-invalid-message").toLowerCase();
          String expectedExpression = countries.get(i).get(1).toLowerCase();
          if (!elementExpression.contains(expectedExpression)) return false;
        }
      }
    }
    return true;
  }
public class ShippingInformationPage {

  private static WebDriverWait wait = new WebDriverWait(SeleniumController.Instance.Driver(), 30);

  public static void continueToPaymentPage() {
    wait.until(
        ExpectedConditions.visibilityOf(
            SeleniumController.Instance.Driver().findElement(By.id("shipMethods"))));
    WebElement element =
        SeleniumController.Instance.Driver()
            .findElement(
                By.cssSelector(
                    "html.no-js body.rfk_wbh div#pageContainer div#main d"
                        + "iv#checkout div.buttonWrapper.action button.button.primary.fullwidth"));
    element.click();
  }

  public static void selectUseEnteredAddressButton() {
    if (!SeleniumController.Instance.Driver().findElement(By.id("address-list")).isDisplayed()) {
      WebElement element =
          wait.until(
              ExpectedConditions.visibilityOf(
                  SeleniumController.Instance.Driver()
                      .findElement(
                          By.cssSelector(
                              "html.no-js body.rfk_wbh div#pageContainer div#main div#checkout div.buttonWrapper.action button#useAddress.foldContent.button.fullwidth.address-not-found-s.address-not-"
                                  + "found-b.qas-missing-apt-s.qas-missing-apt-b.qas-recommendations-s.qas-recommendations-b"))));
      element.click();
    }
  }

  public static boolean verifyCountrySelection(List<List<String>> countries) {
    for (int i = 0; i < countries.size(); i++) {
      SeleniumController.Instance.Driver()
          .findElement(By.className("country"))
          .sendKeys(countries.get(i).get(0) + Keys.ENTER);
      wait.until(
          ExpectedConditions.visibilityOf(
              SeleniumController.Instance.Driver()
                  .findElement(By.className("shipMethods"))
                  .findElement(By.id("shipMethods"))));

      List<WebElement> elements =
          SeleniumController.Instance.Driver().findElements(By.className("possibleState"));
      for (WebElement element : elements) {
        if (element.getAttribute("class").contains("selectedField")) {
          String elementExpression = element.getAttribute("data-invalid-message").toLowerCase();
          String expectedExpression = countries.get(i).get(1).toLowerCase();
          if (!elementExpression.contains(expectedExpression)) return false;
        }
      }
    }
    return true;
  }

  public static boolean verifyCurrentErrorMessages(List<String> errorMessagesExpected) {
    wait.until(
        ExpectedConditions.visibilityOf(
            SeleniumController.Instance.Driver().findElement(By.className("cartItem"))));
    List<WebElement> elements =
        SeleniumController.Instance.Driver()
            .findElement(By.id("addressFields"))
            .findElements(By.className("alert"));

    for (int i = 0; i < errorMessagesExpected.size(); i++) {
      int j;
      for (j = 0; j < elements.size(); j++) {
        if (elements.get(j).getText().contains(errorMessagesExpected.get(i))) break;
      }
      if (j == elements.size()) return false;
    }
    return true;
  }

  public static boolean verifyErrorMessageFunctionality(List<List<String>> fieldsToFill) {
    wait.until(
        ExpectedConditions.visibilityOf(
            SeleniumController.Instance.Driver().findElement(By.className("cartItem"))));
    for (int i = 0; i < fieldsToFill.size(); i++) {

      for (int j = 0; j < fieldsToFill.size(); j++) {
        if (j != i) {
          SeleniumController.Instance.Driver()
              .findElement(By.id("addressFields"))
              .findElement(By.className(fieldsToFill.get(j).get(0)))
              .sendKeys(fieldsToFill.get(j).get(2));
        }
      }
      continueToPaymentPage();
      int size =
          SeleniumController.Instance.Driver()
              .findElement(By.id("addressFields"))
              .findElements(By.className("alert"))
              .size();
      if (size != 1) {
        return false;
      }
      List<String> test = new ArrayList<String>();
      test.add(fieldsToFill.get(i).get(1));
      if (!verifyCurrentErrorMessages(test)) return false;
      for (int j = 0; j < fieldsToFill.size(); j++) {
        WebElement field =
            SeleniumController.Instance.Driver()
                .findElement(By.id("addressFields"))
                .findElement(By.className(fieldsToFill.get(j).get(0)));
        while (!field.getAttribute("value").equals("")) {
          field.sendKeys(Keys.BACK_SPACE);
        }
      }
    }

    return true;
  }

  public static void fillShippingInformation(List<List<String>> shippingInfo) {
    if (!SeleniumController.Instance.Driver().findElement(By.id("address-list")).isDisplayed()) {
      for (int i = 0; i < shippingInfo.size(); i++) {
        SeleniumController.Instance.Driver()
            .findElement(By.id("addressFields"))
            .findElement(By.className(shippingInfo.get(i).get(0)))
            .sendKeys(shippingInfo.get(i).get(1));
        if (shippingInfo.get(i).get(0).equals("country")) {
          SeleniumController.Instance.Driver()
              .findElement(By.id("addressFields"))
              .findElement(By.className(shippingInfo.get(i).get(0)))
              .sendKeys(Keys.ENTER);
          if (shippingInfo.get(i).get(1).contains("United States")) {
            wait.until(
                ExpectedConditions.textToBePresentInElement(
                    SeleniumController.Instance.Driver()
                        .findElement(By.className("shipMethods"))
                        .findElement(By.id("shipMethods")),
                    "U.S."));

          } else
            wait.until(
                ExpectedConditions.textToBePresentInElement(
                    SeleniumController.Instance.Driver()
                        .findElement(By.className("shipMethods"))
                        .findElement(By.id("shipMethods"))
                        .findElement(By.className("shipDescription")),
                    "International"));
        }
      }
    }
  }
}