package io.syndesis.qe.steps.other;

import com.google.api.services.gmail.model.Message;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
import io.syndesis.qe.utils.GMailUtils;
import io.syndesis.qe.utils.GoogleAccount;
import io.syndesis.qe.utils.TestUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import javax.annotation.PostConstruct;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.IOException;
import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.fail;

@Slf4j
public class GMailSteps {

    @Autowired
    @Qualifier("QE Google Mail")
    private GoogleAccount googleAccount;
    private GMailUtils gmu;

    @PostConstruct
    public void setup() {
        gmu = new GMailUtils(googleAccount);
    }

    @When("^.*send an e-mail$")
    public void sendMail() {
        sendEmail("jbossqa.fuse@gmail.com");
    }

    @When("^.*send an e-mail to \"([^\"]*)\"$")
    public void sendEmail(String sendTo) {
        sendEmail(sendTo, "syndesis-tests");
    }

    @When("^.*send an e-mail to \"([^\"]*)\" with subject \"([^\"]*)\"$")
    public void sendEmail(String sendTo, String subject) {
        gmu.sendEmail("me", sendTo, subject, "Red Hat");
    }

    @Given("^delete emails from \"([^\"]*)\" with subject \"([^\"]*)\"$")
    public void deleteMails(String from, String subject) {
        gmu.deleteMessages(from, subject);
    }

    @Then("^check that email from \"([^\"]*)\" with subject \"([^\"]*)\" and text \"([^\"]*)\" exists$")
    public void checkMails(String from, String subject, String text) {
        TestUtils.waitFor(() -> checkMailExists(from, subject, text),
            1, 10,
            "Could not find specified mail");
    }

    private boolean checkMailExists(String from, String subject, String text) {
        try {
            List<Message> messages = gmu.getMessagesMatchingQuery("me", "subject:" + subject + " AND from:" + from);
            // if messages list is empty, we haven't found anything, that's false
            if (messages.size() == 0) {
                return false;
            }

            MimeMessage mime = gmu.getMimeMessage(messages.get(0).getId());
            // now, if subject is the same and content is the same (some connectors might send trailing whitespaces)
            // then we have found our message
            if (mime.getSubject().equalsIgnoreCase(subject)
                && mime.getContent().toString().trim().equalsIgnoreCase(text.trim())) {
                return true;
            }
        } catch (IOException | MessagingException ignored) {
        }

        return false;
    }
}

@@ -34,163 +34,8 @@
 r;%0A%0A
-import com.google.api.services.gmail.model.Message;%0Aimport cucumber.api.java.en.Given;%0Aimport cucumber.api.java.en.Then;%0Aimport cucumber.api.java.en.When;%0A
 impo
@@ -66,24 +66,24 @@
 GMailUtils;%0A
+
 import io.sy
@@ -156,42 +156,9 @@
 ls;%0A
-import lombok.extern.slf4j.Slf4j;
 %0A
+
 impo
@@ -276,24 +276,77 @@
 Qualifier;%0A%0A
+import com.google.api.services.gmail.model.Message;%0A%0A
 import javax
@@ -450,16 +450,17 @@
 essage;%0A
+%0A
 import j
@@ -514,109 +514,136 @@
 ort 
-static org.assertj.core.api.Assertions.assertThat;%0Aimport static org.assertj.core.api.Assertions.fail
+cucumber.api.java.en.Given;%0Aimport cucumber.api.java.en.Then;%0Aimport cucumber.api.java.en.When;%0Aimport lombok.extern.slf4j.Slf4j
 ;%0A%0A@
@@ -2706,18 +2706,98 @@
 ion 
-ignored) %7B
+e) %7B%0A            log.debug(%22There has been an error while checking for existing mail%22, e);
 %0A   

Fix checkstyle for GmailSteps