package com.autonomy.abc.selenium.users;

import com.autonomy.abc.selenium.page.login.GoogleAuth;
import com.autonomy.abc.selenium.util.Waits;
import org.openqa.selenium.*;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

public class GmailSignupEmailHandler implements SignupEmailHandler {
    private final static String GMAIL_URL = "https://accounts.google.com/ServiceLogin?service=mail&continue=https://mail.google.com/mail/#identifier";
    private final GoogleAuth googleAuth;
    private WebDriver driver;

    public GmailSignupEmailHandler(GoogleAuth auth) {
        this.googleAuth = auth;
    }

    public void markAllEmailAsRead(WebDriver driver){
        setDriver(driver);
        goToGoogleAndLogIn();

        driver.findElement(By.cssSelector(".T-I.J-J5-Ji.ar7.nf.T-I-ax7.L3")).click();
        Waits.loadOrFadeWait();
        driver.findElement(By.xpath("//div[text()='Mark all as read']")).click();
        Waits.loadOrFadeWait();
    }

    private void goToGoogleAndLogIn() {
        driver.get(GMAIL_URL);
        new GoogleAuth.GoogleLoginPage(driver).login(googleAuth);
    }

    private void setDriver(WebDriver driver) {
        this.driver = driver;
    }

    @Override
    public boolean goToUrl(WebDriver driver) {
        setDriver(driver);
        goToGoogleAndLogIn();
        openMessage();
        try {
            clickLink();
        } catch (NoSuchElementException e) {
            /* Probably had an unread email */

            driver.findElement(By.cssSelector(".T-I.J-J5-Ji.lS.T-I-ax7.ar7")).click();

            try {
                openMessage();
            } catch (TimeoutException f) {
                //Email was probably opened the first time; but for some reason clicking on the message didn't take you to the 'right' place
                LoggerFactory.getLogger(GmailSignupEmailHandler.class).info("Email failed to open; *probably* signed up already for some reason");
                return false;
            }

            clickLink();
        }

        return true;
    }

    private void openMessage(){
        waitForNewEmail();
        clickUnreadMessage();
        expandCollapsedMessage();
    }

    private void waitForNewEmail() {
        new WebDriverWait(driver,60).until(new ExpectedCondition<Boolean>() {
            @Override
            public Boolean apply(WebDriver driver) {
                List<WebElement> unreadEmails = driver.findElements(By.cssSelector(".zA.zE"));

                if (unreadEmails.size() > 0) {
                    return true;
                }

                driver.findElement(By.cssSelector(".T-I.J-J5-Ji.nu.T-I-ax7.L3")).click();

                return false;
            }
        });
    }

    private void clickUnreadMessage(){
        driver.findElement(By.cssSelector(".zA.zE")).click();
    }

    private void expandCollapsedMessage() {
        try {
            new WebDriverWait(driver,10).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("img.ajT")));

            List<WebElement> ellipses = driver.findElements(By.cssSelector("img.ajT"));
            WebElement finalEllipses = ellipses.get(ellipses.size() - 1);

            if(finalEllipses.isDisplayed()){
                finalEllipses.click();
            }
        } catch (Exception e) { /* No Ellipses */ }
    }

    private void clickLink() {
        driver.findElement(By.partialLinkText("here")).click();

        Waits.loadOrFadeWait();

        List<String> handles = new ArrayList<>(driver.getWindowHandles());
        driver.switchTo().window(handles.get(1));

        try {
            new WebDriverWait(driver,20).until(ExpectedConditions.visibilityOfElementLocated(By.className("twitter")));
        } catch (TimeoutException e) {
            //If not already verified then go back to inbox
            if (!driver.getCurrentUrl().contains("already")) {
                //Want to ignore cases where users are already verified, or taken to verification       TODO figure out which cases need this to be run
                driver.close();
                driver.switchTo().window(handles.get(0));
                //Probably the wrong exception to throw but just to make things easier - happens when a link has already been used for auth
                throw new NoSuchElementException("Incorrect link clicked");
            }
        }

        driver.switchTo().window(handles.get(0));
        driver.close();
        driver.switchTo().window(handles.get(1));
    }
}

@@ -928,82 +928,25 @@
     
-driver.findElement(By.cssSelector(%22.T-I.J-J5-Ji.ar7.nf.T-I-ax7.L3%22)).click
+clickMoreDropdown
 ();%0A
@@ -1615,79 +1615,24 @@
     
-driver.findElement(By.cssSelector(%22.T-I.J-J5-Ji.lS.T-I-ax7.ar7%22)).click
+clickBackToInbox
 ();%0A
@@ -2636,32 +2636,166 @@
      %7D%0A%0A        
+        clickRefreshButton();%0A%0A                return false;%0A            %7D%0A        %7D);%0A    %7D%0A%0A    private void clickRefreshButton() %7B%0A
         driver.f
@@ -2864,62 +2864,240 @@
 ();%0A
-%0A
     
+%7D%0A%0A
     
-        return false
+private void clickUnreadMessage()%7B%0A        driver.findElement(By.cssSelector(%22.zA.zE%22)).click()
 ;%0A    
-  
+%7D%0A%0A
     
-  %7D%0A        %7D
+private void clickBackToInbox()%7B%0A        driver.findElement(By.cssSelector(%22.T-I.J-J5-Ji.lS.T-I-ax7.ar7%22)).click(
 );%0A 
@@ -3120,37 +3120,36 @@
 e void click
-UnreadMessage
+MoreDropdown
 ()%7B%0A        
@@ -3176,37 +3176,61 @@
 y.cssSelector(%22.
-zA.zE
+T-I.J-J5-Ji.ar7.nf.T-I-ax7.L3
 %22)).click();%0A   

Move code into readable methods