package com.rjconsultores.ventaboletos.relatorios.impl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;

import com.rjconsultores.ventaboletos.relatorios.utilitarios.DataSource;
import com.rjconsultores.ventaboletos.relatorios.utilitarios.Relatorio;
import com.rjconsultores.ventaboletos.utilerias.DateUtil;
import com.rjconsultores.ventaboletos.web.utilerias.NamedParameterStatement;

public class RelatorioDescontos extends Relatorio {
	
	List<HashMap<String, Object>> dadosRelatorio = new ArrayList<HashMap<String, Object>>();

	public RelatorioDescontos(Map<String, Object> parametros, Connection conexao) throws Exception {
		super(parametros, conexao);
		
		setCustomDataSource(new DataSource(this){
			@Override
			public void initDados() throws Exception {
				Connection coneConnection = relatorio.getConexao();
				Map<String, Object> parametros = relatorio.getParametros();
				
				Integer idPuntoVenta = (Integer) parametros.get("idPuntoVenta");
				Timestamp fecVentaInicial = new Timestamp((DateUtil.inicioFecha((Date) parametros.get("fecVentaInicial"))).getTime());
				Timestamp fecVentaFinal = new Timestamp((DateUtil.fimFecha((Date) parametros.get("fecVentaFinal"))).getTime());
				String codconvenio = (String) parametros.get("codconvenio");
				
				String query = getQuery(idPuntoVenta, codconvenio);
				System.out.println(query);
				
				NamedParameterStatement statement = new NamedParameterStatement(coneConnection, query);
				statement.setTimestamp("fecVentaInicial", fecVentaInicial);
				statement.setTimestamp("fecVentaFinal", fecVentaFinal);
				if(idPuntoVenta != null) {
					statement.setInt("idPuntoVenta", idPuntoVenta);
				}
				if(codconvenio != null && !codconvenio.isEmpty()) {
					statement.setString("codconvenio", codconvenio);
				}
				
				ResultSet resultSet = statement.executeQuery();
				
				preencherDadosRelatorio(resultSet);
			}


		});
	}

	private void preencherDadosRelatorio(ResultSet resultSet) throws SQLException {
		while (resultSet.next()) {
			HashMap<String, Object> singleData  = new HashMap<String, Object>();
			singleData.put("codConvenio", resultSet.getString("codConvenio"));
			singleData.put("nomeAgencia", resultSet.getString("nomeAgencia"));
			singleData.put("dataEmissao", resultSet.getDate("dataEmissao"));
			singleData.put("dataViagem", resultSet.getDate("dataViagem"));
			singleData.put("codServico", resultSet.getString("codServico"));
			singleData.put("codOrigem", resultSet.getString("codOrigem"));
			singleData.put("codDestino", resultSet.getString("codDestino"));
			singleData.put("tarifaComDesconto", resultSet.getDouble("tarifaComDesconto"));
			singleData.put("tut", resultSet.getDouble("tut"));
			singleData.put("pedagio", resultSet.getDouble("pedagio"));
			dadosRelatorio.add(singleData);
		}		
		
		if(!dadosRelatorio.isEmpty()) {
			setCollectionDataSource(new JRBeanCollectionDataSource(dadosRelatorio));
		}
	}

	private String getQuery(Integer idPuntoVenta, String codconvenio) {
		
      String query =  " SELECT "
    		  
			+ "   CONV.CVECONVENIO        AS codConvenio, "
			+ "   PUNT_V.NOMBPUNTOVENTA   AS nomeAgencia, "
			+ "   BOL.FECHORVENTA         AS dataEmissao, "
			+ "   BOL.FECHORVIAJE         AS dataViagem, "
			+ "   BOL.CORRIDA_ID          AS codServico, "
			+ "   ORIG.CVEPARADA          AS codOrigem, "
			+ "   DEST.CVEPARADA          AS codDestino, "
			+ "   BOL.PRECIOPAGADO        AS tarifaComDesconto, "
			+ "   BOL.IMPORTETAXAEMBARQUE AS tut, "
			+ "   BOL.IMPORTEPEDAGIO      AS pedagio "
			
			+ " FROM BOLETO BOL "
			+ " JOIN CONVENIO_DET CONV_D ON CONV_D.CONVENIODET_ID = BOL.CONVENIODET_ID "
			+ " JOIN CONVENIO       CONV ON CONV.CONVENIO_ID      = CONV_D.CONVENIO_ID "
			+ " JOIN PUNTO_VENTA  PUNT_V ON PUNT_V.PUNTOVENTA_ID  = BOL.PUNTOVENTA_ID "
			+ " JOIN PARADA         ORIG ON ORIG.PARADA_ID        = BOL.ORIGEN_ID "
			+ " JOIN PARADA         DEST ON DEST.PARADA_ID        = BOL.DESTINO_ID "
			
			+ " WHERE "
			+ " BOL.INDSTATUSOPERACION	= 'F' "
			+ " AND BOL.ACTIVO 			= 1 "			
			+ " AND BOL.FECHORVENTA BETWEEN :fecVentaInicial AND :fecVentaFinal ";
      
  		if(idPuntoVenta != null) {
  			query += " AND (BOL.PUNTOVENTA_ID = :idPuntoVenta) ";
  		}
  		if (codconvenio != null && !codconvenio.isEmpty()) {
  			query += " AND (CONV.CVECONVENIO  = :codconvenio)";
  		}
  		
  		query += " ORDER BY CONV.CVECONVENIO ";
      	
  		return query;
	}
	
	@Override
	protected void processaParametros() throws Exception {}
}