package com.ejie.m62fb.control;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import com.ejie.m62fb.model.Adjudicatario;
import com.ejie.m62fb.model.tipos.TipoClave;
import com.ejie.m62fb.service.AdjudicatarioService;
import com.ejie.x38.dto.JQGridJSONModel;
import com.ejie.x38.dto.Pagination;
import com.ejie.x38.util.ObjectConversionManager;

/**
 * AdjudicatarioController generated by UDA, 24-oct-2012 16:55:02.
 * 
XXXX
 */

@Controller
@RequestMapping(value = "/adjudicatario")
public class AdjudicatarioController {

	private static final Logger logger = LoggerFactory
			.getLogger(AdjudicatarioController.class);

	@Autowired
	private AdjudicatarioService adjudicatarioService;

	/**
	 * Method 'getCreateForm'.
	 * 
	 * @param model
	 *            Model
	 * @return String
	 */
	@RequestMapping(value = "maint", method = RequestMethod.GET)
	public String getCreateForm(Model model) {
		AdjudicatarioController.logger.info("[GET - View] : adjudicatario");

		return "adjudicatario";
	}

	/**
	 * Method 'getById'.
	 * 
	 * @param idPersona
	 *            Integer
	 * @return adjudicatario Adjudicatario
	 */
	@RequestMapping(value = "/{idPersona}", method = RequestMethod.GET)
	public @ResponseBody
	Adjudicatario getById(@PathVariable BigDecimal idPersona) {
		Adjudicatario adjudicatario = new Adjudicatario();
		adjudicatario.setIdPersona(idPersona);
		adjudicatario = this.adjudicatarioService.find(adjudicatario);
		AdjudicatarioController.logger
				.info("[GET - findBy_PK] : Obtener Adjudicatario por PK");
		return adjudicatario;
	}

	/**
	 * AUTOCOMPLETE REMOTO
	 */
	@RequestMapping(value = "/remote", method = RequestMethod.GET)
	public @ResponseBody
	List<Adjudicatario> getRemoteAutocomplete(
			@RequestParam(value = "q", required = true) String q,
			@RequestParam(value = "c", required = true) Boolean c) {
		// Filtro
		Adjudicatario adjudicatario = new Adjudicatario();
		adjudicatario.setNombre(q);
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		return adjudicatarioService.findAllLike(adjudicatario, null, !c);
	}

	/**
	 * 
	 * @param idPersona
	 * @param tipoCodigoId
	 * @param codigoId
	 * @param nombre
	 * @param apellido1
	 * @param apellido2
	 * @param codProvincia
	 * @param codMunicipio
	 * @return
	 */
	@RequestMapping(value = "findAllLike", method = RequestMethod.GET)
	public @ResponseBody
	List<Adjudicatario> findAllLike(
			@RequestParam(value = "idPersona", required = false) String idPersona,
			@RequestParam(value = "tipoCodigoId", required = false) String tipoCodigoId,
			@RequestParam(value = "codigoId", required = false) String codigoId,
			@RequestParam(value = "nombre", required = false) String nombre,
			@RequestParam(value = "apellido1", required = false) String apellido1,
			@RequestParam(value = "apellido2", required = false) String apellido2,
			@RequestParam(value = "codProvincia", required = false) String codProvincia,
			@RequestParam(value = "codMunicipio", required = false) String codMunicipio) {
		// Filtro
		Adjudicatario adjudicatario = new Adjudicatario();
		if (idPersona != null && !idPersona.isEmpty()) {
			adjudicatario.setIdPersona(new BigDecimal(idPersona));
		}
		adjudicatario.setTipoCodigoId(new TipoClave(tipoCodigoId));
		adjudicatario.setCodigoId(codigoId);
		adjudicatario.setNombre(nombre);
		adjudicatario.setApellido1(apellido1);
		adjudicatario.setApellido2(apellido2);

		return this.adjudicatarioService
				.findAllLike(adjudicatario, null, false);
	}

	/**
	 * Method 'edit'.
	 * 
	 * @param adjudicatario
	 *            Adjudicatario
	 * @return Adjudicatario
	 */
	@RequestMapping(method = RequestMethod.PUT)
	public @ResponseBody
	Adjudicatario edit(@RequestBody Adjudicatario adjudicatario) {
		Adjudicatario adjudicatarioAux = this.adjudicatarioService
				.update(adjudicatario);
		AdjudicatarioController.logger
				.info("[PUT] : Adjudicatario actualizado correctamente");
		return adjudicatarioAux;
	}

	/**
	 * Method 'add'.
	 * 
	 * @param adjudicatario
	 *            Adjudicatario
	 * @return Adjudicatario
	 */
	@RequestMapping(method = RequestMethod.POST)
	public @ResponseBody
	Adjudicatario add(@RequestBody Adjudicatario adjudicatario) {
		Adjudicatario adjudicatarioAux = this.adjudicatarioService
				.add(adjudicatario);
		AdjudicatarioController.logger
				.info("[POST] : Adjudicatario insertado correctamente");
		return adjudicatarioAux;
	}

	/**
	 * Method 'remove'.
	 * 
	 * @param idPersona
	 *            Integer
	 * @return adjudicatario
	 */
	@RequestMapping(value = "/{idPersona}", method = RequestMethod.DELETE)
	@ResponseStatus(value = HttpStatus.OK)
	public @ResponseBody
	Adjudicatario remove(@PathVariable BigDecimal idPersona) {
		Adjudicatario adjudicatario = new Adjudicatario();
		adjudicatario.setIdPersona(idPersona);
		this.adjudicatarioService.remove(adjudicatario);
		AdjudicatarioController.logger
				.info("[DELETE] : Adjudicatario borrado correctamente");
		return adjudicatario;
	}

	/**
	 * Method 'removeAll'.
	 * 
	 * @param adjudicatarioIds
	 *            List
	 * @return adjudicatarioList
	 */
	@RequestMapping(value = "/deleteAll", method = RequestMethod.POST)
	@ResponseStatus(value = HttpStatus.OK)
	public @ResponseBody
	List<List<String>> removeMultiple(
			@RequestBody List<List<String>> adjudicatarioIds) {
		List<Adjudicatario> adjudicatarioList = new ArrayList<Adjudicatario>();
		for (List<String> adjudicatarioId : adjudicatarioIds) {
			Iterator<String> iterator = adjudicatarioId.iterator();
			Adjudicatario adjudicatario = new Adjudicatario(); // NOPMD - Objeto
																// nuevo en la
			// lista (parametro del
			// servicio)
			adjudicatario.setIdPersona(ObjectConversionManager.convert(
					iterator.next(), BigDecimal.class));
			adjudicatarioList.add(adjudicatario);
		}
		this.adjudicatarioService.removeMultiple(adjudicatarioList);
		AdjudicatarioController.logger
				.info("[POST - DELETE_ALL] : Adjudicatario borrados correctamente");
		return adjudicatarioIds;
	}

	/**
	 * Method 'getAllJQGrid'.
	 * 
	 * @param filterAdjudicatario
	 *            Adjudicatario
	 * @param pagination
	 *            Pagination
	 * @return JQGridJSONModel
	 */
	@RequestMapping(method = RequestMethod.GET, headers = { "JQGridModel=true" })
	public @ResponseBody
	JQGridJSONModel getAllJQGrid(
			@RequestParam(value = "idPersona", required = false) String idPersona,
			@RequestParam(value = "tipoCodigoId", required = false) String tipoCodigoId,
			@RequestParam(value = "codigoId", required = false) String codigoId,
			@RequestParam(value = "nombre", required = false) String nombre,
			@RequestParam(value = "apellido1", required = false) String apellido1,
			@RequestParam(value = "apellido2", required = false) String apellido2,
			@RequestParam(value = "codProvincia", required = false) String codProvincia,
			@RequestParam(value = "codMunicipio", required = false) String codMunicipio,
			@ModelAttribute Pagination pagination) {
		// Filtro
		Adjudicatario adjudicatario = new Adjudicatario();
		if (idPersona != null && !idPersona.isEmpty()) {
			adjudicatario.setIdPersona(new BigDecimal(idPersona));
		}
		adjudicatario.setTipoCodigoId(new TipoClave(tipoCodigoId));
		adjudicatario.setCodigoId(codigoId);
		adjudicatario.setNombre(nombre);
		adjudicatario.setApellido1(apellido1);
		adjudicatario.setApellido2(apellido2);

		List<Adjudicatario> adjudicatarios = this.adjudicatarioService
				.findAllLike(adjudicatario, pagination, false);
		Long recordNum = this.adjudicatarioService.findAllLikeCount(
				adjudicatario, false);
		AdjudicatarioController.logger
				.info("[GET - jqGrid] : Obtener Adjudicatario");
		return new JQGridJSONModel(pagination, recordNum, adjudicatarios);
	}

}
