package com.autonomy.abc.selenium.users;

import com.autonomy.abc.selenium.element.GritterNotice;
import com.autonomy.abc.selenium.page.admin.HSOUsersPage;
import com.autonomy.abc.selenium.page.admin.UsersPage;
import com.autonomy.abc.selenium.util.Factory;
import com.hp.autonomy.frontend.selenium.login.AuthProvider;
import org.openqa.selenium.By;
import org.openqa.selenium.TimeoutException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.util.ArrayList;

// TODO: CSA-1663
public class HSONewUser implements NewUser {

    private final String username;
    private final String email;
    private AuthProvider provider;

    public HSONewUser(String username, String email) {
        this.username = username;
        this.email = email;
    }

    public HSONewUser(String username, String email, AuthProvider provider){
        this(username, email);
        this.provider = provider;
    }

    @Override
    public HSOUser signUpAs(Role role, UsersPage usersPage, Factory<WebDriver> webDriverFactory) {
        HSOUsersPage hsoUsersPage = (HSOUsersPage) usersPage;

        hsoUsersPage.addUsername(username);
        hsoUsersPage.addEmail(email);
        hsoUsersPage.selectRole(role);
        hsoUsersPage.createButton().click();

        new WebDriverWait(usersPage.getDriver(),15).withMessage("User hasn't been created").until(GritterNotice.notificationContaining("Created user"));

        hsoUsersPage.loadOrFadeWait();

        if (hsoUsersPage.getUsernameInput().getValue().equals("")) {
            return new HSOUser(username, email, role, provider);
        }

        throw new UserNotCreatedException(this);
    }

    private void successfullyAdded(UsersPage usersPage) {
        getGmail();

        try {
            new WebDriverWait(driver, 15).until(ExpectedConditions.visibilityOfElementLocated(By.linkText("Google")));
            verifyUser();
        } catch (TimeoutException e) { /* User already verified */ }
    }

    private WebDriver driver;

    private void getGmail(){
        GMailHelper helper = new GMailHelper(driver);

        helper.goToGMail();
        helper.tryLoggingInToEmail();
        helper.waitForNewEmail();
        helper.clickUnreadMessage();
        helper.expandCollapsedMessage();

        //Click on link to verify
        driver.findElement(By.xpath("//a[text()='here']")).click();

        try {
            Thread.sleep(1000);
        } catch (Exception e) {/**/}

        driver.switchTo().window(new ArrayList<>(driver.getWindowHandles()).get(1));
    }

    private void verifyUser(){
        driver.findElement(By.linkText("Google")).click();

        new WebDriverWait(driver, 30).until(ExpectedConditions.visibilityOfElementLocated(By.className("noAccount")));

        driver.findElement(By.linkText("Google")).click();

        new WebDriverWait(driver, 30).until(ExpectedConditions.visibilityOfElementLocated(By.className("find-logo-large")));
    }

    @Override
    public User replaceAuthFor(User user, UsersPage usersPage) {
        return null;
    }

    public class UserNotCreatedException extends RuntimeException {
        public UserNotCreatedException(HSONewUser user){
            this(user.username);
        }

        public UserNotCreatedException(String username){
            super("User '" + username + "' was not created");
        }
    }

    public String getUsername() {
        return username;
    }

    public String getEmail() {
        return email;
    }

    public AuthProvider getProvider() {
        return provider;
    }
}

@@ -343,151 +343,17 @@
 ium.
-By;%0Aimport org.openqa.selenium.TimeoutException;%0Aimport org.openqa.selenium.WebDriver;%0Aimport org.openqa.selenium.support.ui.ExpectedConditions
+WebDriver
 ;%0Aim
@@ -408,37 +408,8 @@
 t;%0A%0A
-import java.util.ArrayList;%0A%0A
 // T
@@ -1518,32 +1518,32 @@
 er);%0A        %7D%0A%0A
+
         throw ne
@@ -1586,1298 +1586,8 @@
  %7D%0A%0A
-    private void successfullyAdded(UsersPage usersPage) %7B%0A        getGmail();%0A%0A        try %7B%0A            new WebDriverWait(driver, 15).until(ExpectedConditions.visibilityOfElementLocated(By.linkText(%22Google%22)));%0A            verifyUser();%0A        %7D catch (TimeoutException e) %7B /* User already verified */ %7D%0A    %7D%0A%0A    private WebDriver driver;%0A%0A    private void getGmail()%7B%0A        GMailHelper helper = new GMailHelper(driver);%0A%0A        helper.goToGMail();%0A        helper.tryLoggingInToEmail();%0A        helper.waitForNewEmail();%0A        helper.clickUnreadMessage();%0A        helper.expandCollapsedMessage();%0A%0A        //Click on link to verify%0A        driver.findElement(By.xpath(%22//a%5Btext()='here'%5D%22)).click();%0A%0A        try %7B%0A            Thread.sleep(1000);%0A        %7D catch (Exception e) %7B/**/%7D%0A%0A        driver.switchTo().window(new ArrayList%3C%3E(driver.getWindowHandles()).get(1));%0A    %7D%0A%0A    private void verifyUser()%7B%0A        driver.findElement(By.linkText(%22Google%22)).click();%0A%0A        new WebDriverWait(driver, 30).until(ExpectedConditions.visibilityOfElementLocated(By.className(%22noAccount%22)));%0A%0A        driver.findElement(By.linkText(%22Google%22)).click();%0A%0A        new WebDriverWait(driver, 30).until(ExpectedConditions.visibilityOfElementLocated(By.className(%22find-logo-large%22)));%0A    %7D%0A%0A
     

Clean up HSONewUser