/*
 * 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.utilities.PostRequest;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.json.JSONObject;

/**
 *
 * @author sistem08user
 */
public class RedireccionarServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        final String URL = "http://172.16.2.53:8080/security-rest/api/proyecto/enviarNuevoToken";
        String auth = request.getParameter("t");
        if (auth.contains("Bearer ")) {
//            int indiceBearer = 0;
//            String[] parts = auth.split(";");
//
//            for (int i = 0; i < parts.length - 1; i++) {
//                if (parts[i].contains("Bearer")) {
//                    indiceBearer = i;
//                    break;
//                }
//            }
//            String authorization = parts[indiceBearer];
//            parts = authorization.split("%20");
//            auth = "Bearer " + parts[1];
            PostRequest sendPost = new PostRequest(URL);
            String respuesta = sendPost.getRespueta(auth);//Crearia una sesion con el codigo usuario
            JSONObject obj = new JSONObject(respuesta);
            JSONObject menu = new JSONObject(obj.get("menu"));
            Cookie cookieAuth = new Cookie("Authorization", "Bearer "+obj.getString("token"));
            Cookie cookieMenu =new Cookie("menu", menu.toString());
            System.out.println("Menu de la cookie ;: "+menu.toString());
            cookieAuth.setMaxAge(36000);//10horas
            cookieMenu.setMaxAge(36000);//10horas
            response.addCookie(cookieAuth);
            response.addCookie(cookieMenu);
            request.getRequestDispatcher("/vistas/main.jsp").forward(request, response);
        } else {
            Cookie cookieAuth = new Cookie("Authorization", "");
            Cookie cookieMenu = new Cookie("menu", "");
            cookieAuth.setMaxAge(0);
            cookieMenu.setMaxAge(0);
            response.addCookie(cookieAuth);
            response.addCookie(cookieMenu);
            request.getRequestDispatcher("/vistas/index.jsp").forward(request, response);
        }
    }

    private void listarPersona(HttpServletRequest request, HttpServletResponse response) throws ServletException {

//        response.setContentType("application/json");
//        PrintWriter pw = null;
//        JSONObject salida = new JSONObject();
//        try {
//            pw = response.getWriter();
//            String authorization =request.getHeader("Authorization");
//            if(authorization!=null && authorization.contains("Bearer ")){
//                
//                PostRequest sendPost = new PostRequest(URL);
//                String respuesta = sendPost.getRespueta(authorization);
//                JSONObject obj = new JSONObject(respuesta);
//                salida=obj;
//            }else{
//                salida.put("status", false);
//                salida.put("message", "Credenciales incorrectas.");
//            }
//        } catch (Exception e) {
//            salida.put("status", false);
//            salida.put("message", e.getMessage());
//        }
//        pw.print(salida);
    }
}
