package com.jasamedika.medifirst2000.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jasamedika.medifirst2000.converter.BaseConverterImpl;
import com.jasamedika.medifirst2000.dao.ObservasiTransaksiDao;
import com.jasamedika.medifirst2000.dao.PengkajianAwalDao;
import com.jasamedika.medifirst2000.dao.VariabelDao;
import com.jasamedika.medifirst2000.entities.JenisObservasi;
import com.jasamedika.medifirst2000.entities.Observasi;
import com.jasamedika.medifirst2000.entities.ObservasiTransaksi;
import com.jasamedika.medifirst2000.entities.PasienDaftar;
import com.jasamedika.medifirst2000.entities.Pegawai;
import com.jasamedika.medifirst2000.entities.PengkajianAwal;
import com.jasamedika.medifirst2000.entities.Variabel;
import com.jasamedika.medifirst2000.entities.VariabelD;
import com.jasamedika.medifirst2000.service.ObservasiTransaksiService;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.vo.JenisObservasiVO;
import com.jasamedika.medifirst2000.vo.ObservasiCustomVO;
import com.jasamedika.medifirst2000.vo.ObservasiTransaksiVO;
import com.jasamedika.medifirst2000.vo.ObservasiVO;
import com.jasamedika.medifirst2000.vo.PasienDaftarVO;
import com.jasamedika.medifirst2000.vo.PegawaiVO;
import com.jasamedika.medifirst2000.vo.VariabelDVO;
import com.jasamedika.medifirst2000.vo.VariabelVO;

@Service("ObservasiTransaksiService")
public class ObservasiTransaksiServiceImpl implements ObservasiTransaksiService {

	@Autowired
	private ObservasiTransaksiDao observasiTransaksiDao;
	
	@Autowired
	private VariabelDao variabelDao;

	@Autowired
	private BaseConverterImpl<ObservasiTransaksiVO, ObservasiTransaksi> observasiTransaksiConverter;

	@Autowired
	private BaseConverterImpl<PasienDaftarVO, PasienDaftar> pasienDaftarConverter;

	@Autowired
	private BaseConverterImpl<ObservasiVO, Observasi> observasiConverter;

	@Autowired
	private BaseConverterImpl<PegawaiVO, Pegawai> pegawaiConverter;

	@Autowired
	private BaseConverterImpl<JenisObservasiVO, JenisObservasi> jenisObservasiConverter;
	
	@Autowired
	private BaseConverterImpl<VariabelVO, Variabel> variabelConverter;
	
	@Autowired
	private BaseConverterImpl<VariabelDVO, VariabelD> variabelDConverter;

	@Override
	public Map<String, Object> save(ObservasiCustomVO vo) {
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			
			//looping for save Observasi
			if(CommonUtil.isNotNullOrEmpty(vo.getListObservasi()))
			{
				for (ObservasiTransaksiVO vos : vo.getListObservasi()) {
					ObservasiTransaksi observasiTransaksi = observasiTransaksiConverter.transferVOToModel(vos,
							new ObservasiTransaksi());
					if (CommonUtil.isNotNullOrEmpty(vos.getNoRegistrasi())) {
						observasiTransaksi.setNoRegistrasi(
								pasienDaftarConverter.transferVOToModel(vos.getNoRegistrasi(), new PasienDaftar()));
					}
					if (CommonUtil.isNotNullOrEmpty(vos.getObservasi())) {
						observasiTransaksi.setObservasi(observasiConverter.transferVOToModel(vos.getObservasi(), new Observasi()));
					}
					if (CommonUtil.isNotNullOrEmpty(vos.getPegawai())) {
						observasiTransaksi.setPegawai(pegawaiConverter.transferVOToModel(vos.getPegawai(),new Pegawai()));
					}
					if (CommonUtil.isNotNullOrEmpty(vos.getJenisObservasi())) {
						observasiTransaksi.setJenisObservasi(jenisObservasiConverter.transferVOToModel(vos.getJenisObservasi(), new JenisObservasi()));
					}
					observasiTransaksi.setTglInput(new Date());
					if(CommonUtil.isNotNullOrEmpty(observasiTransaksi))
					{
						ObservasiTransaksi model = observasiTransaksiDao.save(observasiTransaksi);
						result.put(model.getNoRec(), model.getValueI());
					}
				}
			}

			
			//looping for save variabel
			Set<VariabelD> variabelDetail = new HashSet<>();
			if(CommonUtil.isNotNullOrEmpty(vo.getListVariabel()))
			{
				for(VariabelVO vos : vo.getListVariabel())
				{
					Variabel variabel = variabelConverter.transferVOToModel(vos, new Variabel());
					if (CommonUtil.isNotNullOrEmpty(vos.getNoRegistrasi())) {
						variabel.setNoRegistrasi(
								pasienDaftarConverter.transferVOToModel(vos.getNoRegistrasi(), new PasienDaftar()));
					}
					if (CommonUtil.isNotNullOrEmpty(vos.getPegawai())) {
						variabel.setPegawai(pegawaiConverter.transferVOToModel(vos.getPegawai(),new Pegawai()));
					}
					if (CommonUtil.isNotNullOrEmpty(vos.getJenisObservasi())) {
						variabel.setJenisObservasi(jenisObservasiConverter.transferVOToModel(vos.getJenisObservasi(), new JenisObservasi()));
					}
					variabel.setTglInput(new Date());
					for(VariabelDVO voss : vos.getVariabelDetail())
					{
						VariabelD detail = variabelDConverter.transferVOToModel(voss, new VariabelD());					
						detail.setVariabel(variabel);
						variabelDetail.add(detail);
					}
					variabel.getVariabelDetail().clear();
					variabel.setVariabelDetail(variabelDetail);
					if(CommonUtil.isNotNullOrEmpty(variabel))
					{
						Variabel model = variabelDao.save(variabel);
						result.put(model.getNoRec(), model.getVariabel());
					}
				}
			}			
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	@Override
	public List<Map<String, Object>> getObservasi() {
		List<Map<String, Object>> result = new ArrayList<>();
		List<Map<String, Object>> head = getChildObservasi(0);
		List<String> jenis = observasiTransaksiDao.getJenisObservasi();
		for(String data: jenis)
		{
			Map<String, Object> byJenis = new HashMap<String, Object>();
			byJenis.put("jenis", data);			
			List<Map<String, Object>> observasi = new ArrayList<>();
			for(Map<String, Object> temp : head)
			{
				if(((String)temp.get("jenisObservasi")).equals(data))
				{
					observasi.add(temp);
				}
			}
			List<Map<String, Object>> variabel = observasiTransaksiDao.getValueVariabel(data);
			if(CommonUtil.isNotNullOrEmpty(variabel))
			{
				byJenis.put("variabels", variabel);
			}
			
			byJenis.put("details", observasi);
			result.add(byJenis);
		}
		return result;
	}
	private List<Map<String, Object>> getChildObservasi(int id)
	{
		List<Map<String, Object>> result = observasiTransaksiDao.getObservasi(id);
		for(Map<String, Object> data : result)
		{
			List<Map<String, Object>> temp = getChildObservasi((Integer)data.get("idObservasi"));
			if(CommonUtil.isNotNullOrEmpty(temp))
			{
				data.put("detail", temp);
			}
		}
		return result;
	}
	

	@Override
	public List<Map<String, Object>> getObservasiValue(String startDate, String endDate, String noRegistrasi) {
		List<Map<String, Object>> result = observasiTransaksiDao.getValueObservasi(startDate, endDate, noRegistrasi);
		return result;
	}

}
