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.HashMap;
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.converter.RegistrasiPelayananPasienConverter;
import com.jasamedika.medifirst2000.dao.PasienDao;
import com.jasamedika.medifirst2000.dao.RegistrasiPelayananPasienDao;
import com.jasamedika.medifirst2000.entities.PapGenatalia;
import com.jasamedika.medifirst2000.entities.Pasien;
import com.jasamedika.medifirst2000.entities.RegistrasiPelayananPasien;
import com.jasamedika.medifirst2000.service.PapGenataliaService;
import com.jasamedika.medifirst2000.util.DateUtil;
import com.jasamedika.medifirst2000.vo.PapGenataliaVO;
import com.jasamedika.medifirst2000.vo.PasienVO;
import com.jasamedika.medifirst2000.vo.RegistrasiPelayananPasienVO;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath*:com/jasamedika/**/applicationContext.xml")
public class PapGenataliaServiceImplTest {
	
	@Autowired
	private PapGenataliaService papGenataliaService;
	
	@Autowired
	private PasienDao pasienDao;
	@Autowired
	private PasienConverter pasienConverter;
	
//	@Autowired
//	private RegistrasiPelayananPasienDao registrasiPelayananPasienDao;
//	@Autowired RegistrasiPelayananPasienConverter registrasiPelayananPasienConverter;
	
	@Test
	@Transactional
	public void testAddGenatalia() throws ParseException {
		PapGenataliaVO papGenataliaVO = populateModelVO();
		Map<String,Object> result = papGenataliaService.addGenatalia(papGenataliaVO);
		assertNotNull(result);
		assertEquals("Test Status Genatalia",((PapGenatalia) result.get("papGenatalia")).getStatus());
		assertNotEquals("Test Status Genatalia Error",((PapGenatalia) result.get("papGenatalia")).getStatus());
		assertEquals("Test Keterangan Genatalia",((PapGenatalia) result.get("papGenatalia")).getKeterangan());
	}
	
	@Test
	@Transactional
	public void testUpdateGenatalia() throws ParseException {
		PapGenataliaVO papGenataliaVO = populateModelVO();
		Map<String,Object> result = papGenataliaService.addGenatalia(papGenataliaVO);
		assertNotNull(result);
		
		PapGenataliaVO papGenataliaVOUpdate = populateModelVOUpdate();
		papGenataliaVOUpdate.setNoRec(((PapGenatalia) result.get("papGenatalia")).getNoRec());
		assertNotNull(papGenataliaVOUpdate);
		assertEquals(papGenataliaVOUpdate.getNoRec(),((PapGenatalia) result.get("papGenatalia")).getNoRec());
		
		Map<String,Object> resultUpdate = papGenataliaService.addGenatalia(papGenataliaVOUpdate);
		assertEquals("Test Status Genatalia Update",((PapGenatalia) resultUpdate.get("papGenatalia")).getStatus());
		assertNotEquals("Test Status Genatalia Error",((PapGenatalia) resultUpdate.get("papGenatalia")).getStatus());
		assertEquals("Test Keterangan Genatalia Update",((PapGenatalia) resultUpdate.get("papGenatalia")).getKeterangan());
	}
	
	@Test
	@Transactional
	public void testPopulate() throws ParseException{
		/*Cek Method Populate*/
		PapGenataliaVO papGenataliaVO = populateModelVO();
		assertNotNull(papGenataliaVO);
		
		/*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());
	}

	@Test
	@Transactional
	public void testFindByNoCmAndTglInput() {
		String noCm = "000001";
		String tglInput = "2016-07-17 02:12:03";
		Map<String,Object> result = papGenataliaService.findByNoCmAndTglInput(noCm, DateUtil.toDate(tglInput));
		assertNotNull(result);
		Boolean dataFound=new Boolean((boolean) result.get("dataFound"));
		assertTrue(dataFound);
	}
	
	
	private PapGenataliaVO populateModelVO() throws ParseException{
		PapGenataliaVO papGenataliaVO = new PapGenataliaVO();
		//RegistrasiPelayananPasien registrasiPelayananPasien = registrasiPelayananPasienDao.findOne("2c9090ad56014d2c0156014f2f020003");
		//RegistrasiPelayananPasienVO registrasiPelayananPasienVO = registrasiPelayananPasienConverter.transferModelToVO(registrasiPelayananPasien, new RegistrasiPelayananPasienVO());
		Pasien pasien = pasienDao.findOne(544);
		PasienVO pasienVO = pasienConverter.transferModelToVO(pasien,new PasienVO());
		
		papGenataliaVO.setStatus("Test Status Genatalia");
		papGenataliaVO.setKeterangan("Test Keterangan Genatalia");
		papGenataliaVO.setTglInput(generateDate());
//		papGenataliaVO.setRegistrasiPelayananPasien(registrasiPelayananPasienVO);
		papGenataliaVO.setPasien(pasienVO);
		
		return papGenataliaVO;
	}
	
	private PapGenataliaVO populateModelVOUpdate() throws ParseException{
		PapGenataliaVO papGenataliaVO = new PapGenataliaVO();
		Pasien pasien = pasienDao.findOne(544);
		PasienVO pasienVO = pasienConverter.transferModelToVO(pasien,new PasienVO());
		
		papGenataliaVO.setStatus("Test Status Genatalia Update");
		papGenataliaVO.setKeterangan("Test Keterangan Genatalia Update");
		papGenataliaVO.setTglInput(generateDate());
		papGenataliaVO.setPasien(pasienVO);
		
		return papGenataliaVO;
	}
	
	private Date generateDate() throws ParseException{
		SimpleDateFormat formatter = new SimpleDateFormat("EEEE, MMM dd, yyyy HH:mm:ss a");
		String dateInString = "Friday, Jun 7, 2013 12:10:56 PM";		

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