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 javax.validation.constraints.AssertTrue;

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.RuanganConverter;
import com.jasamedika.medifirst2000.dao.PasienDao;
import com.jasamedika.medifirst2000.dao.RuanganDao;
import com.jasamedika.medifirst2000.entities.PapBedah;
import com.jasamedika.medifirst2000.entities.Pasien;
import com.jasamedika.medifirst2000.entities.Ruangan;
import com.jasamedika.medifirst2000.service.PapBedahService;
import com.jasamedika.medifirst2000.util.DateUtil;
import com.jasamedika.medifirst2000.vo.PapBedahVO;
import com.jasamedika.medifirst2000.vo.PasienVO;
import com.jasamedika.medifirst2000.vo.RuanganVO;

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

	@Autowired
	private PapBedahService papBedahService;
	@Autowired
	private PasienDao pasienDao;
	@Autowired
	private RuanganDao ruanganDao;
	@Autowired
	private PasienConverter pasienConverter;
	@Autowired
	private RuanganConverter ruanganConverter;
	
	@Test
	@Transactional
	public void testAddBedah() throws ParseException {
		PapBedahVO papBedahVO = populateModelVO();
		assertNotNull(papBedahVO);
		Map<String,Object> result = papBedahService.addBedah(papBedahVO);
		//assertEquals("10",((PapBedah) result.get("papBedah")).getStrukOrder().getRuangan().getId());
		assertEquals("Setyo Wibudi",((PapBedah) result.get("papBedah")).getPasien().getNamaPasien());
		
	}
	
	@Test
	@Transactional
	public void testRuangan() throws ParseException {
		PapBedahVO papBedahVO = populateModelVO();
		assertNotNull(papBedahVO.getRuangan());
		assertEquals("10", String.valueOf(papBedahVO.getRuangan().getId()));
	}
	
	@Test
	@Transactional
	public void testUpdateBedah() throws ParseException {
		PapBedahVO papBedahVO = populateModelVO();
		Map<String,Object> result = papBedahService.addBedah(papBedahVO);
		assertNotNull(result);
		
		PapBedahVO papBedahVOUpdate = populateModelVOUpdate();
		papBedahVOUpdate.setNoRec(((PapBedah) result.get("papBedah")).getNoRec());
		assertNotNull(papBedahVOUpdate);
		assertEquals(papBedahVOUpdate.getNoRec(),((PapBedah) result.get("papBedah")).getNoRec());
		
		Map<String,Object> resultUpdate = papBedahService.addBedah(papBedahVO);
		assertEquals(false,((PapBedah) resultUpdate.get("papBedah")).getStatusEnabled());
	}
	
	
	@Test
	public void testFindByNoCmAndTglInput() {
		String noCm = "000000000000002";
		String tglInput = "2016-06-19 14:45:36";
		Map<String,Object> result = papBedahService.findByNoCmAndTglInput(noCm, DateUtil.toDate(tglInput));
		assertNotNull(result);
		Boolean dataFound=new Boolean((boolean) result.get("dataFound"));
		assertTrue(dataFound);
	}
	
	@Test
	public void testFindByNoCmAndTglRegistrasi() {
		String noCm = "000000000000002";
		String tglRegistrasi = "2016-07-17 02:12:03";
		//Map<String,Object> result = papBedahService.findByNoCmAndTglInput(noCm, DateUtil.toDate(tglInput));
		Map<String,Object> result = papBedahService.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 = papBedahService.findPasienByNoCm(noCm);
		assertNotNull(result);
		Boolean dataFound=new Boolean((boolean) result.get("dataFound"));
		assertTrue(dataFound);
		
	}
	
	
	private PapBedahVO populateModelVO() throws ParseException{
		PapBedahVO papBedahVO = new PapBedahVO();
		Pasien pasien = pasienDao.findOne(547);
		PasienVO pasienVO = pasienConverter.transferModelToVO(pasien,new PasienVO());
		papBedahVO.setPasien(pasienVO);
		
		Ruangan ruangan = ruanganDao.findOne(10);
		RuanganVO ruanganVO = ruanganConverter.transferModelToVO(ruangan,new RuanganVO());
		papBedahVO.setRuangan(ruanganVO);
		papBedahVO.setRuanganTujuan(ruanganVO);
		papBedahVO.setTglInput(generateDate());
		papBedahVO.setStatusEnabled(true);
		
		return papBedahVO;
	}
	
	private PapBedahVO populateModelVOUpdate() throws ParseException{
		PapBedahVO papBedahVO = new PapBedahVO();
		Pasien pasien = pasienDao.findOne(547);
		PasienVO pasienVO = pasienConverter.transferModelToVO(pasien,new PasienVO());
		
		Ruangan ruangan = ruanganDao.findOne(10);
		RuanganVO ruanganVO = ruanganConverter.transferModelToVO(ruangan,new RuanganVO());
		
		papBedahVO.setRuangan(ruanganVO);
		papBedahVO.setRuanganTujuan(ruanganVO);
		papBedahVO.setTglInput(generateDate());
		papBedahVO.setPasien(pasienVO);
		papBedahVO.setStatusEnabled(false);
		
		return papBedahVO;
	}
	
	@Test
	public void testPopulate() throws ParseException{
		/*Cek Method Populate*/
		PapBedahVO papBedahVO = populateModelVO();
		assertNotNull(papBedahVO);
		
		
		/*Cek Dao*/
		Pasien pasien = pasienDao.findOne(544);
		assertNotNull(pasien);
		assertEquals("Reynaldi Ramadhani Pratama",pasien.getNamaPasien());
		Ruangan ruangan = ruanganDao.findOne(10);
		assertNotNull(ruangan);
		assertEquals("Klinik Psikologi",ruangan.getNamaRuangan());
		
		/*Cek Converter*/
		PasienVO pasienVO = pasienConverter.transferModelToVO(pasien,new PasienVO());
		assertNotNull(pasienVO);
		assertEquals("Reynaldi Ramadhani Pratama",pasienVO.getNamaPasien());
		
		RuanganVO ruanganVO = ruanganConverter.transferModelToVO(ruangan,new RuanganVO());
		assertNotNull(ruanganVO);
		//assertEquals("Klinik Psikologi",pasienVO.getNamaExternal());
		
	}
	
	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;

	}

}
