package com.jasamedika.medifirst2000.service.impl;

import java.util.ArrayList;
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 com.jasamedika.medifirst2000.converter.BaseConverterImpl;
import com.jasamedika.medifirst2000.dao.K3CheckListFacillitySefetyDao;
import com.jasamedika.medifirst2000.entities.K3CheckListFacillitySefety;
import com.jasamedika.medifirst2000.entities.K3CheckListFacillitySefetyDetail;
import com.jasamedika.medifirst2000.entities.K3ItemPeriksaGedung;
import com.jasamedika.medifirst2000.entities.Ruangan;
import com.jasamedika.medifirst2000.service.K3CheckListFacillitySefetyService;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.vo.K3CheckListFacillitySefetyDetailVO;
import com.jasamedika.medifirst2000.vo.K3CheckListFacillitySefetyVO;
import com.jasamedika.medifirst2000.vo.K3ItemPeriksaGedungVO;
import com.jasamedika.medifirst2000.vo.RuanganVO;

@Service("k3CheckListFacillitySefetyService")
public class K3CheckListFacillitySefetyServiceImpl implements K3CheckListFacillitySefetyService{

	@Autowired
	private K3CheckListFacillitySefetyDao k3CheckListFacillitySefetyDao;
	
	@Autowired
	private BaseConverterImpl<K3CheckListFacillitySefetyVO, K3CheckListFacillitySefety> k3CheckListFacillitySefetyConverter;
	
	@Autowired
	private BaseConverterImpl<RuanganVO, Ruangan> ruanganConverter;
	
	@Autowired
	private BaseConverterImpl<K3CheckListFacillitySefetyDetailVO, K3CheckListFacillitySefetyDetail> k3CheckListFacillitySefetyDetailConverter;
	
	@Autowired
	private BaseConverterImpl<K3ItemPeriksaGedungVO, K3ItemPeriksaGedung> k3ItemPeriksaGedungConverter;
	
	@Override
	public Map<String, Object> getK3Ruangan(){
		Map<String, Object> result = new HashMap<>();
		List<Map<String, Object>> listRuangan = k3CheckListFacillitySefetyDao.getK3Ruangan();
		if(CommonUtil.isNotNullOrEmpty(listRuangan)){
			result.put("listRuangan", listRuangan);
		}
		return result;
	}
	
	@Override
	public Map<String, Object> getK3FacillityCheck(){
		Map<String, Object> result = new HashMap<>();
		List<Map<String, Object>> k3FacillityCheckList = k3CheckListFacillitySefetyDao.getK3FacillityCheck();
		if(CommonUtil.isNotNullOrEmpty(k3FacillityCheckList)){
			result.put("listData", k3FacillityCheckList);
		}
		return result; 
	}
	
	@Override
	public Map<String, Object> getK3KelompokLpjGedung(Integer k3FacillityCheckId){
		Map<String, Object> result = new HashMap<>();
		
		if(CommonUtil.isNotNullOrEmpty(k3FacillityCheckId)){
			List<Map<String, Object>> k3KelompokLpjGedungList = k3CheckListFacillitySefetyDao.getK3KelompokLpjGedung(k3FacillityCheckId);
			
			List<Map<String, Object>> listKelompokSet = new ArrayList<>();
			for(Map<String, Object> mapp : k3KelompokLpjGedungList){
				List<Map<String, Object>> k3ItemPeriksaGedungList = k3CheckListFacillitySefetyDao.getK3ItemPeriksaGedung((Integer)mapp.get("id"));
				if(CommonUtil.isNotNullOrEmpty(k3ItemPeriksaGedungList)){
					Map<String, Object> listKelompok = new HashMap<>();
					listKelompok.put(mapp.get("namaKelompok").toString().replaceAll(" ", ""), k3ItemPeriksaGedungList.toArray());
					listKelompokSet.add(listKelompok);
				}
			}
			result.put("listKelompok", listKelompokSet);
		}
		return result;
	}
	
	@Override
	public Map<String, Object> saveK3CheckListFacillitySefety(K3CheckListFacillitySefetyVO vo){
		Map<String, Object> result = new HashMap<>();
		K3CheckListFacillitySefety k3CheckListFacillitySefety = k3CheckListFacillitySefetyConverter.transferVOToModel(vo, new K3CheckListFacillitySefety());
		
		if(CommonUtil.isNotNullOrEmpty(vo.getRuangan())){
			k3CheckListFacillitySefety.setRuangan(ruanganConverter.transferVOToModel(vo.getRuangan(), new Ruangan()));
		}
		if(CommonUtil.isNotNullOrEmpty(vo.getK3CheckListFacillitySefetyDetail())){
			Set<K3CheckListFacillitySefetyDetail> k3CheckListFacillitySefetyDetailSet = new HashSet<>();
			
			for(K3CheckListFacillitySefetyDetailVO modelVO : vo.getK3CheckListFacillitySefetyDetail()){
				K3CheckListFacillitySefetyDetail k3CheckListFacillitySefetyDetail = k3CheckListFacillitySefetyDetailConverter.transferVOToModel(modelVO, new K3CheckListFacillitySefetyDetail());
				
				if(CommonUtil.isNotNullOrEmpty(modelVO)){
					k3CheckListFacillitySefetyDetail.setK3CheckListFacillitySefety(k3CheckListFacillitySefety);
				}
				if(CommonUtil.isNotNullOrEmpty(modelVO.getK3ItemPeriksaGedung())){
					k3CheckListFacillitySefetyDetail.setK3ItemPeriksaGedung(k3ItemPeriksaGedungConverter.transferVOToModel(modelVO.getK3ItemPeriksaGedung(), new K3ItemPeriksaGedung()));
				}
				if(CommonUtil.isNotNullOrEmpty(k3CheckListFacillitySefetyDetail)){
					k3CheckListFacillitySefetyDetailSet.add(k3CheckListFacillitySefetyDetail);
				}
			}
			if(CommonUtil.isNotNullOrEmpty(k3CheckListFacillitySefetyDetailSet)){
				k3CheckListFacillitySefety.setK3CheckListFacillitySefetyDetail(k3CheckListFacillitySefetyDetailSet);
			}
		}
		if(CommonUtil.isNotNullOrEmpty(k3CheckListFacillitySefety)){
			K3CheckListFacillitySefety k3CheckListFacillitySefetyResult = k3CheckListFacillitySefetyDao.save(k3CheckListFacillitySefety);
			result.put("noRec", k3CheckListFacillitySefetyResult.noRec);
		}
		
		return result;
	}
	
	@Override
	public Map<String, Object> getCheckListFasilitasdanKeamanan() {
		Map<String, Object> result = new HashMap<>();
		result.put("data", this.k3CheckListFacillitySefetyDao.getCheckListFasilitasdanKeamanan());
		return result;
	}

	@Override
	public Map<String, Object> getCheckListFasilitasdanKeamananHeader(Integer k3FacillityCheckId, String noRec) {
		Map<String, Object> result = new HashMap<>();

		List<Map<String, Object>> k3KelompokLpjGedungList = k3CheckListFacillitySefetyDao.getK3KelompokLpjGedung(k3FacillityCheckId);
		
		List<Map<String, Object>> listKelompokSet = new ArrayList<>();
		for(Map<String, Object> mapp : k3KelompokLpjGedungList){
			List<Map<String, Object>> k3ItemPeriksaGedungList = k3CheckListFacillitySefetyDao.getCheckListFasilitasdanKeamananDetail((Integer)mapp.get("id"), noRec);
			if(CommonUtil.isNotNullOrEmpty(k3ItemPeriksaGedungList)){
				Map<String, Object> listKelompok = new HashMap<>();
				listKelompok.put(mapp.get("namaKelompok").toString().replaceAll(" ", ""), k3ItemPeriksaGedungList.toArray());
				listKelompokSet.add(listKelompok);
			}
		}
		result.put("listKelompok", listKelompokSet);

		return result;
	}
	

}
