package com.agilex.healthcare.schedulingmanager.datalayer;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;

import java.util.Date;

import javax.annotation.Resource;

import junit.framework.Assert;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.agilex.healthcare.mobilehealthplatform.domain.PatientIdentifier;
import com.agilex.healthcare.schedulingmanager.datalayer.vista.util.AuthenticationUtil;
import com.agilex.healthcare.schedulingmanager.datalayer.vista.util.TestUtil;
import com.agilex.healthcare.schedulingmanager.domain.WaitListEntries;
import com.agilex.healthcare.schedulingmanager.domain.WaitListEntry;
import com.agilex.healthcare.schedulingmanager.mdws.connection.MdwsConfiguration;
import com.agilex.healthcare.utility.DateHelper;

@ContextConfiguration( locations = { "classpath:integrationApplicationContext.xml" } )
@RunWith(SpringJUnit4ClassRunner.class)
public class SchedulingDataLayerMdwsWaitListVistaTest {
	
	private String site = "777";
	private PatientIdentifier fourPatient = new PatientIdentifier( "dfn" + site , "229" ) ;
	private PatientIdentifier bcmaEighteenPatient = new PatientIdentifier( "dfn" + site , "100033" ) ;
	private PatientIdentifier eightPatient = new PatientIdentifier( "dfn" + site , "3" ) ;
	
	/*
	 	NearListPatient XML Data from MDWS:
	 				<dfn>3</dfn>
					<patientname>EIGHT,PATIENT</patientname>
					<patientssn>666000008</patientssn>
					<eligcodeinternal>15</eligcodeinternal>
					<eligcodeexternal>SC LESS THAN 50%</eligcodeexternal>
					<homephone>222-555-8235</homephone>
					<cellphone/>
					<appointmentrequestdate>3140301.134943</appointmentrequestdate>
					<facilityid>500</facilityid>
	 */
	
	private PatientIdentifier patientIdentifierNoResults = new PatientIdentifier( "dfn" + site , "1" ) ;
	
	@Resource
	MdwsConfiguration mdwsSchedulingConfiguration;

	@BeforeClass
	public static void login( ) {

		AuthenticationUtil.loginUser( "777" , "clerk3434" , "clerk4343$" , "1" ) ;
	}

	@AfterClass
	public static void logoff( ) {

		AuthenticationUtil.logoffUser( ) ;
	}

	@Test
	public void getPatientWaitListNear( ) {

		assertNotNull( "mdwsSchedulingConfiguration is null" , mdwsSchedulingConfiguration ) ; 
		assertNotNull( "Cannot use a null patient" , eightPatient ) ; 
		assertNotNull( "Cannot use a null site" , site ) ; 
		
		SchedulingDataLayerMdws dl = new SchedulingDataLayerMdws( mdwsSchedulingConfiguration ) ;
		WaitListEntries entries = dl.getPatientWaitList( eightPatient , site ) ; // taggedNearListByPatientArrays
		assertNotNull( entries ) ;

		WaitListEntry nearWaitListEntry = null ; 
		findNearWaitListEntryLoop : for( WaitListEntry waitListEntry : entries ) { 
			
			if( waitListEntry.getWaitListType( ).equalsIgnoreCase( "NEAR" ) ) { 
				
				nearWaitListEntry = waitListEntry ;  
				break findNearWaitListEntryLoop ; 
			}
		}
		
		assertNotNull( "Patient not on the NEAR List" , nearWaitListEntry ) ; 
		assertNotNull( "Patient dfn is null" , nearWaitListEntry.getDfn( ) ) ; 
		assertNotNull( "Patient name is null" , nearWaitListEntry.getPatientName( ) ) ; 
		assertNotNull( "Patient SSN is null" , nearWaitListEntry.getSsn( ) ) ; 
		
		assertNotNull( "Patient internal eligibility code is null" , nearWaitListEntry.getEligibilityCodeInternal( ) ) ; 
		assertNotNull( "Patient external eligibility code is null" , nearWaitListEntry.getEligibilityCodeExternal( ) ) ; 
		assertNotNull( "Patient home phone is null" , nearWaitListEntry.getHomePhone( ) ) ; 
		assertNotNull( "Patient appointment request date is null" , nearWaitListEntry.getAppointmentRequestDate( ) ) ; 
		assertNotNull( "Patient facility ID is null" , nearWaitListEntry.getFacilityId( ) ) ; 
	
	}
	
	@Test
	public void getPatientWaitListRecall() {
		SchedulingDataLayerMdws dl = new SchedulingDataLayerMdws(mdwsSchedulingConfiguration);
		WaitListEntries entries = dl.getPatientWaitList(bcmaEighteenPatient, site); //taggedRecallListByPatientArrays
		assertNotNull(entries);
		assertEquals(1, entries.size());
		
		Date recallDate = DateHelper.parseVistaDefaultDateTIme("20130526.0000");
		Date recallDate1 = DateHelper.parseVistaDefaultDateTIme("20130527.0000");
		
		WaitListEntry one = entries.get(0);
		
		assertEquals("", one.getAccessionno());
		assertEquals("30", one.getAppointmentLength());
		assertNull(one.getAppointmentRequestDate());
		assertNull(one.getCellPhone());
		assertEquals("195", one.getClinicId());
		assertEquals("CARDIOLOGY", one.getClinicName());
		assertEquals("", one.getComment());
		assertEquals("100033", one.getDfn());
		assertNull(one.getEligibilityCodeExternal());
		assertNull(one.getEligibilityCodeInternal());
		assertNull(one.getFacilityId());
		assertEquals("f", one.getFastNonFasting());
		assertNull(one.getHomePhone());
		assertEquals("BCMA,EIGHTEEN-PATIENT", one.getPatientName());
		assertEquals("1", one.getProvider());
		assertEquals(recallDate, one.getRecallDate());
		assertEquals(recallDate1, one.getRecallDate1());
		assertEquals("666-33-0018", one.getSsn());
		assertEquals("1", one.getUser());
		assertEquals("RECALL", one.getWaitListType());
		assertNullData(one);
	}
	
	private void assertNullData(WaitListEntry one) {
		TestUtil.assertNullDataIdentifier(one.getDataIdentifier());
		assertNull(one.getDateLastUpdated());
		assertNull(one.getDateReminderSent());
		assertNull(one.getDateRemovedFromEwl());
		assertNull(one.getDaysOnList());
		assertNull(one.getDesiredDate());
		
		assertNull(one.getEnrolleeStatus());
		assertNull(one.getEwlIen());
		
		assertNull(one.getId());
		assertNull(one.getLastName());
		assertNull(one.getMessage());
		assertNull(one.getOriginatingDate());
		TestUtil.assertNullPatientIdentifier(one.getPatientIdentifier());

		assertNull(one.getPatientZip());
		assertNull(one.getPriority());

		assertNull(one.getSecondPrintDate());
		assertNull(one.getServiceConnected());
		assertNull(one.getSourceSystem());

		assertNull(one.getStopCode());
		TestUtil.assertNullPatientIdentifier(one.getSurrogateIdentifier());
	}
	
	@Test
	public void getNoPatientWaitList() {
		SchedulingDataLayerMdws dl = new SchedulingDataLayerMdws(mdwsSchedulingConfiguration);
		WaitListEntries entries = dl.getPatientWaitList(patientIdentifierNoResults, site);
		assertNotNull(entries);
		Assert.assertEquals(0, entries.size());
	}
}
