package dalicia.rsvp;

import static com.google.common.base.Throwables.propagate;
import static dalicia.rsvp.JsonHelper.newLenientObjectMapper;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Locale;
import java.util.Properties;
import java.util.logging.Logger;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Optional;
import dalicia.rsvp.protocol.ErrorResponse;
import dalicia.rsvp.protocol.SuccessResponse;

public class LoginServlet extends HttpServlet {
    private static final Logger log = Logger.getLogger(LoginServlet.class.getName());

    private static final ObjectMapper objectMapper = newLenientObjectMapper();


    private static final InvitationDao invitationDao = new InvitationDaoImpl();

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        doPost(request, response);
    }

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("application/json");

        String code = request.getParameter("code");
        if (code == null) {
            response.getWriter().println(objectMapper.writeValueAsString(new ErrorResponse("badRequest", "missing 'code' parameter")));
            return;
        }

        code = code.toLowerCase(Locale.ENGLISH);
        Optional<Invitation> invitation = invitationDao.load(code);

        if (!invitation.isPresent()) {
            log.info("bad code: '" + code + "'");
            response.getWriter().println(objectMapper.writeValueAsString(new ErrorResponse("badCode", "unrecognized code: '" + code + "'")));
            return;
        }

        log.info("got invitation: " + invitation.get());

        response.getWriter().println(objectMapper.writeValueAsString(new SuccessResponse(invitation.get())));
//        sendEmail();
    }

    private void sendEmail() {

        Properties props = new Properties();
        Session session = Session.getDefaultInstance(props, null);

        String msgBody = "...";

        try {
            Message msg = new MimeMessage(session);
            msg.setFrom(new InternetAddress("dnault@gmail.com", "Dave Nault"));
            msg.addRecipient(Message.RecipientType.TO,
                    new InternetAddress("dnault@mac.com", "Dave Nault"));
            msg.setSubject("someone made a request");
            msg.setText(msgBody);
            Transport.send(msg);

        } catch (Exception e) {
            throw propagate(e);
        }
    }
}

@@ -2159,687 +2159,8 @@
 ));%0A
-//        sendEmail();%0A    %7D%0A%0A    private void sendEmail() %7B%0A%0A        Properties props = new Properties();%0A        Session session = Session.getDefaultInstance(props, null);%0A%0A        String msgBody = %22...%22;%0A%0A        try %7B%0A            Message msg = new MimeMessage(session);%0A            msg.setFrom(new InternetAddress(%22dnault@gmail.com%22, %22Dave Nault%22));%0A            msg.addRecipient(Message.RecipientType.TO,%0A                    new InternetAddress(%22dnault@mac.com%22, %22Dave Nault%22));%0A            msg.setSubject(%22someone made a request%22);%0A            msg.setText(msgBody);%0A            Transport.send(msg);%0A%0A        %7D catch (Exception e) %7B%0A            throw propagate(e);%0A        %7D%0A
     

remove dead code