package Email;

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 EmailPage extends BasicPage {
    private WebDriver driver;
    private SoftAssert sa = new SoftAssert();

    @FindBy(how=How.CLASS_NAME, using = "recipient-list")
    WebElement recipientTextBox;
    @FindBy(how=How.XPATH, using = "/html/body/section/div[2]/dl/div[5]/dd")
    WebElement subjectTextBox;
    @FindBy(how=How.CLASS_NAME, using = "editor")
    WebElement messageTextBox;
    @FindBy(how=How.XPATH, using = "/html/body/section/div[3]/button[3]")
    WebElement discardButton;
    @FindBy(how=How.XPATH, using = "/html/body/section/div[3]/button[2]")
    WebElement saveAndCloseButton;
    @FindBy(how=How.XPATH, using = "/html/body/section/div[3]/button[1]")
    WebElement sendButton;
    @FindBy(how=How.XPATH, using = "/html/body/div[4]")
    WebElement emailAddressPopup;






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

    public void isEmailPageDisplayed(Boolean shouldBeDisplayed)
    {
        boolean result = true;
        result&=isElementPresent(messageTextBox);
        result&=isElementPresent(sendButton);

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

    public void fillRecipientAddressTextbox(String recipientAddress)
    {
        Assert.assertTrue(isElementPresent(recipientTextBox), "Recipient address textbox in Email is not displayed!");
        recipientTextBox.click();
        recipientTextBox.sendKeys(recipientAddress);
        if (isElementPresent(emailAddressPopup))
        {
            emailAddressPopup.click();
        }
    }

    public void fillSubjectTextbox(String subject)
    {
        Assert.assertTrue(isElementPresent(subjectTextBox), "Subject address textbox in Email is not displayed!");
        subjectTextBox.click();
        subjectTextBox.sendKeys(subject);
    }

    public void fillMessageTextbox(String message)
    {
        Assert.assertTrue(isElementPresent(messageTextBox), "Message textbox in Email is not displayed!");
        messageTextBox.click();
        messageTextBox.sendKeys(message);
    }

    public void clickToDiscardButton()
    {
        Assert.assertTrue(isElementPresent(discardButton), "Discard button in Email is not displayed!");
        discardButton.click();
    }

    public void clickToSaveAndCloseButton()
    {
        Assert.assertTrue(isElementPresent(saveAndCloseButton), "SaveAndClose button in Email is not displayed!");
        saveAndCloseButton.click();
    }

    public void clickToSendButton()
    {
        Assert.assertTrue(isElementPresent(sendButton), "Send button in Email is not displayed!");
        sendButton.click();
    }



}
