package com.rjconsultores.ventaboletos.ws.rs;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;

import com.rjconsultores.ventaboletos.service.NotaCreditoVendaPacoteService;
import com.rjconsultores.ventaboletos.service.OCDService;
import com.rjconsultores.ventaboletos.vo.reembolsoocdnotacredito.ReembolsoOcdNotaCreditoVO;
import com.rjconsultores.ventaboletos.web.utilerias.spring.AppContext;

@Path("/reembolsoocdnotacredito")
public class ReembolsoOcdNotaCreditoRS {
	
	private static SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
	
	@GET
	@Produces({ MediaType.APPLICATION_XML })
	public List<ReembolsoOcdNotaCreditoVO> busquedaDatosReembolsoOcdNotaCreditoRS(@QueryParam("fecinicial") String fecInicialParam,
			@QueryParam("fecfinal") String fecFinalParam, @QueryParam("fecpagamentoinicial") String fecPagamentoInicialParam,
			@QueryParam("fecpagamentofinal") String fecPagamentoFinalParam, @QueryParam("pago") Boolean indPago) {
		
		Date fecInicial = getDateFromString(fecInicialParam);
		Date fecFinal = getDateFromString(fecFinalParam);
		Date fecPagamentoInicial = getDateFromString(fecPagamentoInicialParam);
		Date fecPagamentoFinal = getDateFromString(fecPagamentoFinalParam);

		OCDService ocdService = (OCDService) AppContext.getApplicationContext().getBean(OCDService.class);
		List<ReembolsoOcdNotaCreditoVO> result = ocdService.busquedaDatosReembolsoOcdNotaCreditoRS(fecInicial, fecFinal, fecPagamentoInicial, fecPagamentoFinal, indPago);
		
		NotaCreditoVendaPacoteService notaCreditoVendaPacoteService = (NotaCreditoVendaPacoteService) AppContext.getApplicationContext().getBean(NotaCreditoVendaPacoteService.class);
		result.addAll(notaCreditoVendaPacoteService.busquedaDatosReembolsoOcdNotaCreditoRS(fecInicial, fecFinal, fecPagamentoInicial, fecPagamentoFinal, indPago));
		
		return result;
		
	}
	
	private Date getDateFromString(String dateString) {
		try {
			return dateFormat.parse(dateString);
		} catch (Exception e) {
			return null;
		}
	}

}
