/*
 * 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 java.util.ArrayList;
import javax.json.Json;
import javax.json.JsonArray;
import javax.json.JsonArrayBuilder;
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.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PUT;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.json.JSONObject;
import pe.siso.asistenciaAniversario.Beans.Conteo;
import pe.siso.asistenciaAniversario.Services.ConteoServices;

/**
 * REST Web Service
 *
 * @author sistem13user
 */
@Path("Ticket")
public class TicketRes {

    @Context
    private UriInfo context;

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

    /**
     * Retrieves representation of an instance of servicio.GenericResource
     *
     * @param json
     * @return an instance of java.lang.String
     */
    @POST
    @Path("registrarTicket")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public Response registrarTicket(String ticket) throws Exception {
        JsonObjectBuilder jsonObjBuilder = Json.createObjectBuilder();
        JSONObject recoData = new JSONObject(ticket);
        String ticketString = (String) recoData.get("ticket");
        ConteoServices servicios = new ConteoServices();
        int dato = 0;
        int cantidad = 0;
        String cantidadFinal = "00000000";
        int respuesta = 0;
        String tipo = "";
        System.out.println("ticket: " + ticketString);

        if (ticketString.equals("72923976")) {
            tipo = "Cortesía";
        } else if (ticketString.equals("70014656")) {
            tipo = "General";
        } else {
            tipo = "Ninguno";
        }
        System.out.println("tipo: " + tipo);
        dato = servicios.registrarTicket(tipo);
        if (tipo.equals("Ninguno")) {
            if (dato == 1) {
                respuesta = 0;
            } else {
                respuesta = 2;
            }
        } else {
            if (dato == 1) {
                respuesta = 1;
            } else {
                respuesta = 2;
            }
        }

        cantidad = servicios.totalTicket();

        cantidadFinal = String.format("%08d", cantidad);

        jsonObjBuilder.add("respuesta", respuesta);
        jsonObjBuilder.add("tipo", tipo);
        jsonObjBuilder.add("cantidadFinal", cantidadFinal);

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

    @POST
    @Path("cantidadTotal")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public Response cantidadTotal() throws Exception {
        JsonObjectBuilder jsonObjBuilder = Json.createObjectBuilder();
        ConteoServices servicios = new ConteoServices();
        int cantidad = 0;
        String cantidadFinal = "00000000";
        cantidad = servicios.totalTicket();
        cantidadFinal = String.format("%08d", cantidad);

        jsonObjBuilder.add("cantidadFinal", cantidadFinal);

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

    @POST
    @Path("consultarTotalEntradasPorTipo")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public Response consultarTotalEntradasPorTipo() throws Exception {
        JsonArrayBuilder jsonArrayBuilder = Json.createArrayBuilder();
        ConteoServices servicios = new ConteoServices();
        ArrayList<Conteo> lista = new ArrayList<Conteo>();
        lista = servicios.consultarTotalEntradasPorTipo();


        for (int i = 0; i < lista.size(); i++) {
            JsonObjectBuilder jsonObjBuilder = Json.createObjectBuilder();
           JsonObject jsonobject = jsonObjBuilder.add("tipo", lista.get(i).getTipo() )
                   .add("cantidad", String.format("%08d", lista.get(i).getCantidad()) ).build();
           jsonArrayBuilder.add(jsonobject);           
        }
        
        JsonArray jsonArray = jsonArrayBuilder.build();

        return Response.ok(jsonArray.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();
    }

}
