package com.jasamedika.medifirst2000.service.impl;

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

import javax.persistence.Query;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.jpa.JpaSystemException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jasamedika.medifirst2000.util.JsonUtil;
import com.jasamedika.medifirst2000.exception.ServiceVOException;
import com.jasamedika.medifirst2000.service.ManagementDashboardService;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.entities.AntrianPasienDiPeriksa;
import com.jasamedika.medifirst2000.entities.ManagementDashboard;
import com.jasamedika.medifirst2000.vo.ManagementDashboardVO;
import com.jasamedika.medifirst2000.converter.BaseConverterImpl;
import com.jasamedika.medifirst2000.dao.ManagementDashboardDao;

/**
 * Implement class for ManagementDashboardService
 * 
 * @author Generator
 * @param <T>
 */
@Service("managementDashboardService")
public class ManagementDashboardServiceImpl extends BaseVoServiceImpl implements ManagementDashboardService {
	@Autowired
	private BaseConverterImpl<ManagementDashboardVO, ManagementDashboard> managementDashboardConverter;

	@Autowired
	private ManagementDashboardDao managementDashboardDao;

	@Override
	public Map<String, Object> saveManagementDashboard(ManagementDashboardVO vo) {
		Map<String, Object> result = new HashMap<String, Object>();
		ManagementDashboard model = new ManagementDashboard();
		model = managementDashboardConverter.transferVOToModel(vo, model);

		ManagementDashboard resultDomain = managementDashboardDao.save(model);
		if (CommonUtil.isNotNullOrEmpty(resultDomain)) {
			result.put("noRec", resultDomain.getNoRec());
		}

		return result;
	}

	@Override
	public Map<String, Object> findByName(String nama) {
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			result.put("items",
					JsonUtil.ToMaps(managementDashboardDao
							.findByKelompokContainingOrQueryContainingOrTypeContainingOrTitleContainingOrSpanContainingOrSeriesContainingOrDetailContaining(
									nama, nama, nama, nama, nama, nama, nama)));
		} catch (IllegalArgumentException | IllegalAccessException e) {
			e.printStackTrace();
		}
		return result;
	}

	@Override
	public Map<String, Object> findByNoRec(String noRec) {
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			result.put("items", managementDashboardDao.findOne(noRec).ToMap());
		} catch (IllegalArgumentException | IllegalAccessException e) {
			e.printStackTrace();
		}
		return result;
	}

	@Override
	public Map<String, Object> findByNoDetail(String noRec, Date date, Date date2) {
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			ManagementDashboard model = managementDashboardDao.findOne(noRec);
			StringBuffer buffer = new StringBuffer();
			buffer.append(model.getQuery());

			Query query = em.createQuery(buffer.toString());
			date2.setHours(23);
			date2.setMinutes(59);
			date2.setSeconds(59);
			query.setParameter("start", date);

			query.setParameter("until", date2);

			;
			result.put("items", JsonUtil.ToMaps(query.getResultList()));
		} catch (IllegalArgumentException | IllegalAccessException  e) {
			e.printStackTrace();
		}
		return result;
	}
}