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 java.util.UUID;
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.PapPernapasan;
import com.jasamedika.medifirst2000.entities.Pasien;
import com.jasamedika.medifirst2000.service.PapPernapasanService;
import com.jasamedika.medifirst2000.service.PasienService;
import com.jasamedika.medifirst2000.util.DateUtil;
import com.jasamedika.medifirst2000.vo.DataAlatBantuNafasVO;
import com.jasamedika.medifirst2000.vo.PapPernapasanVO;
import com.jasamedika.medifirst2000.vo.PasienVO;
import com.jasamedika.medifirst2000.vo.StatusAdaTidakAdaVO;
import com.jasamedika.medifirst2000.vo.StatusNormalTidakNormalVO;
import com.jasamedika.medifirst2000.vo.StatusRegulerIrregulerVO;

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

	@Autowired
	private PapPernapasanService papPernapasanService;
	@Autowired
	private PasienService pasienService;
	@Autowired
	public PasienDao pasienDao;
	@Autowired
	public PasienConverter pasienConverter;
	//public PasienVO pasienVO;
	
	@Transactional
	@Test
	public void testAddPernapasan() throws ParseException {
		
		PapPernapasanVO papPernapasanVO = populateModelVO();
		assertNotNull(papPernapasanVO);
		Map<String,Object> result = papPernapasanService.addPernapasan(papPernapasanVO);
		assertNotNull(result);
		assertEquals(true,((PapPernapasan) result.get("papPernapasan")).getIsNormal());
		
	}
	
	@Test
	@Transactional
	public void testUpdatePernapasan() throws ParseException {
		PapPernapasanVO papPernapasanVO = populateModelVO();
		Map<String,Object> result = papPernapasanService.addPernapasan(papPernapasanVO);
		assertNotNull(result);
		
		PapPernapasanVO papPernapasanVOUpdate = populateModelVOUpdate();
		papPernapasanVOUpdate.setNoRec(((PapPernapasan) result.get("papPernapasan")).getNoRec());
		assertNotNull(papPernapasanVOUpdate);
		assertEquals(papPernapasanVOUpdate.getNoRec(),((PapPernapasan) result.get("papPernapasan")).getNoRec());
		
		Map<String,Object> resultUpdate = papPernapasanService.addPernapasan(papPernapasanVOUpdate);
		assertEquals(false,((PapPernapasan) resultUpdate.get("papPernapasan")).getIsNormal());
		assertEquals(false,((PapPernapasan) resultUpdate.get("papPernapasan")).getStatusEnabled());
	}
	
	@Test
	public void testFindByNoCmAndTglInput() {
		String noCm = "000000000000005";
		String tglInput = "2016-07-17 04:26:00";
		Map<String,Object> result = papPernapasanService.findByNoCmAndTglInput(noCm, DateUtil.toDate(tglInput));
		assertNotNull(result);
		Boolean dataFound=new Boolean((boolean) result.get("dataFound"));
		assertTrue(dataFound);
	}
	
	private PapPernapasanVO populateModelVO() throws ParseException{
		PapPernapasanVO papPernapasanVO = new PapPernapasanVO();
		Pasien pasien = pasienDao.findOne(547);
		PasienVO pasienVO = pasienConverter.transferModelToVO(pasien,new PasienVO());
		papPernapasanVO.setPasien(pasienVO);
		
		final String uuid = UUID.randomUUID().toString().replaceAll("-", "");
		papPernapasanVO.setNoRec(uuid);
		
		DataAlatBantuNafasVO alatBantuNafasVO = new DataAlatBantuNafasVO();
		alatBantuNafasVO.setId(1);
		papPernapasanVO.setAlatBantuNafas(alatBantuNafasVO);
		
		StatusNormalTidakNormalVO bentukDadaVO = new StatusNormalTidakNormalVO();
		bentukDadaVO.setId(1);
		papPernapasanVO.setBentukDada(bentukDadaVO);
		
		StatusRegulerIrregulerVO iramaVO = new StatusRegulerIrregulerVO();
		iramaVO.setId(1);
		papPernapasanVO.setIrama(iramaVO);
		
		StatusAdaTidakAdaVO retraksiDadaVO = new StatusAdaTidakAdaVO();
		retraksiDadaVO.setId(1);
		papPernapasanVO.setRetraksiDada(retraksiDadaVO);
		
		papPernapasanVO.setTglInput(generateDate());
		
		StatusNormalTidakNormalVO polaNapasVO = new StatusNormalTidakNormalVO();
		polaNapasVO.setId(1);
		papPernapasanVO.setPolaNapas(polaNapasVO);
		
		StatusNormalTidakNormalVO suaraNapasVO = new StatusNormalTidakNormalVO();
		suaraNapasVO.setId(1);
		papPernapasanVO.setSuaraNapas(suaraNapasVO);
		
		StatusAdaTidakAdaVO nafasKupingHidungVO = new StatusAdaTidakAdaVO();
		nafasKupingHidungVO.setId(1);
		papPernapasanVO.setNafasKupingHidung(nafasKupingHidungVO);
		
		StatusAdaTidakAdaVO sianosisVO = new StatusAdaTidakAdaVO();
		sianosisVO.setId(1);
		papPernapasanVO.setSianosis(sianosisVO);
		papPernapasanVO.setKdProfile((short) 897);
		papPernapasanVO.setIsNormal(true);
		papPernapasanVO.setStatusEnabled(true);
		
		return papPernapasanVO;
	}
	
	private PapPernapasanVO populateModelVOUpdate() throws ParseException{
		PapPernapasanVO papPernapasanVO = new PapPernapasanVO();
		Pasien pasien = pasienDao.findOne(547);
		PasienVO pasienVO = pasienConverter.transferModelToVO(pasien,new PasienVO());
		papPernapasanVO.setPasien(pasienVO);
		
		final String uuid = UUID.randomUUID().toString().replaceAll("-", "");
		papPernapasanVO.setNoRec(uuid);
		
		DataAlatBantuNafasVO alatBantuNafasVO = new DataAlatBantuNafasVO();
		alatBantuNafasVO.setId(1);
		papPernapasanVO.setAlatBantuNafas(alatBantuNafasVO);
		
		StatusNormalTidakNormalVO bentukDadaVO = new StatusNormalTidakNormalVO();
		bentukDadaVO.setId(1);
		papPernapasanVO.setBentukDada(bentukDadaVO);
		
		StatusRegulerIrregulerVO iramaVO = new StatusRegulerIrregulerVO();
		iramaVO.setId(1);
		papPernapasanVO.setIrama(iramaVO);
		
		StatusAdaTidakAdaVO retraksiDadaVO = new StatusAdaTidakAdaVO();
		retraksiDadaVO.setId(1);
		papPernapasanVO.setRetraksiDada(retraksiDadaVO);
		
		papPernapasanVO.setTglInput(generateDate());
		
		StatusNormalTidakNormalVO polaNapasVO = new StatusNormalTidakNormalVO();
		polaNapasVO.setId(1);
		papPernapasanVO.setPolaNapas(polaNapasVO);
		
		StatusNormalTidakNormalVO suaraNapasVO = new StatusNormalTidakNormalVO();
		suaraNapasVO.setId(1);
		papPernapasanVO.setSuaraNapas(suaraNapasVO);
		
		StatusAdaTidakAdaVO nafasKupingHidungVO = new StatusAdaTidakAdaVO();
		nafasKupingHidungVO.setId(1);
		papPernapasanVO.setNafasKupingHidung(nafasKupingHidungVO);
		
		StatusAdaTidakAdaVO sianosisVO = new StatusAdaTidakAdaVO();
		sianosisVO.setId(1);
		papPernapasanVO.setSianosis(sianosisVO);
		papPernapasanVO.setKdProfile((short) 890);
		papPernapasanVO.setIsNormal(false);
		papPernapasanVO.setStatusEnabled(false);
		
		return papPernapasanVO;
	}
	
	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;

	}

}
