package com.ejie.m62fb.control;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ejie.m62fb.model.tipos.AbstractTipoGenericoP28;
import com.ejie.m62fb.model.tipos.ComboP28;
import com.ejie.m62fb.model.tipos.ComboR09;
import com.ejie.m62fb.model.tipos.TipoElemento;
import com.ejie.m62fb.model.tipos.TipoVinculacion;
import com.ejie.m62fb.service.ComboP28Service;
import com.ejie.m62fb.service.ComboR09Service;

/**
 * ComboController
 * 
XXXX
 * 
 * @param <T>
 */
@Controller
@RequestMapping(value = "/combo")
public class ComboController<T extends AbstractTipoGenericoP28> {
	private static final Logger LOGGER = LoggerFactory
			.getLogger(ComboController.class);

	@Autowired
	private ComboP28Service<T> comboP28Service;

	@Autowired
	@SuppressWarnings("rawtypes")
	private ComboR09Service comboR09Service;

	/**
	 * Method 'getCombo'.
	 * 
	 * @param numRegistro
	 *            String Tipo de combo a buscar
	 * 
	 * @return List
	 */
	@RequestMapping(value = "/p28/{numRegistro}", method = RequestMethod.GET)
	public @ResponseBody
	List<? extends AbstractTipoGenericoP28> getComboP28(
			@PathVariable int numRegistro) {
		ComboP28 combo = new ComboP28(numRegistro);
		ComboController.LOGGER
				.info("[GET - find_ALL] : Obtener Combo por filtro");
		List<T> listaCombo = (List<T>) this.comboP28Service.findAll((T) combo);

		// borrar VIVIENDA/LOCAL de los tipos de elemento
		if (numRegistro == TipoElemento.NUM_REGISTRO) {
			List<T> listaComboAux = new ArrayList<T>();
			for (T t : listaCombo) {
				if (
//						!t.getCodTabla().equalsIgnoreCase("L")
//						&& 
						!t.getCodTabla().equalsIgnoreCase("V")) {
					listaComboAux.add(t);

				}
			}
			listaCombo = listaComboAux;
		}
		// borrar FINANCIADO de los tipos de VINCULACION
		if (numRegistro == TipoVinculacion.NUM_REGISTRO) {
			List<T> listaComboAux = new ArrayList<T>();
			for (T t : listaCombo) {
				if (!t.getCodTabla().equalsIgnoreCase("F")) {
					listaComboAux.add(t);

				}
			}
			listaCombo = listaComboAux;
		}
		return listaCombo;
	}

	/**
	 * Method 'getComboR09'.
	 * 
	 * @param codTipoEntidad
	 *            String Tipo de combo a buscar
	 * 
	 * @return List<ComboR09>
	 */
	@RequestMapping(value = "/r09/{codTipoEntidad}", method = RequestMethod.GET)
	@SuppressWarnings("unchecked")
	public @ResponseBody
	List<ComboR09> getComboR09(@PathVariable String codTipoEntidad) {
		ComboR09 combo = new ComboR09(codTipoEntidad);
		List<ComboR09> listaCombo = this.comboR09Service.findAll(combo);
		return listaCombo;
	}
}