package com.jasamedika.medifirst2000.dao.custom.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.Query;

import org.springframework.stereotype.Repository;

import com.jasamedika.medifirst2000.dao.custom.PasienDaoCustom;
import com.jasamedika.medifirst2000.dao.custom.base.impl.CoreDaoImpl;
import com.jasamedika.medifirst2000.entities.Pasien;
import com.jasamedika.medifirst2000.util.CommonUtil;

/**
 * Repository Custom class
 * 
 * @author Adik
 * @param <T>
 */
@Repository
public class PasienDaoCustomImpl extends CoreDaoImpl<Pasien> implements PasienDaoCustom {

	public PasienDaoCustomImpl() {
		super(Pasien.class);
	}
	
	@Override
	public Integer findAllPasienPagingCount(Date startDate, Date endDate,String noCm,String namaIbu,Date birthDate) {
		
		StringBuffer buffer = new StringBuffer();
		buffer.append("select count(x.id) from Pasien x where x.id IS NOT NULL  ");
		if(CommonUtil.isNotNullOrEmpty(startDate) && CommonUtil.isNotNullOrEmpty(endDate)){
			buffer.append("and x.tglDaftar between :startDate and :endDate  ");
		}
		if(CommonUtil.isNotNullOrEmpty(noCm)){
			buffer.append("and (x.noCm like :noCm or x.namaPasien like :noCm) ");
		}
		
		if(CommonUtil.isNotNullOrEmpty(namaIbu)){
			buffer.append("and x.namaIbu like :namaIbu ");
		}
		if(CommonUtil.isNotNullOrEmpty(birthDate)){
			buffer.append("and date(x.tglLahir) =:birthDate ");
		}
		
		Query query = em.createQuery(buffer.toString());
		if (CommonUtil.isNotNullOrEmpty(noCm)) {
			query.setParameter("noCm", "%"+noCm.toLowerCase()+"%");
		}
		if(CommonUtil.isNotNullOrEmpty(startDate) && CommonUtil.isNotNullOrEmpty(endDate)){
			query.setParameter("startDate", startDate);
			query.setParameter("endDate", endDate);
		}
		
		if(CommonUtil.isNotNullOrEmpty(namaIbu)){
			query.setParameter("namaIbu", "%"+namaIbu.toLowerCase()+"%");
		}
		if(CommonUtil.isNotNullOrEmpty(birthDate)){
			query.setParameter("birthDate", birthDate);
		}
		
		
		return ((Long) query.getSingleResult()).intValue();
	}

	@Override
	public List<Pasien> findAllPasienPagingList(int rowStart, int rowEnd, Date startDate, Date endDate,String noCm,String namaAyah,String birthDate) {
		
		StringBuffer buffer = new StringBuffer();
		buffer.append("select x from Pasien x where x.id IS NOT NULL  ");
		if(CommonUtil.isNotNullOrEmpty(startDate) && CommonUtil.isNotNullOrEmpty(endDate)){
			buffer.append("and x.tglDaftar between :startDate and :endDate  ");
		}
		if(CommonUtil.isNotNullOrEmpty(noCm)){
			buffer.append("and (upper(x.noCm) like :noCm or upper(x.namaPasien) like :noCm) ");
		}
		if(CommonUtil.isNotNullOrEmpty(birthDate)){
			buffer.append("and to_char(x.tglLahir, 'yyyy-MM-dd') =:birthDate ");
		}

		if(CommonUtil.isNotNullOrEmpty(namaAyah)){
			
			buffer.append("and lower(x.namaAyah) like lower(:namaAyah) ");
		}		
		
		Query query = em.createQuery(buffer.toString());
		if (CommonUtil.isNotNullOrEmpty(noCm)) {
			query.setParameter("noCm", "%"+noCm.toUpperCase()+"%");
		}
		if(CommonUtil.isNotNullOrEmpty(startDate) && CommonUtil.isNotNullOrEmpty(endDate)){
			query.setParameter("startDate", startDate);
			query.setParameter("endDate", endDate);
		}
		if(CommonUtil.isNotNullOrEmpty(namaAyah)){
			query.setParameter("namaAyah", namaAyah);
		}
		if(CommonUtil.isNotNullOrEmpty(birthDate)){
			query.setParameter("birthDate", birthDate);
		}
		query.setFirstResult(rowStart);
		query.setMaxResults(rowEnd);

		return (List<Pasien>) query.getResultList();
	}
	
	public Map<String, Object> getJenisKelamin(){
		Map<String, Object> result = new HashMap<>();
		try{
		StringBuffer buffer = new StringBuffer();
			buffer.append("select NEW Map ("
				+ "a.id as id, "
				+ "a.jenisKelamin as jenisKelamin) "
				+ "from JenisKelamin a");
			Query query = em.createQuery(buffer.toString());
			result = (Map<String, Object>) query.getResultList();
		} catch(Exception ex){
			ex.printStackTrace();
		}
		return result;
	}
	
	public Map<String, Object> getAgama(){
		Map<String, Object> result = new HashMap<>();
		try{
		StringBuffer buffer = new StringBuffer();
			buffer.append("select NEW Map ("
				+ "a.id as id, "
				+ "a.agama as agama) "
				+ "from Agama a");
			Query query = em.createQuery(buffer.toString());
			result = (Map<String, Object>) query.getResultList();
		} catch(Exception ex){
			ex.printStackTrace();
		}
		return result;
	}
	
	public Map<String, Object> getPendidikan(){
		Map<String, Object> result = new HashMap<>();
		try{
		StringBuffer buffer = new StringBuffer();
			buffer.append("select NEW Map ("
				+ "a.id as id, "
				+ "a.namaPendidikan as namaPendidikan) "
				+ "from Pendidikan a");
			Query query = em.createQuery(buffer.toString());
			result = (Map<String, Object>) query.getResultList();
		} catch(Exception ex){
			ex.printStackTrace();
		}
		return result;
	}
	
	public Map<String, Object> getPekerjaan(){
		Map<String, Object> result = new HashMap<>();
		try{
		StringBuffer buffer = new StringBuffer();
			buffer.append("select NEW Map ("
				+ "a.id as id, "
				+ "a.pekerjaan as pekerjaan) "
				+ "from Pekerjaan a");
			Query query = em.createQuery(buffer.toString());
			result = (Map<String, Object>) query.getResultList();
		} catch(Exception ex){
			ex.printStackTrace();
		}
		return result;
	}

	public Map<String, Object> getKebangsaan(){
		Map<String, Object> result = new HashMap<>();
		try{
		StringBuffer buffer = new StringBuffer();
			buffer.append("select NEW Map ("
				+ "a.id as id, "
				+ "a.name as kebangsaan) "
				+ "from Kebangsaan a");
			Query query = em.createQuery(buffer.toString());
			result = (Map<String, Object>) query.getResultList();
		} catch(Exception ex){
			ex.printStackTrace();
		}
		return result;
	}
	
	public Map<String, Object> getNegara(){
		Map<String, Object> result = new HashMap<>();
		try{
		StringBuffer buffer = new StringBuffer();
			buffer.append("select NEW Map ("
				+ "a.id as id, "
				+ "a.namaNegara as namaNegara) "
				+ "from Negara a");
			Query query = em.createQuery(buffer.toString());
			result = (Map<String, Object>) query.getResultList();
		} catch(Exception ex){
			ex.printStackTrace();
		}
		return result;
	}
	
	public Map<String, Object> getProponsi(){
		Map<String, Object> result = new HashMap<>();
		try{
		StringBuffer buffer = new StringBuffer();
			buffer.append("select NEW Map ("
				+ "a.id as id, "
				+ "a.namaPropinsi as namaPropinsi) "
				+ "from Propinsi a");
			Query query = em.createQuery(buffer.toString());
			result = (Map<String, Object>) query.getResultList();
		} catch(Exception ex){
			ex.printStackTrace();
		}
		return result;
	}
	
}
