package com.jasamedika.medifirst2000.service.impl;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.collections.IteratorUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.jpa.JpaSystemException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.jasamedika.medifirst2000.converter.AlamatConverter;
import com.jasamedika.medifirst2000.dao.AlamatDao;
import com.jasamedika.medifirst2000.entities.Alamat;
import com.jasamedika.medifirst2000.entities.JenisAlamat;
import com.jasamedika.medifirst2000.entities.Pasien;
import com.jasamedika.medifirst2000.entities.PurchasedTest;
import com.jasamedika.medifirst2000.exception.ServiceVOException;
import com.jasamedika.medifirst2000.service.AlamatService;
import com.jasamedika.medifirst2000.vo.ModelVO;
import com.jasamedika.medifirst2000.vo.NegaraVO;
import com.jasamedika.medifirst2000.vo.PropinsiVO;
import com.jasamedika.medifirst2000.vo.ModelVO;
import com.jasamedika.medifirst2000.vo.AlamatVO;
import com.jasamedika.medifirst2000.vo.JenisAlamatVO;

/**
 * Implement class for AlamatService
 * 
 * @author Roberto
 * @param <T>
 */
@Service("AlamatService")
public class AlamatServiceImpl<T> implements AlamatService<T> {

	@Autowired
	private AlamatDao alamatDao;

	@Autowired
	private AlamatConverter alamatConverter;

	@SuppressWarnings("unchecked")
	@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
	public List<AlamatVO> getAllAlamatForContoh() {
		List<AlamatVO> alamatVOList = new ArrayList<AlamatVO>();

		List<Alamat> listAlamat = IteratorUtils.toList(alamatDao.findAll().iterator());

		alamatConverter.transferListOfModelToListOfVO(listAlamat, alamatVOList);

		java.lang.annotation.Annotation[] annotations;
		try {
			annotations = Class.forName("com.jasamedika.medifirst2000.entities.Alamat").getAnnotations();
			System.out.println(Arrays.toString(annotations));
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}

		return alamatVOList;
	}

	@Override
	@Transactional(readOnly = false)
	public AlamatVO add(AlamatVO vo) throws JpaSystemException, ServiceVOException {
		// convert to model and call repository
		Alamat alamat = new Alamat();
		alamat=alamatConverter.transferVOToModel(vo, alamat);

		Alamat resultModel = alamatDao.save(alamat);

		// return VO
		AlamatVO resultVo = new AlamatVO();
		alamatConverter.transferModelToVO(resultModel, resultVo);

		return resultVo;
	}

	@Override
	@Transactional(readOnly = false)
	public AlamatVO update(AlamatVO vo) throws JpaSystemException, ServiceVOException {

		Alamat alamat = alamatDao.findOne(vo.getId());
		
		alamatConverter.transferVOToModel(vo, alamat);

		alamatDao.save(alamat);

		return vo;
	}

	@Override
	@Transactional(readOnly = false)
	public Boolean delete(Integer key) throws JpaSystemException {
		if (alamatDao.exists(key)) {
			alamatDao.delete(key);
			// if (!alamatMDao.exists(key))
			return Boolean.TRUE;
		}
		return Boolean.FALSE;
	}

	@Override
	@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
	public AlamatVO findById(Integer key) throws JpaSystemException {
		Alamat alamat = alamatDao.findOne(key);
		if (alamat == null) {
			return null;
		}
		AlamatVO alamatVO = new AlamatVO();
		alamatConverter.transferModelToVO(alamat, alamatVO);
		return alamatVO;
	}

	@Override
	@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
	public AlamatVO findByPasienId(Integer key) throws JpaSystemException {
		Alamat alamat = alamatDao.findAlamatByPasienId(key);
		if (alamat == null) {
			return null;
		}
		
		AlamatVO alamatVO = new AlamatVO();
		/*
		alamatVO.setId(alamat.getId());
		alamatVO.setKecamatan(alamat.getKecamatan());
		alamatVO.setRTRW(alamat.getRTRW());
		alamatVO.setAlamatLengkap(alamat.getAlamatLengkap());
		alamatVO.setDesaKelurahan(alamat.getDesaKelurahan());
		PropinsiVO prop=new PropinsiVO();
		NegaraVO neg=new NegaraVO();
		JenisAlamatVO jenAl=new JenisAlamatVO();
		prop.setId(1);
		neg.setId(1);
		jenAl.setId(1);
		alamatVO.setObjectPropinsi(prop);
		alamatVO.setObjectNegara(neg);
		alamatVO.setObjectJenisAlamat(jenAl);
		*/
		alamatConverter.transferModelToVO(alamat, alamatVO);
		return alamatVO;
	}
	@Override
	@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
	public Alamat findByPasienId2(Integer key) throws JpaSystemException {
		Alamat alamat = alamatDao.findAlamatByPasienId(key);
		if (alamat == null) {
			return null;
		}
		return alamat;
	}

	@SuppressWarnings("unchecked")
	@Override
	@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
	public List<AlamatVO> findAll() throws JpaSystemException {

		List<Alamat> alamatList = (List<Alamat>) alamatDao.findAll();

		List<AlamatVO> alamatVOList = new ArrayList<AlamatVO>();
		alamatConverter.transferListOfModelToListOfVO(alamatList, alamatVOList);

		return alamatVOList;
	}

	@Override
	@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
	public Map<String, Object> findAllWithPageAndLimitAndSortByAndDirectionParameter(Integer page, Integer limit,
			String sort, String dir) {
		return null;
	}

	

}
