package Servlet;

import Datos.JornadaDB;
import Modelo.CodClub;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

public class ElegirJornadaServlet extends HttpServlet {

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        String encoding = request.getCharacterEncoding();
        if (encoding == null) {
            request.setCharacterEncoding("UTF-8");
        }

        String url;
        
        String operacion = request.getParameter("operacion");
        CodClub club = (CodClub) request.getSession().getAttribute("club");
        ArrayList <String> jornadas = JornadaDB.getFechasJornada(club.getId_club());
        ArrayList<String> fechas = new ArrayList<>();
        for(int i = 0; i < jornadas.size(); i++){
            String fecha = jornadas.get(i);
            SimpleDateFormat formato = new SimpleDateFormat("yyyy-MM-dd");
            Date fecha_entrenamiento = null;
            try{
                fecha_entrenamiento = formato.parse(fecha);
            }catch(ParseException ex){
                  ex.printStackTrace();
            }
            String fecha_final = fecha_entrenamiento.getDate()+"/"+(fecha_entrenamiento.getMonth()+1)+"/"+(fecha_entrenamiento.getYear()+1900);
            fechas.add(fecha_final);
        }
        request.setAttribute("jornadas", jornadas);
        request.setAttribute("fechas", fechas);
        url = "/elegirJornada.jsp?operacion="+operacion;
        RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(url);
        dispatcher.forward(request, response);
    }

    // <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);
    }
}
