package com.ejie.ab61.service.expedientes;

import java.util.List;

import org.apache.commons.io.FilenameUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ejie.ab61.dao.expedientes.RelExpPlaDao;
import com.ejie.ab61.exceptions.PifException;
import com.ejie.ab61.model.expedientes.RelExpPla;
import com.ejie.ab61.tramita.adapter.PifAdapterService;
import com.ejie.x38.dto.JQGridRequestDto;
import com.ejie.x38.dto.JQGridResponseDto;
import com.ejie.x38.dto.JerarquiaDto;
import com.ejie.x38.dto.TableRowDto;
import com.ejie.y31.vo.Y31AttachmentBean;

/**
 * RelExpPlaServiceImpl  
 * 
 *  
 */

@Service(value = "relExpPlaService")
public class RelExpPlaServiceImpl implements RelExpPlaService {

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

	@Autowired
	private RelExpPlaDao relExpPlaDao;
	@Autowired
	private PifAdapterService pifAdapterService;

	public String getRuta(RelExpPla relExpPla) throws PifException {
		int cont = 0;
		String path = "";
		String ruta = null;
		RelExpPla relexpplaAux = new RelExpPla();

		if (null != relExpPla.getExpediente()) {
			relexpplaAux = this.relExpPlaDao.obtenerDatosAportarDocumento(relExpPla);
		} else if (null != relExpPla.getInspeccion()) {
			relexpplaAux = this.relExpPlaDao.obtenerDocumentoDesdeInspeccion(relExpPla);
		} else {
			relexpplaAux = this.relExpPlaDao.obtenerDocumentoDesdeProcedimiento(relExpPla);
		}

		// buscamos si aun esta el archivo en piff
		if (null != relexpplaAux && null != relexpplaAux.getRuta()) {
			RelExpPlaServiceImpl.logger
					.info("[GET - findBy_PK] : Obtener RelExpPla por PK:" + relexpplaAux.getIdRelExpPla());
			String name = FilenameUtils.getName(relexpplaAux.getRuta());
			path = FilenameUtils.getPath(relexpplaAux.getRuta());

			List<Y31AttachmentBean> files = pifAdapterService.listFile("/" + path);
			for (int i = 0; i < files.size(); i++) {
				if (files.get(i).getFileName().equals(name)) {
					cont = 1;
					ruta = relexpplaAux.getRuta();
					break;

				}
			}
			if (cont == 0) {
				ruta = null;
				relExpPlaDao.remove(relexpplaAux);
			}

		}

		return ruta;
	}

	/**
	 * Trear la ruta de la plantilla para abrriela con el webDav
	 *
	 * @param idRelExpPla
	 *            RelExpPla
	 * @return ruta String
	 */

	public String getRutaNegocio(RelExpPla relExpPla) throws PifException {
		int cont = 0;
		String path = "";
		String ruta = null;
		RelExpPla relexpplaAux = new RelExpPla();

		relexpplaAux = this.relExpPlaDao.obtenerRutaByIdentificador(relExpPla);

		// buscamos si aun esta el archivo en piff
		if (null != relexpplaAux && null != relexpplaAux.getRuta()) {
			RelExpPlaServiceImpl.logger
					.info("[GET - findBy_PK] : Obtener RelExpPla por PK:" + relexpplaAux.getIdRelExpPla());
			String name = FilenameUtils.getName(relexpplaAux.getRuta());
			path = FilenameUtils.getPath(relexpplaAux.getRuta());

			List<Y31AttachmentBean> files = pifAdapterService.listFile("/" + path);
			for (int i = 0; i < files.size(); i++) {
				if (files.get(i).getFileName().equals(name)) {
					cont = 1;
					ruta = relexpplaAux.getRuta();
					break;

				}
			}
			if (cont == 0) {
				ruta = null;
				relExpPlaDao.remove(relexpplaAux);
			}

		}

		return ruta;
	}

	/**
	 * Inserts a single row in the RelExpPla table.
	 *
	 * @param relExpPla
	 *            RelExpPla
	 * @return RelExpPla
	 */
	@Transactional(rollbackFor = Throwable.class)
	public RelExpPla add(RelExpPla relExpPla) {
		return this.relExpPlaDao.add(relExpPla);
	}

	/**
	 * Updates a single row in the RelExpPla table.
	 *
	 * @param relExpPla
	 *            RelExpPla
	 * @return RelExpPla
	 */
	@Transactional(rollbackFor = Throwable.class)
	public RelExpPla update(RelExpPla relExpPla) {
		return this.relExpPlaDao.update(relExpPla);
	}

	/**
	 * Finds a single row in the RelExpPla table.
	 *
	 * @param relExpPla
	 *            RelExpPla
	 * @return RelExpPla
	 */
	public RelExpPla find(RelExpPla relExpPla) {
		return (RelExpPla) this.relExpPlaDao.find(relExpPla);
	}

	/**
	 * Finds a single row in the RelExpPla table.
	 *
	 * @param relExpPla
	 *            RelExpPla
	 * @return RelExpPla
	 */
	public RelExpPla obtenerDatosAportarDocumento(RelExpPla relExpPla) {
		return (RelExpPla) this.relExpPlaDao.obtenerDatosAportarDocumento(relExpPla);
	}

	/**
	 * Deletes a single row in the RelExpPla table.
	 *
	 * @param relExpPla
	 *            RelExpPla
	 */
	@Transactional(rollbackFor = Throwable.class)
	public void remove(RelExpPla relExpPla) {
		this.relExpPlaDao.remove(relExpPla);
	}

	/**
	 * Finds a list of rows in the RelExpPla table.
	 *
	 * @param relExpPla
	 *            RelExpPla
	 * @param jqGridRequestDto
	 *            JQGridRequestDto
	 * @return List<RelExpPla>
	 */
	public List<RelExpPla> findAll(RelExpPla relExpPla, JQGridRequestDto jqGridRequestDto) {
		return (List<RelExpPla>) this.relExpPlaDao.findAll(relExpPla, jqGridRequestDto);
	}

	/**
	 * Finds rows in the RelExpPla table using like.
	 *
	 * @param relExpPla
	 *            RelExpPla
	 * @param jqGridRequestDto
	 *            JQGridRequestDto
	 * @param startsWith
	 *            Boolean
	 * @return List<RelExpPla>
	 */
	public List<RelExpPla> findAllLike(RelExpPla relExpPla, JQGridRequestDto jqGridRequestDto, Boolean startsWith) {
		return (List<RelExpPla>) this.relExpPlaDao.findAllLike(relExpPla, jqGridRequestDto, startsWith);
	}

	/*
	 * OPERACIONES RUP_TABLE
	 */

	/**
	 * Removes rows from the RelExpPla table.
	 *
	 * @param filterRelExpPla
	 *            RelExpPla
	 * @param jqGridRequestDto
	 *            JQGridRequestDto
	 * @param startsWith
	 *            Boolean
	 */
	public void removeMultiple(RelExpPla filterRelExpPla, JQGridRequestDto jqGridRequestDto, Boolean startsWith) {
		this.relExpPlaDao.removeMultiple(filterRelExpPla, jqGridRequestDto, startsWith);
	}

	/**
	 * Filter method in the RelExpPla table.
	 *
	 * @param filterRelExpPla
	 *            RelExpPla
	 * @param jqGridRequestDto
	 *            JQGridRequestDto
	 * @param startsWith
	 *            Boolean
	 * @return JQGridResponseDto<RelExpPla>
	 */
	public JQGridResponseDto<RelExpPla> filter(RelExpPla filterRelExpPla, JQGridRequestDto jqGridRequestDto,
			Boolean startsWith) {
		List<RelExpPla> listaRelExpPla = this.relExpPlaDao.findAllLike(filterRelExpPla, jqGridRequestDto, false);
		Long recordNum = this.relExpPlaDao.findAllLikeCount(filterRelExpPla != null ? filterRelExpPla : new RelExpPla(),
				false);
		if (jqGridRequestDto.getMultiselection().getSelectedIds() != null) {
			List<TableRowDto<RelExpPla>> reorderSelection = this.relExpPlaDao.reorderSelection(filterRelExpPla,
					jqGridRequestDto, startsWith);
			return new JQGridResponseDto<RelExpPla>(jqGridRequestDto, recordNum, listaRelExpPla, reorderSelection);
		}
		return new JQGridResponseDto<RelExpPla>(jqGridRequestDto, recordNum, listaRelExpPla);
	}

	/**
	 * Searches rows in the RelExpPla table.
	 *
	 * @param filterRelExpPla
	 *            RelExpPla
	 * @param searchRelExpPla
	 *            RelExpPla
	 * @param jqGridRequestDto
	 *            JQGridRequestDto
	 * @param startsWith
	 *            Boolean
	 * @return List<TableRowDto<RelExpPla>>
	 */
	public List<TableRowDto<RelExpPla>> search(RelExpPla filterRelExpPla, RelExpPla searchRelExpPla,
			JQGridRequestDto jqGridRequestDto, Boolean startsWith) {
		return this.relExpPlaDao.search(filterRelExpPla, searchRelExpPla, jqGridRequestDto, startsWith);
	}

	/**
	 * Reorder the selection made in RelExpPla table.
	 *
	 * @param filterRelExpPla
	 *            RelExpPla
	 * @param jqGridRequestDto
	 *            JQGridRequestDto
	 * @param startsWith
	 *            Boolean
	 * @return Object
	 */
	public Object reorderSelection(RelExpPla filterRelExpPla, JQGridRequestDto jqGridRequestDto, Boolean startsWith) {
		return this.relExpPlaDao.reorderSelection(filterRelExpPla, jqGridRequestDto, startsWith);
	}

	/*
	 * OPERACIONES RUP_TABLE JERARQUIA
	 */

	/**
	 * Finder method in the hierarchical the RelExpPla table.
	 *
	 * @param filterRelExpPla
	 *            RelExpPla
	 * @param jqGridRequestDto
	 *            JQGridRequestDto
	 * @param startsWith
	 *            Boolean
	 * @return JQGridResponseDto<JerarquiaDto<RelExpPla>>
	 */
	public JQGridResponseDto<JerarquiaDto<RelExpPla>> jerarquia(RelExpPla filterRelExpPla,
			JQGridRequestDto jqGridRequestDto, Boolean startsWith) {
		List<JerarquiaDto<RelExpPla>> listaRelExpPla = this.relExpPlaDao.findAllLikeJerarquia(filterRelExpPla,
				jqGridRequestDto);
		Long recordNum = this.relExpPlaDao.findAllLikeCountJerarquia(filterRelExpPla, jqGridRequestDto);
		return new JQGridResponseDto<JerarquiaDto<RelExpPla>>(jqGridRequestDto, recordNum, listaRelExpPla);
	}

	/**
	 * Finder method for siblings rows in the hierarchical the RelExpPla table.
	 *
	 * @param filterRelExpPla
	 *            RelExpPla
	 * @param jqGridRequestDto
	 *            JQGridRequestDto
	 * @return JQGridResponseDto<JerarquiaDto<RelExpPla>>
	 */
	public JQGridResponseDto<JerarquiaDto<RelExpPla>> jerarquiaChildren(RelExpPla filterRelExpPla,
			JQGridRequestDto jqGridRequestDto) {
		JQGridResponseDto<JerarquiaDto<RelExpPla>> jqGridResponseDto = new JQGridResponseDto<JerarquiaDto<RelExpPla>>();
		jqGridResponseDto.addAdditionalParam(JQGridResponseDto.CHILDREN,
				this.relExpPlaDao.findAllChild(filterRelExpPla, jqGridRequestDto));
		return jqGridResponseDto;
	}

}
