package tutorial.rest.mvc;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestParam;
import tutorial.core.models.entities.Message;
import tutorial.core.services.AccountService;
import tutorial.core.services.MessageService;
import tutorial.core.services.exceptions.AccountDoesNotExistException;
import tutorial.core.services.util.MessageList;
import tutorial.rest.exceptions.ConflictException;
import tutorial.rest.resources.MessageListResource;
import tutorial.rest.resources.MessageResource;
import tutorial.rest.resources.asm.MessageListResourceAsm;
import tutorial.rest.resources.asm.MessageResourceAsm;



/**
 * 
 * @author Xavier
 */
@Controller
@RequestMapping("/rest/message")
public class MessageController {
    private MessageService messageService;
    private AccountService accountService;

    /**
     *
     * @param messageService
     * @param accountService
     */
    @Autowired
    public MessageController(MessageService messageService, AccountService accountService) {
        this.messageService = messageService;
        this.accountService = accountService;
    }

    /**
     * Adds one message to the database
     * @param payload the values that the created message will have
     * @return returns the created message
     */
    @RequestMapping(method = RequestMethod.POST)
    @PreAuthorize("permitAll")
    public ResponseEntity<MessageResource> createMessage(
            @RequestBody MessageResource payload)
    {
        try {
            Message createdMessage = messageService.createMessage(payload);
            MessageResource createdMessageRes = new MessageResourceAsm().toResource(createdMessage);
            return new ResponseEntity<MessageResource>(createdMessageRes, HttpStatus.CREATED);
        } catch(AccountDoesNotExistException exception)
        {
            throw new ConflictException(exception);
        }
    }

    /**
     * Returns a List of all the messages
     * @return a List of all the messages
     */
    @RequestMapping(method = RequestMethod.GET)
    @PreAuthorize("permitAll")
    public ResponseEntity<MessageListResource> findAllMessages()
    {
        MessageList messageList = messageService.findAllMessages();
        MessageListResource messageListRes = new MessageListResourceAsm().toResource(messageList);
        return new ResponseEntity<MessageListResource>(messageListRes, HttpStatus.OK);
    }
    
    /**
     * Clears all the email domains from the database
     * @param name the user name 
     * @param password the user password in MD5
     * @return returns a message with OK or CONFLICT
     */
    @RequestMapping(value="/clearTable",
    method = RequestMethod.POST)
    @PreAuthorize("permitAll")
    public ResponseEntity deleteAllEmailDomains(
            @RequestParam(value="name", required = true) String name,
            @RequestParam(value="password", required = true) String password) {
        if(!accountService.isAccountCorrect(name, password)) {
            return new ResponseEntity(HttpStatus.CONFLICT);
        }else{
            messageService.deleteAllMessages();
        }
        return new ResponseEntity(HttpStatus.OK);
    }
}
