package com.jasamedika.medifirst2000.service.impl;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
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.ApresiasiAtasLayananDao;
import com.jasamedika.medifirst2000.entities.ApresiasiAtasLayanan;
import com.jasamedika.medifirst2000.entities.GambarMukaKepuasan;
import com.jasamedika.medifirst2000.entities.StatusYaTidak;
import com.jasamedika.medifirst2000.service.ApresiasiAtasLayananService;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.vo.ApresiasiAtasLayananVO;
import com.jasamedika.medifirst2000.vo.GambarMukaKepuasanVO;
import com.jasamedika.medifirst2000.vo.StatusYaTidakVO;

@Service("ApresiasiAtasLayananService")
public class ApresiasiAtasLayananServiceImpl implements ApresiasiAtasLayananService{
	
	@Autowired
	private ApresiasiAtasLayananDao apresiasiAtasLayananDao;
	
	@Autowired
	private BaseConverterImpl<ApresiasiAtasLayananVO, ApresiasiAtasLayanan> apresiasiAtasLayananConverter;
	
	@Autowired
	private BaseConverterImpl<GambarMukaKepuasanVO, GambarMukaKepuasan> gambarMukaKepuasanConverter;
	
	@SuppressWarnings("deprecation")
	@Override
	public Map<String, Object> addApresiasiAtasLayanan(ApresiasiAtasLayananVO vo) {
		ApresiasiAtasLayanan apresiasiAtasLayanan = new ApresiasiAtasLayanan();
		if(CommonUtil.isNotNullOrEmpty(vo)){
			apresiasiAtasLayanan = apresiasiAtasLayananConverter.transferVOToModel(vo, new ApresiasiAtasLayanan());
		}
		if(CommonUtil.isNotNullOrEmpty(vo.getGambarMukaKepuasan())){
			apresiasiAtasLayanan.setGambarMukaKepuasan(gambarMukaKepuasanConverter.transferVOToModel(vo.getGambarMukaKepuasan(), new GambarMukaKepuasan()));
		}
		Date tanggal = new Date();
		Date now = new Date();
		
		Calendar calendar = Calendar.getInstance();
		calendar.clear();
		calendar.set(Calendar.YEAR, now.getYear() + 1900);
		calendar.set(Calendar.MONTH, now.getMonth());
		calendar.set(Calendar.DATE, now.getDate());
		tanggal = calendar.getTime();
		
		apresiasiAtasLayanan.setTanggal(tanggal);
		Map<String, Object> result = new HashMap<String,Object>();
		
		ApresiasiAtasLayanan resultModel = apresiasiAtasLayananDao.save(apresiasiAtasLayanan);
		
		if(CommonUtil.isNotNullOrEmpty(resultModel)){
			result.put("data", resultModel);
		}
		return result;
	}

	@Override
	public Map<String, Object> updateApresiasiAtasLayanan(ApresiasiAtasLayananVO vo) {
		ApresiasiAtasLayanan apresiasiAtasLayanan = new ApresiasiAtasLayanan();
		if(CommonUtil.isNotNullOrEmpty(vo)){
			apresiasiAtasLayanan = apresiasiAtasLayananConverter.transferVOToModel(vo, new ApresiasiAtasLayanan());
		}
		if(CommonUtil.isNotNullOrEmpty(vo.getGambarMukaKepuasan())){
			apresiasiAtasLayanan.setGambarMukaKepuasan(gambarMukaKepuasanConverter.transferVOToModel(vo.getGambarMukaKepuasan(), new GambarMukaKepuasan()));
		}
		apresiasiAtasLayanan.setTanggal(new Date());
		
		Map<String, Object> result = new HashMap<String,Object>();
		
		ApresiasiAtasLayanan resultModel = apresiasiAtasLayananDao.save(apresiasiAtasLayanan);
		
		if(CommonUtil.isNotNullOrEmpty(resultModel)){
			result.put("data", resultModel);
		}
		return result;
	}

	@Override
	public Map<String, Object> findAllApresiasiAtasLayanan() {
		List<ApresiasiAtasLayanan> list = new ArrayList<ApresiasiAtasLayanan>();
		Map<String, Object> result = new HashMap<String,Object>();
		list = (List<ApresiasiAtasLayanan>) apresiasiAtasLayananDao.findAll();
		
		if(CommonUtil.isNotNullOrEmpty(list)){
			result.put("data", list);
		}
		return result;
	}

	@Override
	public Map<String, Object> rekapApresiasi(String from, String to) {
		Map<String, Object> result = new HashMap<String, Object>();
		Integer puas, kurangPuas, tidakPuas;
		puas = apresiasiAtasLayananDao.getPuas(from, to);
		kurangPuas = apresiasiAtasLayananDao.getKurangPuas(from, to);
		tidakPuas = apresiasiAtasLayananDao.getTidakPuas(from, to);
		result.put("Puas", puas);
		result.put("KurangPuas", kurangPuas);
		result.put("TidakPuas", tidakPuas);
		if(CommonUtil.isNotNullOrEmpty(result)){
			result.put("dataFound", true);
		}
		else{
			result.put("dataFound", false);
		}
		return result;
	}

}
