package com.agilex.healthcare.schedulingmanager.datalayer;

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

import java.util.Date;

import javax.annotation.Resource;

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.filter.datefilter.DateFilter;
import com.agilex.healthcare.mobilehealthplatform.domain.filter.datefilter.DateFilterFactory;
import com.agilex.healthcare.schedulingmanager.datalayer.vista.util.AuthenticationUtil;
import com.agilex.healthcare.schedulingmanager.domain.SMConsult;
import com.agilex.healthcare.schedulingmanager.domain.SMConsultDetail;
import com.agilex.healthcare.schedulingmanager.domain.SMConsultService;
import com.agilex.healthcare.schedulingmanager.domain.SMConsultServices;
import com.agilex.healthcare.schedulingmanager.domain.SMConsults;
import com.agilex.healthcare.schedulingmanager.mdws.connection.MdwsConfiguration;
import com.agilex.healthcare.utility.DateHelper;

@ContextConfiguration(locations = {"classpath:integrationApplicationContext.xml"})
@RunWith(SpringJUnit4ClassRunner.class)
public class SchedulingDataLayerMdwsConsultsVistaTest {
	
	@Resource
	MdwsConfiguration mdwsSchedulingConfiguration;

	@BeforeClass
	public static void login() {
		AuthenticationUtil.loginUser("777", "clerk3434", "clerk4343$", "1");
	}
	@AfterClass
	public static void logoff() {
		AuthenticationUtil.logoffUser();
	}

	@Test
	public void getConsultsBySpecialties() {
		SchedulingDataLayerMdws dl = new SchedulingDataLayerMdws(mdwsSchedulingConfiguration);
		DateFilter dateFilter = DateFilterFactory.createFilterFromDate(DateHelper.parseDate("01/01/1997"), DateHelper.parseDate("04/01/2014"));
		SMConsults consults = dl.getConsultsBySpecialties("5", "1", dateFilter, "777");
		assertNotNull(consults);
		assertEquals(38, consults.size());
		
		SMConsult one = consults.get(0);
		Date requestDate = DateHelper.parseVistaDefaultDateTIme("20131230.123452");
		assertEquals("639", one.getConsultIen());
		assertNotNull(one.getDaysOnList());
		assertEquals("229", one.getPatientDfn());
		assertEquals("FOUR,PATIENT", one.getPatientName());
		assertEquals(requestDate, one.getRequestDate());
		assertEquals("1", one.getSendingProviderDuz());
		assertEquals("PROGRAMMER,ONE", one.getSendingProviderName());
		assertEquals("5", one.getServiceIen());
		assertEquals("GASTROENTEROLOGY", one.getServiceName());
		assertEquals("PENDING", one.getStatus());
		assertNull(one.getText());
	}
	
	@Test
	public void getConsultServices() {
		SchedulingDataLayerMdws dl = new SchedulingDataLayerMdws(mdwsSchedulingConfiguration);
		SMConsultServices services = dl.getConsultServices("777");
		assertNotNull(services);
		assertEquals(65, services.size());
		
		SMConsultService one = services.get(0);
		assertEquals("3487", one.getOrderableItem());
		assertEquals("56", one.getParent());
		assertEquals("54", one.getServiceIen());
		assertEquals("ANTICOAG CLINIC DR. BLACK", one.getServiceName());
		assertEquals("", one.getServiceUsage());
		
		for (int i=0; i<services.size(); i++) {
			String msg = String.format("Expecting isHasChildren to be false and it wasn't for number %d", i);
			assertFalse(msg, one.isHasChildren());
		}
	}

	@Test
	public void getDetailsForConsult() {
		SchedulingDataLayerMdws dl = new SchedulingDataLayerMdws(mdwsSchedulingConfiguration);
		SMConsultDetail detail = dl.getDetailsForConsult("78", "777");
		assertNotNull(detail);
		assertEquals("78", detail.getConsultIen());
		assertNotNull(detail.getText());
		assertTrue(detail.getText().length() > 0);
		assertEquals("Current PC Team:       RED\nCurrent Pat. Status:   Outpatient\nPrimary Eligibility:   SC LESS THAN 50%(VERIFIED)\nPatient Type:          SC VETERAN\nOEF/OIF:               NO\n\nService Connection/Rated Disabilities\nSC Percent:            10%\nRated Disabilities:    NONE STATED\n\nOrder Information\nTo Service:            HEMATOLOGY NEW NAME\nAttention:             PROGRAMMER,TWENTY\nFrom Service:          7A SURG\nRequesting Provider:   \nService is to be rendered on an INPATIENT basis\nPlace:                 Consultant's choice\nUrgency:               Routine\nEarliest Appr. Date:   \nOrderable Item:        \nConsult:               Consult Request\nProvisional Diagnosis: test\nReason For Request:\nPatient is anemica and would like a consult.\n \nInter-facility Information\nThis is not an inter-facility consult request.\n\nStatus:                DISCONTINUED\nLast Action:           DISCONTINUED\n\nFacility\n Activity                Date/Time/Zone      Responsible Person  Entered By\n-------------------------------------------------------------------------------\n ENTERED IN CPRS         12/06/95 11:30      PROVIDER,ONEHUNDR   WARDCLERK,FIFTYTH\n CPRS RELEASED ORDER     12/06/95 11:31      PROVIDER,ONEHUNDR   WARDCLERK,FIFTYTH\n PRINTED TO 562          12/06/95 11:31      PROVIDER,ONEHUNDR   WARDCLERK,FIFTYTH\n DISCONTINUED            12/31/07 12:58      LABTECH,FORTYEIGH   LABTECH,FORTYEIGH\nPatient failed to show for appointment.  Discontued per policy.\n\n\nNote: TIME ZONE is local if not indicated\n\nNo local TIU results or Medicine results available for this consult\n==================================== END =====================================\n", detail.getText());
	}
}
