package demojsoncrud.servlets;

import demojsoncrud.services.PersonaService;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.json.JSONObject;

public class PersonaServlet extends HttpServlet {

  @Override
  protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String param = request.getParameter("accion");
    switch (param) {
      case "listarPersona":
        listarPersona(request, response);
        break;
      case "registrarPersona":
        registrarPersona(request, response);
        break;
      case "editarPersona":
        editarPersona(request, response);
        break;
      case "cambiarEstado":
        cambiarEstado(request, response);
        break;
      default:
        response.setContentType("application/json");
        PrintWriter pw = response.getWriter();
        JSONObject respuesta = new JSONObject();
        respuesta
                .put("status", false)
                .put("message", "No existe la url solicitada.")
                .put("data", "");
        pw.print(respuesta);
        break;
    }
  }

  private void listarPersona(HttpServletRequest request, HttpServletResponse response) throws IOException {
    response.setContentType("application/json");
    PrintWriter pw = response.getWriter();
    PersonaService service = new PersonaService();

    String search = request.getParameter("search");

    JSONObject datos = new JSONObject();
    datos.put("draw", request.getParameter("draw"));
    datos.put("start", request.getParameter("start"));
    datos.put("length", request.getParameter("length"));
    datos.put("search", search);

    JSONObject json = service.listarPersona(datos);
    pw.print(json);
  }

  private void registrarPersona(HttpServletRequest request, HttpServletResponse response) throws IOException {
    response.setContentType("application/json");
    PrintWriter pw = response.getWriter();
    PersonaService service = new PersonaService();
    JSONObject datos = new JSONObject(request.getParameter("json"));
    JSONObject respuesta = service.registrarPersona(datos);
    pw.print(respuesta);

  }

  private void editarPersona(HttpServletRequest request, HttpServletResponse response) throws IOException {
    response.setContentType("application/json");
    PrintWriter pw = response.getWriter();
    PersonaService service = new PersonaService();
    JSONObject datos = new JSONObject(request.getParameter("json"));
    JSONObject respuesta = service.editarPersona(datos);
    pw.print(respuesta);
  }

  private void cambiarEstado(HttpServletRequest request, HttpServletResponse response) throws IOException {
    response.setContentType("application/json");
    PrintWriter pw = response.getWriter();
    PersonaService service = new PersonaService();
    JSONObject datos = new JSONObject(request.getParameter("json"));
    JSONObject respuesta = service.cambiarEstado(datos);
    pw.print(respuesta);
  }

}
