package org.wyona.webapp.controllers;

import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import org.wyona.webapp.interfaces.EmailValidation;
import org.wyona.webapp.models.Email;
import org.wyona.webapp.models.Greeting;
import org.wyona.webapp.services.MailerService;

import javax.mail.MessagingException;

/**
 * 'Hello World' Controller 
 */
@RestController
@RequestMapping(value = "/api/greeting")
public class HelloWorldController {

    private static final Logger logger = LogManager.getLogger("HelloWorldController");

    private MailerService mailerService;

    private EmailValidation emailValidation;

    @Autowired
    public HelloWorldController(MailerService mailerService, EmailValidation emailValidation){
        this.mailerService = mailerService;
        this.emailValidation = emailValidation;
    }

    /**
     * Send greetings by email
     */
    @RequestMapping(method = RequestMethod.GET, produces = "application/json")
    @ApiOperation(value="Generate greeting and send greeting as email when address specified")
    @ApiResponses(value = {
            @ApiResponse(code = 400, message = "Bad Request, e.g. provided email parameter is not valid email address")
    })
    public ResponseEntity<Greeting> getGreeting(
        @ApiParam(name = "email", value = "email address greeting will be sent to, e.g. 'michael.wechner@wyona.com'", required = false) @RequestParam(name = "email", required = false) String email
        ) throws MessagingException {

        Greeting greeting = new Greeting("World");
        logger.info(greeting.getGreeting());

        if(email != null && !email.isEmpty()) {
            mailerService.sendEmailGreeting(email, greeting.getGreeting(), greeting.getGreeting());
        }

        return new ResponseEntity<>(greeting, HttpStatus.OK);
    }

    /**
     * Send greetings by email, whereas subject and body text can be set
     */
    @PostMapping("/send")
    @ApiOperation(value = "Send an e-mail with provided text, subject to email address which is specified")
    @ApiResponses(value = {
            @ApiResponse(code = 400, message = "Bad Request, e.g. provided email parameter is not valid email address")
    })
    public ResponseEntity<Email> sendEmail(@ApiParam(name = "e-mail", value = "e-mail address to be sent to", required = true)
                                              @RequestParam(name = "e-mail") String email,
                                           @ApiParam(name = "subject", value = "subject to be sent to")
                                              @RequestParam(name = "subject") String subject,
                                           @ApiParam(name = "text", value = "text to be sent to")
                                              @RequestParam(name = "text") String text) throws MessagingException {

        Boolean emailValid = emailValidation.isEmailValid(email);
        if (!emailValid) {
            throw new IllegalArgumentException("Provided email is not valid");
        }
        String automatedSubject = emailValidation.isSubjectEmpty(subject);
        String automatedText = emailValidation.isTextEmpty(text);

        Email object = new Email(email, automatedSubject, automatedText);
        mailerService.sendEmailToUser(object);

        return new ResponseEntity<>(object, HttpStatus.OK);
    }
}

@@ -1848,24 +1848,294 @@
 tring email%0A
+        //@ApiParam(name = %22email%22, value = %22email address greeting will be sent to, e.g. 'michael.wechner@wyona.com'%22, required = false) @javax.validation.constraints.Email(message = %22Email should be valid%22) @RequestParam(name = %22email%22, required = false) String email%0A
         ) th
@@ -2922,25 +2922,24 @@
 am(name = %22e
--
 mail%22, value
@@ -2949,24 +2949,16 @@
 %22e-mail 
-address 
 to be se
@@ -2985,482 +2985,33 @@
 rue)
-%0A                                              @RequestParam(name = %22e-mail%22) String email,%0A                                           @ApiParam(name = %22subject%22, value = %22subject to be sent to%22)%0A                                              @RequestParam(name = %22subject%22) String subject,%0A                                           @ApiParam(name = %22text%22, value = %22text to be sent to%22)%0A                                              @RequestParam(name = %22text%22) String text
+ @RequestBody Email email
 ) th
@@ -3100,16 +3100,27 @@
 id(email
+.getEmail()
 );%0A     
@@ -3295,23 +3295,34 @@
 ctEmpty(
-s
+email.getS
 ubject
+()
 );%0A     
@@ -3379,90 +3379,27 @@
 pty(
-text);%0A%0A        Email object = new Email(email, automatedSubject, automated
+email.get
 Text
+()
 );%0A
+%0A
     
@@ -3432,22 +3432,21 @@
 lToUser(
-object
+email
 );%0A%0A    
@@ -3481,14 +3481,13 @@
 y%3C%3E(
-object
+email
 , Ht

use @RequesBody