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.MappingPertanyaanToKeteranganDao;
import com.jasamedika.medifirst2000.dao.PertanyaanSurveyDao;
import com.jasamedika.medifirst2000.dto.KeteranganSurveyDto;
import com.jasamedika.medifirst2000.dto.MappingPertanyaanDto;
import com.jasamedika.medifirst2000.dto.PertanyaanSurveyDto;
import com.jasamedika.medifirst2000.entities.KeteranganSurvey;
import com.jasamedika.medifirst2000.entities.MappingPertanyaanToKeterangan;
import com.jasamedika.medifirst2000.entities.PertanyaanSurvey;
import com.jasamedika.medifirst2000.service.PertanyaanSurveyService;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.vo.KeteranganSurveyVO;
import com.jasamedika.medifirst2000.vo.PertanyaanSurveyVO;

@Service("PertanyaanSurveyService")
public class PertanyaanSurveyServiceImpl extends BaseVoServiceImpl implements PertanyaanSurveyService{
	
	@Autowired
	private PertanyaanSurveyDao pertanyaanSurveyDao;
	
	@Autowired
	private MappingPertanyaanToKeteranganDao mappingDao;
	
	@Autowired
	private BaseConverterImpl<PertanyaanSurveyVO, PertanyaanSurvey> pertanyaanSurveyConverter;
	
	@Autowired
	private BaseConverterImpl<KeteranganSurveyVO, KeteranganSurvey> keteranganSurveyConverter;
	
	@Override
	public Map<String, Object> addPertanyaan(PertanyaanSurveyVO vo) {
		Map<String, Object> result = new HashMap<String, Object>();
		PertanyaanSurvey pertanyaanSurvey = new PertanyaanSurvey();
		if(CommonUtil.isNotNullOrEmpty(vo)){
			pertanyaanSurvey = pertanyaanSurveyConverter.transferVOToModel(vo, new PertanyaanSurvey());
		}
		List<MappingPertanyaanToKeterangan> list = new ArrayList<MappingPertanyaanToKeterangan>();
		if(CommonUtil.isNotNullOrEmpty(vo.getKeteranganSurvey())){
			for (KeteranganSurveyVO keteranganSurvey : vo.getKeteranganSurvey()) {
				MappingPertanyaanToKeterangan mapping = new MappingPertanyaanToKeterangan();
				mapping.setPertanyaanSurvey(pertanyaanSurvey);
				mapping.setKeteranganSurvey(keteranganSurveyConverter.transferVOToModel(keteranganSurvey, new KeteranganSurvey()));
				list.add(mapping);
			}
		}
		
		if(CommonUtil.isNotNullOrEmpty(pertanyaanSurvey.getMappingSet())){
			pertanyaanSurvey.getMappingSet().clear();
		}
		
		if(CommonUtil.isNotNullOrEmpty(list)){
			pertanyaanSurvey.getMappingSet().addAll(list);
		}
		
		pertanyaanSurvey.setCounterNomer(pertanyaanSurveyDao.getCount()+1);
		PertanyaanSurvey resultModel = new PertanyaanSurvey();
		if(CommonUtil.isNotNullOrEmpty(pertanyaanSurvey)){
			resultModel = pertanyaanSurveyDao.save(pertanyaanSurvey);
		}
		if(CommonUtil.isNotNullOrEmpty(resultModel)){
			result.put("data", resultModel.getId());
		}
		return result;
	}

	@Override
	public Map<String, Object> updatePertanyaan(PertanyaanSurveyVO vo) {
		Map<String, Object> result = new HashMap<String, Object>();
		PertanyaanSurvey pertanyaanSurvey = new PertanyaanSurvey();
		if(CommonUtil.isNotNullOrEmpty(vo)){
			pertanyaanSurvey = pertanyaanSurveyConverter.transferVOToModel(vo, new PertanyaanSurvey());
		}
		PertanyaanSurvey resultModel = new PertanyaanSurvey();
		if(CommonUtil.isNotNullOrEmpty(pertanyaanSurvey)){
			resultModel = pertanyaanSurveyDao.save(pertanyaanSurvey);
		}
		if(CommonUtil.isNotNullOrEmpty(resultModel)){
			result.put("data", resultModel.getId());
		}
		return result;
	}

	@Override
	public Map<String, Object> findAllPertanyaan() {
		Map<String, Object> result = new HashMap<String, Object>();
		List<PertanyaanSurvey> list = new ArrayList<PertanyaanSurvey>();
		list = (List<PertanyaanSurvey>) pertanyaanSurveyDao.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> deletePertanyaan(Integer id) {
		Map<String, Object> result = new HashMap<String, Object>();
		if(CommonUtil.isNotNullOrEmpty(id)){
			pertanyaanSurveyDao.delete(id);
			result.put("dataFound", true);
		}
		else{
			result.put("dataFound", false);
		}
		return result;
	}

	@Override
	public Map<String, Object> findAllPertanyaanSurveyFromMapping() {
		Map<String, Object> result = new HashMap<String, Object>();
		List<PertanyaanSurvey> pertanyaanList = new ArrayList<PertanyaanSurvey>();
		List<PertanyaanSurveyDto> pertanyaanDtoList = new ArrayList<PertanyaanSurveyDto>();
		pertanyaanList = mappingDao.getPertanyaanSurveyFromMapping();
		if(CommonUtil.isNotNullOrEmpty(pertanyaanList)){
			for(PertanyaanSurvey pertanyaanSurvey : pertanyaanList){
				PertanyaanSurveyDto pertanyaanDto = new PertanyaanSurveyDto();
				List<MappingPertanyaanToKeterangan> mappingList = new ArrayList<MappingPertanyaanToKeterangan>();
				pertanyaanDto.setId(pertanyaanSurvey.getId());
				pertanyaanDto.setPertanyaan(pertanyaanSurvey.getPertanyaan());
				pertanyaanDto.setCounterNomer(pertanyaanSurvey.getCounterNomer());
				mappingList = mappingDao.getMappingByIdPertanyaan(pertanyaanSurvey.getId());
				if(CommonUtil.isNotNullOrEmpty(mappingList)){
					List<MappingPertanyaanDto> mappingSet = new ArrayList<MappingPertanyaanDto>();
					for(MappingPertanyaanToKeterangan map : mappingList){
						MappingPertanyaanDto mappingDto = new MappingPertanyaanDto();
						mappingDto.setNoRec(map.getNoRec());
						mappingDto.setJumlah(map.getJumlah());
						KeteranganSurvey keteranganSurvey = new KeteranganSurvey();
						keteranganSurvey = mappingDao.getKeteranganSurveyByNorecMapping(map.getNoRec());
						if(CommonUtil.isNotNullOrEmpty(keteranganSurvey)){
							KeteranganSurveyDto keteranganDto = new KeteranganSurveyDto();
							keteranganDto.setId(keteranganSurvey.getId());
							keteranganDto.setNomor(keteranganSurvey.getNomor());
							keteranganDto.setKeterangan(keteranganSurvey.getKeterangan());
							mappingDto.setKeteranganSurvey(keteranganDto);
							mappingDto.setId(keteranganSurvey.getId());
							mappingDto.setKeterangan(keteranganSurvey.getKeterangan());
							mappingSet.add(mappingDto);
						}
					}
					pertanyaanDto.getMappingSet().addAll(mappingSet);
					pertanyaanDtoList.add(pertanyaanDto);
				}
			}
		}
		
		if(CommonUtil.isNotNullOrEmpty(pertanyaanDtoList)){
			result.put("data", pertanyaanDtoList);
		}
		return result;
	}

}
