package mainapp;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class AddProductionAction extends HttpServlet {

    /**
     * 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 {
        request.setCharacterEncoding("UTF-8");
        String factory_id = null;
        String olives_weight = null;
        String oil_weight = null;
        String acidity = null;
        String costs = null;
        String user_id = null;
        String bio = null;
        try {
            factory_id = (request.getParameter("factory"));
            bio = (request.getParameter("bio"));
            olives_weight = (request.getParameter("olives_weight"));
            oil_weight = (request.getParameter("oil_weight"));
            acidity = (request.getParameter("acidity"));
            costs = (request.getParameter("costs"));
            user_id = (request.getParameter("user_id"));
            int timestamp = (int) (System.currentTimeMillis() / 1000L);
            
            Map<String, String> fields = new HashMap<>();
            fields.put("producer_id", user_id);
            fields.put("is_bio", bio);
            fields.put("factory_id", factory_id);
            fields.put("timestamp", Integer.toString(timestamp));
            fields.put("olives_weight", olives_weight);
            fields.put("oil_weight", oil_weight);
            fields.put("acidity", acidity);
            fields.put("costs", costs);
            try {
                DB.insertRow("production", fields);
            } catch (Exception ex) {
                request.setAttribute("errorMessage", "Συνέβει κάποιο σφάλμα. Παρακαλώ προσπαθήστε ξανά");
                request.getRequestDispatcher("?action=add_production&user_id="+user_id).forward(request, response);
                return;
            }
            response.sendRedirect("?action=productions&insert=1&user_id="+user_id);
        } catch (Exception e) {
            request.setAttribute("errorMessage", "Παρακαλώ συμπληρώστε όλα τα πεδία");
            request.setAttribute("factory", factory_id);
            request.setAttribute("address", olives_weight);
            request.setAttribute("owner", oil_weight);
            request.setAttribute("phone", acidity);
            request.setAttribute("phone", costs);
            request.getRequestDispatcher("?action=add_production&user_id="+user_id).forward(request, response);
            return;
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * 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 {
        processRequest(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";
    }// </editor-fold>

}
