package zacksolutions.pages;

import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import zacksolutions.base.Initialization;
import zacksolutions.base.JokesAPI;

import java.io.IOException;
import java.time.Duration;

public class HomePage extends Initialization {
  // Using PageFactory Object Model we get our elemets
  @FindBy(id = "message")
  WebElement message;

  @FindBy(css = "button[type='submit']")
  WebElement button;

  @FindBy(xpath = "//tbody/tr[1]/td[1]")
  WebElement ConfirmText;

  // Initialization
  public HomePage() {
    PageFactory.initElements(driver, this);
  }

  // Actions
  public void insertText() throws InterruptedException, IOException {
    JokesAPI jokesAPI = new JokesAPI();
    jokesAPI.fetchJokes();
    String setup = jokesAPI.getSetup();
    String punchline = jokesAPI.getPunchline();
    test = extent.createTest("Test msg");
    message.sendKeys(setup + " ==> " + punchline);
    button.click();
    extent.flush();
  }

  public void textConfirmation() {
    WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(15)); // Adjust the timeout as needed
    wait.until(ExpectedConditions.visibilityOf(ConfirmText));
    String joke = ConfirmText.getText();
    System.out.println(joke);
  }
}
