package com.ejie.ad74a.service.test.interoperabilidad;

import java.util.List;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.ejie.ad74a.dao.ExpedientesDao;
import com.ejie.ad74a.exceptions.ManagerErrors;
import com.ejie.ad74a.model.DocumentoPapel0;
import com.ejie.ad74a.model.Expedientes;
import com.ejie.ad74a.model.MaestroTiposDoc;
import com.ejie.ad74a.service.DocumentoPapel0Service;
import com.ejie.ad74a.service.MaestroTiposDocService;
import com.ejie.ad74a.service.documentos.DocumentosService;
import com.ejie.ad74a.tareas.service.TareasUtils;
import com.ejie.ad74a.util.comun.UtilSeguridad;
import com.ejie.ad74a.ws.client.platea.expedienteElectronico.TBoolean;
import com.ejie.ad74a.ws.client.platea.expedienteElectronico.TDocument;
import com.ejie.ad74a.ws.client.platea.expedienteElectronico.TGetTelActionDocumentList;
import com.ejie.ad74a.ws.client.platea.expedienteElectronico.TPersonIDName;
import com.ejie.ad74a.ws.client.platea.expedienteElectronico.TTelActionDocument;
import com.ejie.ad74a.ws.client.platea.service.PlateaService;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:test/application-context.xml")
public class TestDocumentosService {

	@Autowired
	private DocumentosService documentosService;

	@Autowired
	private PlateaService plateaService;

	@Autowired
	private ExpedientesDao expedienteDao;

	@Autowired
	private MaestroTiposDocService maestroTiposDocService;

	@Autowired
	private DocumentoPapel0Service documentoPapel0Service;

	@Test
	@Transactional(propagation = Propagation.NOT_SUPPORTED)
	@Rollback(false)
	public void testGetDocumentsTelAction() {
		try {
			String procedureID = "";
			String iopTraceID = "";
			boolean encontrado = false;
			String authorizationID = "R02T28E4175B76280D4A4750F92F737317423D0EB281";

			TGetTelActionDocumentList folderDocuList = plateaService
					.getDocumentsTelAction("R02T4EC11FF5C531BD02165888F3A41EF24EBC611CD9", "0087002");
			if (null != folderDocuList && null != folderDocuList.getTelActionList()
					&& null != folderDocuList.getTelActionList().getTelAction()) {
				for (TTelActionDocument folderAutho : folderDocuList.getTelActionList().getTelAction()) {
					if (encontrado)
						break;
					if (null != folderAutho.getAuthorizationList()
							&& null != folderAutho.getAuthorizationList().getDocument()) {

						for (TDocument document : folderAutho.getAuthorizationList().getDocument()) {
							if (document.getAuthorization().getAuthorizationID().equalsIgnoreCase(authorizationID)) {
								iopTraceID = document.getAuthorization().getIopTraceID();
								encontrado = true;
								break;
							}
						}
					}
				}
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// assertTrue("No se lanzado la peticion correctamente", respuesta != null);
	}

	@Test
	@Transactional(propagation = Propagation.NOT_SUPPORTED)
	@Rollback(false)
	public void testIntermediacion() {
		Expedientes exp = new Expedientes();
		exp.setIdExpediente00(53L);
		exp = this.expedienteDao.find(exp);
		String nif = "S4833001C";
		try {
			this.documentosService.authorizationResultConsolidation(exp, "0087002", nif);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// assertTrue("No se lanzado la peticion correctamente", respuesta != null);
	}

	@Test
	@Transactional(propagation = Propagation.NOT_SUPPORTED)
	@Rollback(false)
	public void testRetramitateDocumentoPapel0() {
		try {
			MaestroTiposDoc tipoDoc = new MaestroTiposDoc();
			DocumentoPapel0 documentoEntrada = new DocumentoPapel0();

			String oidDokusi = "09f4240181045099";
			Long idTipoDocTipificar = 18L; // Documento de urgencia
			String procedureID = "00870"; // Solicitud de alta

			tipoDoc.setRoleid36("ejgv_d_acuerdo.ejgv_d_acuerdo_tramurge.X87_Documento_Acreditativo_Urgencia");
			List<MaestroTiposDoc> listaTiposDoc = this.maestroTiposDocService.findAll(tipoDoc, null);

//			String senderID = UtilSeguridad.getUsuarioConectado().getNif();
//			String senderName = UtilSeguridad.getUsuarioConectado().getFullName();

			TPersonIDName person = new TPersonIDName();
			com.ejie.ad74a.ws.client.platea.tareasTramitacion.TPersonIDName sender = TareasUtils
					.getSenderIdAutomatico();
			person.setID(sender.getID());
			person.setName(sender.getName());

			TBoolean resultado = this.plateaService.documentChangeRoleID(oidDokusi, listaTiposDoc.get(0).getRoleid36(),
					person, procedureID);

			if (TBoolean.TRUE.equals(resultado)) {
				documentoEntrada.setOidDocumento13(oidDokusi);
				documentoEntrada.setIdTipoDoc13(idTipoDocTipificar);
				this.documentoPapel0Service.updateRoleDocument(documentoEntrada, null, null);

			} else {
				ManagerErrors.manageError("error.tipificar.generico");
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		// assertTrue("No se lanzado la peticion correctamente", respuesta != null);
	}

}
