package com.rjconsultores.ventaboletos.vo.busquedapacotes.transformer;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.transform.ResultTransformer;

import com.rjconsultores.ventaboletos.vo.busquedapacotes.ServicoVO;

public class DatosServicoResultTransformer implements ResultTransformer {

	private static final long serialVersionUID = 1L;

	@Override
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public List<ServicoVO> transformList(List mapList) {
		Map<Long, ServicoVO> servicos = new HashMap<Long, ServicoVO>();
		
		for (Object objmap : mapList) {
			
			Map<String, Object> tupleMap = (Map<String, Object>) objmap;
			Long vendapacoteId = (Long) tupleMap.get("VENDAPACOTE_ID");
			
			if(!servicos.containsKey(vendapacoteId)){
				ServicoVO servico = new ServicoVO();
				servico.setVendapacoteId(vendapacoteId);
				setDatosServico(servico, tupleMap);
				servicos.put(vendapacoteId, servico);				
			}
		}
				
		List<ServicoVO> servicosList = new ArrayList<ServicoVO>();
		servicosList.addAll(servicos.values());
		
		return servicosList;
	}

	private void setDatosServico(ServicoVO servico, Map<String, Object> tupleMap) {
		servico.setDataviagem((Date) tupleMap.get("FECHORVIAJE"));
		servico.setHorario((Date) tupleMap.get("FECHORVIAJE"));
		servico.setLocalorigem((Integer) tupleMap.get("ORIGEN_ID"));
		servico.setOrigem((String) tupleMap.get("ORIGEM"));
		servico.setDestino((String) tupleMap.get("DESTINO"));
	}

	@Override
	public Object transformTuple(Object[] data, String[] aliases) {
		Map<String,Object> row = new HashMap<String,Object>();
		
		for(int index = 0; index < aliases.length; index++){
			row.put(aliases[index], data[index]);
		}
		
		return row;
	}

}
