/*
 * 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 pe.siso.asistenciaAniversario.Restfull;

import javax.json.Json;
import javax.json.JsonObject;
import javax.json.JsonObjectBuilder;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.UriInfo;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.Consumes;
import javax.ws.rs.*;
import static javax.ws.rs.HttpMethod.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PUT;
import javax.ws.rs.POST;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.json.JSONObject;

/**
 * REST Web Service
 *
 * @author sistem13user
 */
@Path("Usuario")
public class UsuarioRest {

    @Context
    private UriInfo context;

    /**
     * Creates a new instance of UsuarioRest
     */
    public UsuarioRest() {
    }

    /**
     * Retrieves representation of an instance of servicio.GenericResource
     *
     * @param json
     * @return an instance of java.lang.String
     */
    @POST
    @Path("Login")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public Response getJson(String json) throws Exception {
        JsonObjectBuilder jsonObjBuilder = Json.createObjectBuilder();
        int u = 0;
        JSONObject recoData = new JSONObject(json);
        String usu = (String) recoData.get("usu");
        String pass = (String) recoData.get("pass");
//        System.out.println("Valores: " + usu + " " + pass);

        //UsuarioServices servu = new UsuarioServices();
        if (usu.equals("admin")  && pass.equals("adm159")) {
            u = 1; //servu.validarUsuario(usu, pass);
        } else {
            u = 0;
        }
        System.out.println(u);
        jsonObjBuilder.add("usu", usu);
        jsonObjBuilder.add("pass", pass);
        jsonObjBuilder.add("cod", u);

        JsonObject jsonObj = jsonObjBuilder.build();
        return Response.ok(jsonObj.toString())
                .header("Access-Control-Allow-Origin", "*")
                .header("Access-Control-Allow-Methods", "POST, GET, PUT, UPDATE, OPTIONS")
                .header("Access-Control-Allow-Headers", "Content-Type, Accept, X-Requested-With").build();
        //TODO return proper representation object
        //throw new UnsupportedOperationException();
    }

    /**
     * PUT method for updating or creating an instance of GenericResource
     *
     * @param content representation for the resource
     */
    @PUT
    @Consumes(MediaType.APPLICATION_JSON)
    public void putJson(String content) {
    }

}
