import java.util.List;
import java.util.Map;
import org.openqa.selenium.support.CacheLookup;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

public class Name {
    private Map<String, String> data;
    private WebDriver driver;
    private int timeout = 15;

    @FindBy(css = "a[href='http://www.goole.com/about/']")
    private WebElement about;

    @FindBy(css = "a[href='http://www.goole.com/acceptable-use/']")
    private WebElement acceptableUse;

    @FindBy(css = "a[href='http://www.goole.com/blogs-facebook/']")
    private WebElement blogsFacebookTwitter;

    @FindBy(css = "a[href='http://www.goole.com/business-directory/']")
    private WebElement businessDirectory;

    @FindBy(css = "a[href='http://www.goole.com/charities-support-groups/']")
    private WebElement charitiesSupportGroups;

    @FindBy(css = "a[href='http://www.goole.com/schools-and-education/']")
    private WebElement education;

    @FindBy(css = "a[href='http://www.goole.com/gallery/']")
    private WebElement gallery;

    @FindBy(css = "a[title='Goole']")
    private WebElement goole;

    @FindBy(css = "a[href='http://www.goole.com/history/']")
    private WebElement gooleHistory;

    @FindBy(css = "a[href='https://www.mojeek.com/']")
    private WebElement mojeek;

    @FindBy(css = "a[href='http://www.goole.com/news/']")
    private WebElement news;

    private final String pageLoadedText = "";

    private final String pageUrl = "/home/nhatnh/Code/n8n-protractor-page-object-generator/index.html";

    @FindBy(css = "a[href='http://www.goole.com/privacy/']")
    private WebElement privacyPolicy;

    @FindBy(css = "#ask form table tbody tr:nth-of-type(2) td input:nth-of-type(2)")
    private WebElement search;

    @FindBy(css = "a[href='http://www.goole.com/sport/']")
    private WebElement sport;

    @FindBy(css = "a[href='http://www.goole.com/terms-conditions/']")
    private WebElement termsConditions;

    public Name() {
    }

    public Name(WebDriver driver) {
        this();
        this.driver = driver;
    }

    public Name(WebDriver driver, Map<String, String> data) {
        this(driver);
        this.data = data;
    }

    public Name(WebDriver driver, Map<String, String> data, int timeout) {
        this(driver, data);
        this.timeout = timeout;
    }

    /**
     * Click on About Link.
     *
     * @return the Name class instance.
     */
    public Name clickAboutLink() {
        about.click();
        return this;
    }

    /**
     * Click on Acceptable Use Link.
     *
     * @return the Name class instance.
     */
    public Name clickAcceptableUseLink() {
        acceptableUse.click();
        return this;
    }

    /**
     * Click on Blogs Facebook Twitter Link.
     *
     * @return the Name class instance.
     */
    public Name clickBlogsFacebookTwitterLink() {
        blogsFacebookTwitter.click();
        return this;
    }

    /**
     * Click on Business Directory Link.
     *
     * @return the Name class instance.
     */
    public Name clickBusinessDirectoryLink() {
        businessDirectory.click();
        return this;
    }

    /**
     * Click on Charities Support Groups Link.
     *
     * @return the Name class instance.
     */
    public Name clickCharitiesSupportGroupsLink() {
        charitiesSupportGroups.click();
        return this;
    }

    /**
     * Click on Education Link.
     *
     * @return the Name class instance.
     */
    public Name clickEducationLink() {
        education.click();
        return this;
    }

    /**
     * Click on Gallery Link.
     *
     * @return the Name class instance.
     */
    public Name clickGalleryLink() {
        gallery.click();
        return this;
    }

    /**
     * Click on Goole History Link.
     *
     * @return the Name class instance.
     */
    public Name clickGooleHistoryLink() {
        gooleHistory.click();
        return this;
    }

    /**
     * Click on Goole Link.
     *
     * @return the Name class instance.
     */
    public Name clickGooleLink() {
        goole.click();
        return this;
    }

    /**
     * Click on Mojeek Link.
     *
     * @return the Name class instance.
     */
    public Name clickMojeekLink() {
        mojeek.click();
        return this;
    }

    /**
     * Click on News Link.
     *
     * @return the Name class instance.
     */
    public Name clickNewsLink() {
        news.click();
        return this;
    }

    /**
     * Click on Privacy Policy Link.
     *
     * @return the Name class instance.
     */
    public Name clickPrivacyPolicyLink() {
        privacyPolicy.click();
        return this;
    }

    /**
     * Click on Search Button.
     *
     * @return the Name class instance.
     */
    public Name clickSearchButton() {
        search.click();
        return this;
    }

    /**
     * Click on Sport Link.
     *
     * @return the Name class instance.
     */
    public Name clickSportLink() {
        sport.click();
        return this;
    }

    /**
     * Click on Terms Conditions Link.
     *
     * @return the Name class instance.
     */
    public Name clickTermsConditionsLink() {
        termsConditions.click();
        return this;
    }

    /**
     * Submit the form to target page.
     *
     * @return the Name class instance.
     */
    public Name submit() {
        clickSearchButton();
        return this;
    }

    /**
     * Verify that the page loaded completely.
     *
     * @return the Name class instance.
     */
    public Name verifyPageLoaded() {
        (new WebDriverWait(driver, timeout)).until(new ExpectedCondition<Boolean>() {
            public Boolean apply(WebDriver d) {
                return d.getPageSource().contains(pageLoadedText);
            }
        });
        return this;
    }

    /**
     * Verify that current page URL matches the expected URL.
     *
     * @return the Name class instance.
     */
    public Name verifyPageUrl() {
        (new WebDriverWait(driver, timeout)).until(new ExpectedCondition<Boolean>() {
            public Boolean apply(WebDriver d) {
                return d.getCurrentUrl().contains(pageUrl);
            }
        });
        return this;
    }
}
