package com.ejie.ab79.service;

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 org.springframework.web.multipart.MultipartFile;

import com.ejie.ab79.clientesWS.ab61WebService.Ab61Exception;
import com.ejie.ab79.clientesWS.ab61WebService.IdentificadoresPid;
import com.ejie.ab79.clientesWS.ab61WebService.Usuario;
import com.ejie.ab79.comun.Constantes;
import com.ejie.ab79.dao.RelCamDocDao;
import com.ejie.ab79.model.DocumentosSubidos;
import com.ejie.ab79.model.RelCamDoc;
import com.ejie.ab79.model.inspecciones.CampainasInspeccion;
import com.ejie.ab79.webservice.adaptadores.PIFAdaptador;
import com.ejie.ab79.webservice.adaptadores.ab61WebServiceAdaptador;
import com.ejie.x38.dto.TableRequestDto;
import com.ejie.x38.dto.TableResponseDto;

/**
 * RelCamDocServiceImpl  
 * 
 *  
 */

@Service(value = "relCamDocService")
public class RelCamDocServiceImpl implements RelCamDocService {

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

	@Autowired
	private RelCamDocDao relCamDocDao;

	@Autowired
	private PIFAdaptador pifAdapterService;

	@Autowired
	private ab61WebServiceAdaptador ab61WebServiceAdaptadorService;

	@Transactional(rollbackFor = Throwable.class)
	public RelCamDoc add(RelCamDoc relCamDoc) {
		return this.relCamDocDao.add(relCamDoc);
	}

	public RelCamDoc find(RelCamDoc relCamDoc) {
		return this.relCamDocDao.find(relCamDoc);
	}

	@Transactional(rollbackFor = Throwable.class)
	public void remove(RelCamDoc relCamDoc) {
		this.relCamDocDao.remove(relCamDoc);
	}

	public List<RelCamDoc> findAll(RelCamDoc relCamDoc, TableRequestDto tableRequestDto) {
		return this.relCamDocDao.findAll(relCamDoc, tableRequestDto);
	}

	public List<RelCamDoc> findAllLike(RelCamDoc relCamDoc, TableRequestDto tableRequestDto, Boolean startsWith) {
		return this.relCamDocDao.findAllLike(relCamDoc, tableRequestDto, startsWith);
	}

	public TableResponseDto<RelCamDoc> filter(RelCamDoc filterRelCamDoc, TableRequestDto tableRequestDto,
			Boolean startsWith) {
		List<RelCamDoc> listaRelCamDoc = this.relCamDocDao.findAllLike(filterRelCamDoc, tableRequestDto, false);

		Long recordNum = this.relCamDocDao.findAllLikeCount(filterRelCamDoc != null ? filterRelCamDoc : new RelCamDoc(),
				false);

		return new TableResponseDto<RelCamDoc>(tableRequestDto, recordNum, listaRelCamDoc);
	}

	public Object reorderSelection(RelCamDoc filterRelCamDoc, TableRequestDto tableRequestDto, Boolean startsWith) {
		return this.relCamDocDao.reorderSelection(filterRelCamDoc, tableRequestDto, startsWith);
	}

	public void removeByIdDocumentoSubido(RelCamDoc relCamDoc) {
		this.relCamDocDao.removeByIdDocumentoSubido(relCamDoc);
	}

	@Transactional
	@Override
	public void subidaPidInsertar(RelCamDoc relcamdoc, MultipartFile file, Usuario usuario) throws Ab61Exception {

		IdentificadoresPid identificadoresPid = new IdentificadoresPid();
		Integer aportadoExpediente = Constantes.APORTADO_EXPEDIENTE_NO;

		try {
			String pathPif = pifAdapterService.putFileToPID(file.getInputStream(),
					FilenameUtils.getName(file.getOriginalFilename()));

			identificadoresPid = this.ab61WebServiceAdaptadorService.insertarDocumentoPid(pathPif,
					file.getOriginalFilename(), relcamdoc.getDocumentoSubido().getPlantilla().getIdPlantilla(),
					file.getOriginalFilename(), aportadoExpediente, Constantes.CODIGO_APLICACION, usuario.getUsuario());
		} catch (Exception e) {
			RelCamDocServiceImpl.logger.error("insertarDocumentoPid - " + e.getMessage(), e);
			throw new Ab61Exception("Ha ocurrido un problema al guardar el documento en el PID.", null);
		}
		RelCamDocServiceImpl.logger.info("Subimos documento al PID - " + identificadoresPid.getOidPid());

		RelCamDocServiceImpl.logger.info("Insertamos RelActDoc");
		RelCamDoc relCamDoc = new RelCamDoc();
		relCamDoc.setCampainaInspeccion(new CampainasInspeccion(relcamdoc.getCampainaInspeccion().getIdCampaina()));
		relCamDoc.setDocumentoSubido(new DocumentosSubidos(identificadoresPid.getIdDocumentoPid()));
		relCamDoc.setUsuarioRegistro(usuario.getUsuario());
		this.relCamDocDao.add(relCamDoc);
	}

}
