package Overview;

import Support.BasicPage;
import org.openqa.selenium.By;
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;

import java.util.List;

public class OverviewPage extends BasicPage {
    private WebDriver driver;
    private SoftAssert sa = new SoftAssert();

    @FindBy(how=How.ID, using = "user")
    WebElement userCombobox;
    @FindBy(how=How.ID, using = "logo")
    WebElement logo;
    @FindBy(how = How.XPATH, using=".//*[@id='content']/nav/div/div/ul[1]/li[1]")
    WebElement inboxButton;
    @FindBy(how = How.XPATH, using=".//*[@id='content']/nav/div/div/ul[1]/li[2]")
    WebElement sentMessagesButton;
    @FindBy(how = How.XPATH, using=".//*[@id='content']/nav/div/div/ul[1]/li[3]")
    WebElement itemizedMessagesButton;
    @FindBy(how = How.XPATH, using=".//*[@id='content']/nav/div/div/ul[1]/li[4]")
    WebElement bulkMessagesButton;
    @FindBy(how = How.XPATH, using=".//*[@id='content']/nav/div/div/ul[1]/li[5]")
    WebElement archiveButton;
    @FindBy(how = How.XPATH, using=".//*[@id='content']/nav/div/div/ul[1]/li[6]")
    WebElement spamButton;
    @FindBy(how = How.XPATH, using=".//*[@id='content']/nav/div/div/ul[1]/li[7]")
    WebElement trashButton;
    @FindBy(how = How.XPATH, using=".//*[@id='content']/nav/div/div/ul[1]/li[8]")
    WebElement favoriteMessagesButton;
    @FindBy(how = How.XPATH, using="/html/body/div[2]/nav/div/ul[1]/li")
    WebElement contactsButton;
    @FindBy(how = How.XPATH, using="/html/body/div[2]/nav/div/a[2]")
    WebElement newEmailButton;
    @FindBy(how = How.CLASS_NAME, using="wide")
    List<WebElement> emails;
    @FindBy(how = How.CLASS_NAME, using="select")
    WebElement emailCheckbox;
    @FindBy(how = How.XPATH, using="/html/body/div[2]/header/div[2]/span[2]/button[5]")
    WebElement deleteButton;
    @FindBy(how = How.XPATH, using="/html/body/div[2]/header/div[2]/span[2]/button[3]")
    WebElement moveToSpamButton;
    @FindBy(how = How.XPATH, using="/html/body/div[2]/header/div[2]/span[2]/button[2]")
    WebElement markButton;
    @FindBy(how = How.XPATH, using="/html/body/div[2]/header/div[2]/span[2]/button[1]")
    WebElement moveButton;
    @FindBy(how = How.XPATH, using="/html/body/div[2]/header/div[2]/span[1]")
    WebElement checkAllButton;
    @FindBy(how = How.XPATH, using="/html/body/div[2]/header/div[2]/span[1]/label/span")
    WebElement checkAllCheckbox;




    private static final String EMAIL_CHECKBOX_CLASS_NAME="select";






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

    /**
     * check if Overview page is displayed
     * @param shouldBeDisplayed if should be displayed
     */
    public void isOverviewPageDisplayed(Boolean shouldBeDisplayed)
    {
        boolean result = true;
        result&=isElementPresent(userCombobox);
        result&=isElementPresent(logo);

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

    }

    public void clickToNewEmailButton()
    {
        Assert.assertTrue(isElementPresent(newEmailButton), "New email button in Overview page is not displayed!");
        newEmailButton.click();
    }

    public void clickToInboxButton()
    {
        Assert.assertTrue(isElementPresent(inboxButton), "Inbox button in Overview page is not displayed!");
        inboxButton.click();
    }

    public void clickToSentMessagesButton()
    {
        Assert.assertTrue(isElementPresent(sentMessagesButton), "Sent messages button in Overview page is not displayed!");
        sentMessagesButton.click();
    }

    public void clickToItemizedMessagesButton()
    {
        Assert.assertTrue(isElementPresent(itemizedMessagesButton), "Itemized messages button in Overview page is not displayed!");
        itemizedMessagesButton.click();
    }

    public void clickToBulkMessagesButton()
    {
        Assert.assertTrue(isElementPresent(bulkMessagesButton), "Bulk messages button in Overview page is not displayed!");
        bulkMessagesButton.click();
    }

    public void clickToArchiveButton()
    {
        Assert.assertTrue(isElementPresent(archiveButton), "Archive button in Overview page is not displayed!");
        archiveButton.click();
    }

    public void clickToSpamButton()
    {
        Assert.assertTrue(isElementPresent(moveToSpamButton), "Spam button in Overview page is not displayed!");
        moveToSpamButton.click();
    }

    public void clickToTrashButton()
    {
        Assert.assertTrue(isElementPresent(trashButton), "Trash button in Overview page is not displayed!");
        trashButton.click();
    }

    public void clickToFavoritesButton()
    {
        Assert.assertTrue(isElementPresent(favoriteMessagesButton), "Favorite messages button in Overview page is not displayed!");
        favoriteMessagesButton.click();
    }

    public void clickToContactsButton()
    {
        Assert.assertTrue(isElementPresent(contactsButton), "Contacts button in Overview page is not displayed!");
        contactsButton.click();
    }

    /**
     * check if email is present in any folder (inbox, sent messages, etc.) based on email address and subject.
     * DON'T PUT THIS METHOD TO EXCEL THIS IS JUST COMMON METHOD
     * @param addressAndSubject must be in format "emailAddress, subject"
     * @param shouldBeThere if email should be present
     * @return if email is present
     */
    public boolean isEmailPresent(String addressAndSubject, boolean shouldBeThere)
    {
        WebElement email = getEmail(addressAndSubject);

        return ((email!=null)&&shouldBeThere);
    }

    public WebElement getEmail(String addressAndSubject)
    {
        String emailDetails;
        WebElement finalEmail=null;

        addressAndSubject = addressAndSubject.replaceAll("\\s+",""); //delete all whitespaces
        String emailAddress = addressAndSubject.substring(0, addressAndSubject.indexOf(',')); //separete email address
        String subject = addressAndSubject.substring(addressAndSubject.indexOf(',')); //separate subject
        subject = subject.replaceAll(",",""); //remove "," from beginning of the subject

        waitForSeconds(1);
        for (WebElement email: emails) {
            emailDetails = email.getText();
            emailDetails= emailDetails.replaceAll("\\s+","");
            if (emailDetails.contains(emailAddress)&&emailDetails.contains(subject))
            {
                finalEmail = email;
            }
        }

        return finalEmail;
    }


    public void markEmail(String addressAndSubject)
    {
        WebElement email = getEmail(addressAndSubject);
            if (email!=null)
            {
                email.findElement(By.className(EMAIL_CHECKBOX_CLASS_NAME)).click();
            }
    }

    /**
     * check if email is present in any folder (inbox, sent messages, etc.) based on email address and subject
     * @param addressAndSubject must be in format "emailAddress, subject"
     */
    public void checkEmailIsPresent(String addressAndSubject)
    {
        Assert.assertTrue(isEmailPresent(addressAndSubject, true), "Email from: "+addressAndSubject+" is not displayed!");
    }

    /**
     * check if email is not present in any folder (inbox, sent messages, etc.) based on email address and subject
     * @param addressAndSubject must be in format "emailAddress, subject"
     */
    public void checkEmailIsNotPresent(String addressAndSubject)
    {
        Assert.assertTrue(isEmailPresent(addressAndSubject, false), "Email from: "+addressAndSubject+" is displayed but shouldn't be!");
    }

    public void clickDeleteButton()
    {
        Assert.assertTrue(isElementPresent(deleteButton), "Delete button in Overview page is not displayed!");
        deleteButton.click();
    }

    public void clickMoveToSpamButton()
    {
        Assert.assertTrue(isElementPresent(moveToSpamButton), "Spam button in Overview page is not displayed!");
        moveToSpamButton.click();
    }

    public void clickMarkButton()
    {
        Assert.assertTrue(isElementPresent(markButton), "Mark button in Overview page is not displayed!");
        markButton.click();
    }

    public void clickMoveButton()
    {
        Assert.assertTrue(isElementPresent(moveButton), "Move button in Overview page is not displayed!");
        moveButton.click();
    }

    public void checkAllEmails()
    {
        Assert.assertTrue(isElementPresent(checkAllCheckbox), "Check all checkbox in Overview page is not displayed!");
        checkAllCheckbox.click();
    }

}
