package Login;

import Support.BasicPage;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;
import org.testng.Assert;
import org.testng.asserts.SoftAssert;

public class LoginPage extends BasicPage {
    private WebDriver driver;
    private SoftAssert sa;

    @FindBy(how=How.CLASS_NAME, using = "paper")
    WebElement loginForm;
    @FindBy (how=How.ID, using = "login-username")
    WebElement usernameTextBox;
    @FindBy (how=How.ID, using = "login-password")
    WebElement passwordTextBox;
    @FindBy (how=How.XPATH, using = "/html/body/div[1]/section[1]/div/form[1]/button")
    WebElement submitButton;
    @FindBy (how=How.XPATH, using = ".//*[@id='login']/form[1]/label[2]")
    WebElement errorMessageArea;

    /**
     * constructor
     * @param driver WebDriver which we are using
     */
    public LoginPage(WebDriver driver, SoftAssert sa)
    {
        super(driver);
        this.sa = sa;
    }

    /**
     * check if Login page is displayed
     * @param shouldBeDisplayed if should be displayed
     */
    public void isLoginPageDisplayed(Boolean shouldBeDisplayed)
    {
        boolean result = true;
        result&=isElementPresent(loginForm);

        if (shouldBeDisplayed)
        {
            Assert.assertTrue(result, "Login page is not displayed!");
        }
        else
        {
            Assert.assertFalse(result, "Login page is displayed but shouldn't be!");
        }
    }

    /**
     * check error message in login form
     * @param errorMessage that should be displayed
     */
    public void checkErrorMessage(String errorMessage)
    {
        String message = loginForm.getText();
        sa.assertTrue(message.contains(errorMessage), "Error message in Login page is not displayed correctly");
    }

    /**
     * set username
     * @param username which we want to set
     */
    public void setUsername(String username)
    {
        Assert.assertTrue(isElementPresent(usernameTextBox),"Username textBox in Login page is not displayed!");
        usernameTextBox.clear();
        usernameTextBox.sendKeys(username);
    }

    /**
     * set password
     * @param password which we want to set
     */
    public void setPassword(String password)
    {
        Assert.assertTrue(isElementPresent(passwordTextBox),"Password textBox in Login page is not displayed!");
        passwordTextBox.clear();
        passwordTextBox.sendKeys(password);
    }

    /**
     * click on submit button
     */
    public void clickOnSubmitButton()
    {
        Assert.assertTrue(isElementPresent(submitButton),"Submit button in Login page is not displayed!");
        submitButton.click();
    }



}
