package ch.heigvd.amt.gamy.web.controllers;

import ch.heigvd.amt.gamy.model.APIKey;
import ch.heigvd.amt.gamy.services.dao.ApplicationDAOLocal;

import java.io.IOException;

import java.util.logging.Level;
import java.util.logging.Logger;

import javax.ejb.EJB;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * RegisterApplicationServlet is the controller to create a new application.
 * This controller should check the data from the create application from 
 * before to create a new app in Gamy.
 * 
 * doGet show the form to create a new app with some auto-generated data.
 * doPost create the application with the information gave by the user.
 * 
 * This servlet answer to the doGet() and doPost().
 */
@WebServlet(name = "RegisterApplicationServlet", urlPatterns = {"/RegisterApplicationServlet"})
public class RegisterApplicationServlet extends HttpServlet {

   @EJB
   ApplicationDAOLocal applicationDAO;

   /**
    * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
    * methods.
    *
    * @param request servlet request
    * @param response servlet response
    * @throws ServletException if a servlet-specific error occurs
    * @throws IOException if an I/O error occurs
    */
   protected void processRequest(HttpServletRequest request, HttpServletResponse response)
           throws ServletException, IOException {
      response.setContentType("text/html;charset=UTF-8");
      
      // Read the information from the form.
      String name = request.getParameter("name");
      String description = request.getParameter("description");
      String apiKey = request.getParameter("apikey");
      String status = request.getParameter("status");
      boolean state = status.equalsIgnoreCase("Enable");

      // Create a new application using the information from the form and the email 
      // address of the user who is creating it.
      applicationDAO.createApp((String) request.getSession().getAttribute("principal"), 
              name, description, state, apiKey);
      
      // Redirect the user to the homeapps
      // TODO: notify the client as the app is created
      response.sendRedirect("homeapps");
   }

   /**
    * Handles the HTTP <code>GET</code> method.
    *
    * @param request servlet request
    * @param response servlet response
    * @throws ServletException if a servlet-specific error occurs
    * @throws IOException if an I/O error occurs
    */
   @Override
   protected void doGet(HttpServletRequest request, HttpServletResponse response)
           throws ServletException, IOException {

      // Read the email address of the user from the sessions.
      String email = (String) request.getSession().getAttribute("principal");
      request.setAttribute("acc", email);
      
      // Generate a new API key for the future application.
      APIKey apikey = new APIKey(email);
      request.setAttribute("key", apikey.getApikey());

      // For a new app there no end user.
      request.setAttribute("nbUser", "0");

      // Do a log and forward the request.
      Logger.getLogger(LoginServlet.class.getName()).log(Level.INFO, apikey.getApikey());
      request.getRequestDispatcher("/WEB-INF/pages/registerApp.jsp").forward(request, response);
   }

   /**
    * Handles the HTTP <code>POST</code> method.
    *
    * @param request servlet request
    * @param response servlet response
    * @throws ServletException if a servlet-specific error occurs
    * @throws IOException if an I/O error occurs
    */
   @Override
   protected void doPost(HttpServletRequest request, HttpServletResponse response)
           throws ServletException, IOException {
      processRequest(request, response);
   }

   /**
    * Returns a short description of the servlet.
    *
    * @return a String containing servlet description
    */
   @Override
   public String getServletInfo() {
      return "Short description";
   }
}
