package com.jasamedika.medifirst2000.service.test;

import static org.junit.Assert.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;
import com.jasamedika.medifirst2000.converter.PasienConverter;
import com.jasamedika.medifirst2000.dao.PasienDao;
import com.jasamedika.medifirst2000.entities.PapAnamesis;
import com.jasamedika.medifirst2000.entities.Pasien;
import com.jasamedika.medifirst2000.service.PapAnamesisService;
import com.jasamedika.medifirst2000.util.DateUtil;
import com.jasamedika.medifirst2000.vo.PapAnamesisVO;
import com.jasamedika.medifirst2000.vo.PasienVO;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath*:com/jasamedika/**/applicationContext.xml")
public class PapAnamesisServiceImplTest {
	@Autowired
	private PapAnamesisService papAnamesisService;
	@Autowired
	private PasienDao pasienDao;
	@Autowired
	private PasienConverter pasienConverter;
	
	@Test
	@Transactional
	public void testAddAnamesis() throws ParseException {
		PapAnamesisVO papAnamesisVO = populateModelVO();
		assertNotNull(papAnamesisVO);
		Map<String,Object> result = papAnamesisService.addAnamesis(papAnamesisVO);
		assertNotNull(result);
		assertEquals("Setyo Wibudi",((PapAnamesis) result.get("papAnamesis")).getPasien().getNamaPasien());
		
	}
	
	@Test
	@Transactional
	public void testUpdateAnamesis() throws ParseException {
		PapAnamesisVO papAnamesisVO = populateModelVO();
		Map<String,Object> result = papAnamesisService.addAnamesis(papAnamesisVO);
		assertNotNull(result);
		
		PapAnamesisVO papAnamesisVOUpdate = populateModelVOUpdate();
		papAnamesisVOUpdate.setNoRec(((PapAnamesis) result.get("papAnamesis")).getNoRec());
		assertNotNull(papAnamesisVOUpdate);
		assertEquals(papAnamesisVOUpdate.getNoRec(),((PapAnamesis) result.get("papAnamesis")).getNoRec());
		
		Map<String,Object> resultUpdate = papAnamesisService.addAnamesis(papAnamesisVOUpdate);
		assertEquals(false,((PapAnamesis) resultUpdate.get("papAnamesis")).getStatusEnabled());
	}
	
	
	@Test
	public void testFindByNoCmAndTglInput() {
		String noCm = "000001";
		String tglInput = "2016-07-17 02:12:03";
		Map<String,Object> result = papAnamesisService.findByNoCmAndTglInput(noCm, DateUtil.toDate(tglInput));
		assertNotNull(result);
		Boolean dataFound=new Boolean((boolean) result.get("dataFound"));
		assertTrue(dataFound);
	}
	
	@Test
	public void testFindByNoCmAndTglRegistrasi() {
		String noCm = "000001";
		String tglRegistrasi = "2016-07-17 02:12:03";
		//Map<String,Object> result = papAnamesisService.findByNoCmAndTglInput(noCm, DateUtil.toDate(tglInput));
		Map<String,Object> result = papAnamesisService.findRegistrasiPelayananPasienByNoCmAndTglRegistrasi(noCm,(short) 0, DateUtil.toDate(tglRegistrasi));
		assertNotNull(result);
		Boolean dataFound=new Boolean((boolean) result.get("dataFound"));
		assertTrue(dataFound);
	}
	
	@Test
	public void testFindPasienByNoCm() {
		String noCm = "000000000000002";
		Map<String,Object> result = papAnamesisService.findPasienByNoCm(noCm);
		assertNotNull(result);
		Boolean dataFound=new Boolean((boolean) result.get("dataFound"));
		assertTrue(dataFound);
		
	}
	
	
	private PapAnamesisVO populateModelVO() throws ParseException{
		PapAnamesisVO papAnamesisVO = new PapAnamesisVO();
		Pasien pasien = pasienDao.findOne(547);
		PasienVO pasienVO = pasienConverter.transferModelToVO(pasien,new PasienVO());
		papAnamesisVO.setPasien(pasienVO);
		papAnamesisVO.setTglInput(generateDate());
		papAnamesisVO.setStatusEnabled(true);
		
		return papAnamesisVO;
	}
	
	private PapAnamesisVO populateModelVOUpdate() throws ParseException{
		PapAnamesisVO papAnamesisVO = new PapAnamesisVO();
		Pasien pasien = pasienDao.findOne(547);
		PasienVO pasienVO = pasienConverter.transferModelToVO(pasien,new PasienVO());
		papAnamesisVO.setTglInput(generateDate());
		papAnamesisVO.setPasien(pasienVO);
		papAnamesisVO.setStatusEnabled(false);
		
		return papAnamesisVO;
	}
	
	@Test
	public void testPopulate() throws ParseException{
		/*Cek Method Populate*/
		PapAnamesisVO papAnamesisVO = populateModelVO();
		assertNotNull(papAnamesisVO);
		
		
		/*Cek Dao*/
		Pasien pasien = pasienDao.findOne(544);
		assertNotNull(pasien);
		assertEquals("Reynaldi Ramadhani Pratama",pasien.getNamaPasien());
		
		/*Cek Converter*/
		PasienVO pasienVO = pasienConverter.transferModelToVO(pasien,new PasienVO());
		assertNotNull(pasienVO);
		assertEquals("Reynaldi Ramadhani Pratama",pasienVO.getNamaPasien());
		
	}
	
	private Date generateDate() throws ParseException{
		SimpleDateFormat formatter = new SimpleDateFormat("EEEE, MMM dd, yyyy HH:mm:ss a");
		String dateInString = "Friday, Jun 7, 2013 11:40:39 PM";		

		Date date = formatter.parse(dateInString);
		return date;

	}


}
