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

import java.util.ArrayList;

import org.junit.Assert;
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.ws.client.platea.expedienteElectronico.TAccount;
import com.ejie.ad74a.ws.client.platea.expedienteElectronico.TFolderSearchType;
import com.ejie.ad74a.ws.client.platea.service.PlateaService;

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

	@Autowired
	private PlateaService plateaService;

	@Test
	@Transactional(propagation = Propagation.SUPPORTS)
	@Rollback(false)
	public void testSearchFolders() {

		// TODO HAY QUE VER SI VIENE REPRESENTANTE
		// con notificacion postal - R02T6AD69D58D6B4C3F1A0E587BC5CDD19DA260246D0
		String folderID = "R02T5A17C851F2226223220F3E44A0FCBD0633B65FDC";
		ArrayList<String> folderIDList = new ArrayList<String>();
		folderIDList.add(folderID);
		com.ejie.ad74a.ws.client.platea.expedienteElectronico.TAccount account = new com.ejie.ad74a.ws.client.platea.expedienteElectronico.TAccount();
		try {
			com.ejie.ad74a.ws.client.platea.expedienteElectronico.TFolderList listFolders = this.plateaService
					.searchFolders(folderIDList, TFolderSearchType.PARAM_NOTIFICATIONS);

			for (com.ejie.ad74a.ws.client.platea.expedienteElectronico.TFolder folder : listFolders.getFolder()) {
				if (null != folder.getParticipantRRIList()
						&& null != folder.getParticipantRRIList().getParticipantList()) {

					for (com.ejie.ad74a.ws.client.platea.expedienteElectronico.TParticipantRRI participante : folder
							.getParticipantRRIList().getParticipantList()) {

						if (null != participante.getRepresentative()) {
							// si vienre representante, cogemos lo del representante
							account = participante.getRepresentative().getAccount();
							break;
						} else {
							if (null != participante && null != participante.getRole() && participante.getRole().equals(
									com.ejie.ad74a.ws.client.platea.expedienteElectronico.TParticipantType.SOLICITANTE)
									&& null != participante.getInterestedList()
									&& null != participante.getInterestedList().getInterested()) {

								for (com.ejie.ad74a.ws.client.platea.expedienteElectronico.TInterested person : participante
										.getInterestedList().getInterested()) {
									account = person.getAccount();
									break;
								}
							}
						}
					}
				}
			}

			if (null != account) {
				System.out.println("CUENTA: " + account);
			}

			System.out.println("Procedimientos: " + listFolders);
		} catch (Throwable e) {
			e.printStackTrace();
			Assert.fail("Error al recuperar el procedimiento");
		}
		// Assert.assertTrue("Se ha encontrado un registro que no existe", true);
	}

}
