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.PapRehabilitasi;
import com.jasamedika.medifirst2000.entities.Pasien;
import com.jasamedika.medifirst2000.service.PapRehabilitasiService;
import com.jasamedika.medifirst2000.util.DateUtil;
import com.jasamedika.medifirst2000.vo.PapRehabilitasiVO;
import com.jasamedika.medifirst2000.vo.PasienVO;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath*:com/jasamedika/**/applicationContext.xml")

public class PapRehabilitasiServiceImplTest {

	@Autowired
	private PapRehabilitasiService papRehabilitasiService;
	@Autowired
	private PasienDao pasienDao;
	@Autowired
	private PasienConverter pasienConverter;
	
	@Test
	@Transactional
	public void testAddRehabilitasi() throws ParseException {
		PapRehabilitasiVO papRehabilitasiVO = populateModelVO();
		assertNotNull(papRehabilitasiVO);
		Map<String,Object> result = papRehabilitasiService.addRehabilitasi(papRehabilitasiVO);
		assertNotNull(result);
		assertEquals("Setyo Wibudi",((PapRehabilitasi) result.get("papRehabilitasi")).getPasien().getNamaPasien());
		
	}
	
	@Test
	@Transactional
	public void testUpdateRehabilitasi() throws ParseException {
		PapRehabilitasiVO papRehabilitasiVO = populateModelVO();
		Map<String,Object> result = papRehabilitasiService.addRehabilitasi(papRehabilitasiVO);
		assertNotNull(result);
		
		PapRehabilitasiVO papRehabilitasiVOUpdate = populateModelVOUpdate();
		papRehabilitasiVOUpdate.setNoRec(((PapRehabilitasi) result.get("papRehabilitasi")).getNoRec());
		assertNotNull(papRehabilitasiVOUpdate);
		assertEquals(papRehabilitasiVOUpdate.getNoRec(),((PapRehabilitasi) result.get("papRehabilitasi")).getNoRec());
		
		Map<String,Object> resultUpdate = papRehabilitasiService.addRehabilitasi(papRehabilitasiVOUpdate);
		assertEquals(false,((PapRehabilitasi) resultUpdate.get("papRehabilitasi")).getStatusEnabled());
	}
	
	
	@Test
	public void testFindByNoCmAndTglInput() {
		String noCm = "000003";
		String tglInput = "2013-06-07 11:40:39";
		Map<String,Object> result = papRehabilitasiService.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 = papRehabilitasiService.findByNoCmAndTglInput(noCm, DateUtil.toDate(tglInput));
		Map<String,Object> result = papRehabilitasiService.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 = papRehabilitasiService.findPasienByNoCm(noCm);
		assertNotNull(result);
		Boolean dataFound=new Boolean((boolean) result.get("dataFound"));
		assertTrue(dataFound);
		
	}
	
	
	private PapRehabilitasiVO populateModelVO() throws ParseException{
		PapRehabilitasiVO papRehabilitasiVO = new PapRehabilitasiVO();
		Pasien pasien = pasienDao.findOne(547);
		PasienVO pasienVO = pasienConverter.transferModelToVO(pasien,new PasienVO());
		papRehabilitasiVO.setPasien(pasienVO);
		papRehabilitasiVO.setTglInput(generateDate());
		papRehabilitasiVO.setStatusEnabled(true);
		
		return papRehabilitasiVO;
	}
	
	private PapRehabilitasiVO populateModelVOUpdate() throws ParseException{
		PapRehabilitasiVO papRehabilitasiVO = new PapRehabilitasiVO();
		Pasien pasien = pasienDao.findOne(547);
		PasienVO pasienVO = pasienConverter.transferModelToVO(pasien,new PasienVO());
		papRehabilitasiVO.setTglInput(generateDate());
		papRehabilitasiVO.setPasien(pasienVO);
		papRehabilitasiVO.setStatusEnabled(false);
		
		return papRehabilitasiVO;
	}
	
	@Test
	public void testPopulate() throws ParseException{
		/*Cek Method Populate*/
		PapRehabilitasiVO papRehabilitasiVO = populateModelVO();
		assertNotNull(papRehabilitasiVO);
		
		
		/*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;

	}

}
