/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package demojsoncrud.servlets;

import demojsoncrud.beans.PersonaBean;
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;

/**
 *
 * @author Alonso
 */
public class PersonaServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String param = request.getParameter("accion");
        if ("listarPersonaTest".equals(param)) {
            listarPersonaTest(request, response);
        } else if ("listarPersona".equals(param)) {
            listarPersona(request, response);
        } else if ("registrarPersona".equals(param)) {
            registrarPersona(request, response);
        } else if ("editarPersona".equals(param)) {
            editarPersona(request, response);
        } else if ("activarPersona".equals(param)) {
            activarPersona(request, response);
        } else if ("desactivarPersona".equals(param)) {
            desactivarPersona(request, response);
        }
    }

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

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

        String draw = request.getParameter("draw");
        String start = request.getParameter("start");
        String length = request.getParameter("length");
        String search = request.getParameter("search");

        System.out.println("draw => " + draw + " | start => " + start + " | length => " + length + " | search => " + search);
        JSONObject json = service.listarPersona(search, draw, start, length);
        pw.print(json);
    }

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

        String json = request.getParameter("json");
        JSONObject jsonObject = new JSONObject(json);

        String dni = jsonObject.getString("dni");
        String apellidos = jsonObject.getString("apellidos");
        String nombres = jsonObject.getString("nombres");
        String edad = jsonObject.getString("edad");
        String correo = jsonObject.getString("correo");

        PersonaBean bean = new PersonaBean();
        bean.setDni(dni);
        bean.setApellidos(apellidos);
        bean.setNombres(nombres);
        bean.setEdad(edad);
        bean.setCorreo(correo);

        JSONObject jsonReturn = service.registrarPersona(bean);

        pw.print(jsonReturn);

    }

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

        String json = request.getParameter("json");
        JSONObject jsonObject = new JSONObject(json);

        String apellidos = jsonObject.getString("apellidos");
        String nombres = jsonObject.getString("nombres");
        String edad = jsonObject.getString("edad");
        String correo = jsonObject.getString("correo");
        String codigo = jsonObject.getString("codigo");

        PersonaBean bean = new PersonaBean();
        bean.setApellidos(apellidos);
        bean.setNombres(nombres);
        bean.setEdad(edad);
        bean.setCorreo(correo);
        bean.setCodigo(codigo);

        JSONObject jsonReturn = service.editarPersona(bean);

        pw.print(jsonReturn);

    }

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

        String json = request.getParameter("json");
        JSONObject jsonObject = new JSONObject(json);

        String codigo = jsonObject.getString("codigo");
        PersonaBean bean = new PersonaBean();
        bean.setCodigo(codigo);

        JSONObject jsonReturn = service.activarPersona(bean);

        pw.print(jsonReturn);
    }

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

        String json = request.getParameter("json");
        JSONObject jsonObject = new JSONObject(json);

        String codigo = jsonObject.getString("codigo");
        PersonaBean bean = new PersonaBean();
        bean.setCodigo(codigo);

        JSONObject jsonReturn = service.desactivarPersona(bean);

        pw.print(jsonReturn);
    }

}
