package com.jasamedika.medifirst2000.service.impl;

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jasamedika.medifirst2000.converter.BaseConverterImpl;
import com.jasamedika.medifirst2000.dao.KeteranganSurveyDao;
import com.jasamedika.medifirst2000.dao.MappingPertanyaanToKeteranganDao;
import com.jasamedika.medifirst2000.dao.PertanyaanSurveyDao;
import com.jasamedika.medifirst2000.entities.KeteranganSurvey;
import com.jasamedika.medifirst2000.entities.MappingPertanyaanToKeterangan;
import com.jasamedika.medifirst2000.entities.PertanyaanSurvey;
import com.jasamedika.medifirst2000.service.MappingPertanyaanToKeteranganService;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.vo.KeteranganSurveyVO;
import com.jasamedika.medifirst2000.vo.MappingPertanyaanToKeteranganVO;
import com.jasamedika.medifirst2000.vo.PertanyaanSurveyVO;

@Service("MappingPertanyaanToKeteranganService")
public class MappingPertanyaanToKeteranganServiceImpl implements MappingPertanyaanToKeteranganService {
	
	@Autowired
	private PertanyaanSurveyDao pertanyaanSurveyDao;
	
	@Autowired
	private KeteranganSurveyDao keteranganSurveyDao;
	
	@Autowired
	private MappingPertanyaanToKeteranganDao mappingDao;
	
	@Autowired
	private BaseConverterImpl<MappingPertanyaanToKeteranganVO, MappingPertanyaanToKeterangan> mappingPertanyaanToKeteranganConverter;
	
	@Autowired
	private BaseConverterImpl<PertanyaanSurveyVO, PertanyaanSurvey> pertanyaanSurveyConverter;
	
	@Autowired
	private BaseConverterImpl<KeteranganSurveyVO, KeteranganSurvey> keteranganSurveyConverter;
	
	@Override
	public Map<String, Object> addMappingPertanyaan(MappingPertanyaanToKeteranganVO vo) {
		Map<String, Object> result = new HashMap<String, Object>();
		MappingPertanyaanToKeterangan cek = new MappingPertanyaanToKeterangan();
		cek = mappingDao.getByPertanyaanDanKeterangan(vo.getPertanyaanSurvey().getId(), vo.getKeteranganSurvey().getId());
		if(!(CommonUtil.isNotNullOrEmpty(cek))){
			MappingPertanyaanToKeterangan mappingPertanyaanToKeterangan = new MappingPertanyaanToKeterangan();
			if(CommonUtil.isNotNullOrEmpty(vo)){
				mappingPertanyaanToKeterangan = mappingPertanyaanToKeteranganConverter.transferVOToModel(vo, new MappingPertanyaanToKeterangan());
			}
			if(CommonUtil.isNotNullOrEmpty(vo.getPertanyaanSurvey())){
				mappingPertanyaanToKeterangan.setPertanyaanSurvey(pertanyaanSurveyConverter.transferVOToModel(vo.getPertanyaanSurvey(), new PertanyaanSurvey()));
			}
			if(CommonUtil.isNotNullOrEmpty(vo.getKeteranganSurvey())){
				mappingPertanyaanToKeterangan.setKeteranganSurvey(keteranganSurveyConverter.transferVOToModel(vo.getKeteranganSurvey(), new KeteranganSurvey()));
			}
			mappingPertanyaanToKeterangan.setJumlah(0);
			MappingPertanyaanToKeterangan resultModel = new MappingPertanyaanToKeterangan();
			if(CommonUtil.isNotNullOrEmpty(mappingPertanyaanToKeterangan)){
				resultModel = mappingDao.save(mappingPertanyaanToKeterangan);
			}
			if(CommonUtil.isNotNullOrEmpty(resultModel)){
				result.put("data", resultModel.getNoRec());
			}
		}
		return result;
	}

	@Override
	public Map<String, Object> findAllMappingPertanyaan() {
		Map<String, Object> result = new HashMap<String, Object>();
		List<MappingPertanyaanToKeterangan> list = new ArrayList<MappingPertanyaanToKeterangan>();
		list = (List<MappingPertanyaanToKeterangan>) mappingDao.findAll();
		if(CommonUtil.isNotNullOrEmpty(list)){
			result.put("data", list);
			result.put("dataFound", true);
		}
		else{
			result.put("dataFound", false);
		}
		return result;
	}

	@Override
	public Map<String, Object> addAllMappingPertanyaan(List<MappingPertanyaanToKeteranganVO> vos) {
		Map<String, Object> result = new HashMap<String, Object>();
		if(CommonUtil.isNotNullOrEmpty(vos)){
			setPertanyaanDanKeterangan();
			for(MappingPertanyaanToKeteranganVO vo : vos){
				MappingPertanyaanToKeterangan cek = new MappingPertanyaanToKeterangan();
				cek = mappingDao.getByPertanyaanDanKeterangan(vo.getPertanyaanSurvey().getId(), vo.getKeteranganSurvey().getId());
				if((CommonUtil.isNotNullOrEmpty(cek))){
					MappingPertanyaanToKeterangan mappingPertanyaanToKeterangan = new MappingPertanyaanToKeterangan();
					if(CommonUtil.isNotNullOrEmpty(vo)){
						mappingPertanyaanToKeterangan = mappingPertanyaanToKeteranganConverter.transferVOToModel(vo, new MappingPertanyaanToKeterangan());
					}
					if(CommonUtil.isNotNullOrEmpty(vo.getPertanyaanSurvey())){
						mappingPertanyaanToKeterangan.setPertanyaanSurvey(pertanyaanSurveyConverter.transferVOToModel(vo.getPertanyaanSurvey(), new PertanyaanSurvey()));
					}
					if(CommonUtil.isNotNullOrEmpty(vo.getKeteranganSurvey())){
						mappingPertanyaanToKeterangan.setKeteranganSurvey(keteranganSurveyConverter.transferVOToModel(vo.getKeteranganSurvey(), new KeteranganSurvey()));
					}
					mappingPertanyaanToKeterangan.setNoRec(cek.getNoRec());
					mappingPertanyaanToKeterangan.setStatusEnabled(true);
					mappingPertanyaanToKeterangan.setJumlah(0);
					MappingPertanyaanToKeterangan resultModel = new MappingPertanyaanToKeterangan();
					if(CommonUtil.isNotNullOrEmpty(mappingPertanyaanToKeterangan)){
						resultModel = mappingDao.save(mappingPertanyaanToKeterangan);
					}
				}
			}
			result.put("data", "Success");
		}
		return result;
	}
	
	private void setPertanyaanDanKeterangan(){
		List<PertanyaanSurvey> listPertanyaan = new ArrayList<PertanyaanSurvey>();
		listPertanyaan = (List<PertanyaanSurvey>) pertanyaanSurveyDao.findAll();
		List<KeteranganSurvey> listKeterangan = new ArrayList<KeteranganSurvey>();
		listKeterangan = (List<KeteranganSurvey>) keteranganSurveyDao.findAll();
		if(CommonUtil.isNotNullOrEmpty(listPertanyaan)){
			for(PertanyaanSurvey pertanyaan : listPertanyaan){
				if(CommonUtil.isNotNullOrEmpty(listKeterangan)){
					for(KeteranganSurvey keterangan : listKeterangan){
						MappingPertanyaanToKeterangan mapping = new MappingPertanyaanToKeterangan();
						mapping = mappingDao.getByPertanyaanDanKeterangan(pertanyaan.getId(), keterangan.getId());
						if(!CommonUtil.isNotNullOrEmpty(mapping)){
							mapping = new MappingPertanyaanToKeterangan();
							mapping.setJumlah(0);
							KeteranganSurvey mKeterangan = new KeteranganSurvey();
							mKeterangan.setId(keterangan.getId());
							PertanyaanSurvey mPertanyaan = new PertanyaanSurvey();
							mPertanyaan.setId(pertanyaan.getId());
							mapping.setKeteranganSurvey(mKeterangan);
							mapping.setPertanyaanSurvey(mPertanyaan);
						}
						mapping.setStatusEnabled(false);
						if(CommonUtil.isNotNullOrEmpty(mapping)){
							mappingDao.save(mapping);
						}
					}
				}
			}
		}
	}
	
	@Override
	public Map<String, Object> updateJumlahSurvey(List<MappingPertanyaanToKeteranganVO> vos) {
		Map<String, Object> result = new HashMap<String, Object>();
		if(CommonUtil.isNotNullOrEmpty(vos)){
			for(MappingPertanyaanToKeteranganVO vo : vos){
				Integer idPertanyaan = null, idKeterangan = null;
				idPertanyaan = vo.getPertanyaanSurvey().getId();
				idKeterangan = vo.getKeteranganSurvey().getId();
				MappingPertanyaanToKeterangan mappingPertanyaanToKeterangan = new MappingPertanyaanToKeterangan();
				mappingPertanyaanToKeterangan = mappingDao.getByPertanyaanDanKeterangan(idPertanyaan, idKeterangan);
				if(CommonUtil.isNotNullOrEmpty(mappingPertanyaanToKeterangan)){
					mappingPertanyaanToKeterangan.setJumlah(mappingDao.getJumlahByPertanyaanDanKeterangan(idPertanyaan, idKeterangan)+1);
					MappingPertanyaanToKeterangan resultModel = new MappingPertanyaanToKeterangan();
					resultModel = mappingDao.save(mappingPertanyaanToKeterangan);
					result.put("jumlah", resultModel.getJumlah());
					result.put("dataFound", true);
				}
			}
		}
		else{
			result.put("dataFound", false);
		}
		return result;
	}

}
