package com.jasamedika.medifirst2000.service.impl;

import java.text.DecimalFormat;
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.HVADao;
import com.jasamedika.medifirst2000.entities.DetailHVA;
import com.jasamedika.medifirst2000.entities.HVA;
import com.jasamedika.medifirst2000.entities.JenisHVA;
import com.jasamedika.medifirst2000.service.HVAService;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.vo.DetailHVAVO;
import com.jasamedika.medifirst2000.vo.HVAVO;
import com.jasamedika.medifirst2000.vo.JenisHVAVO;

@Service("HVAService")
public class HVAServiceImpl implements HVAService{

	@Autowired
	private HVADao hvaDao;
	
	@Autowired
	private BaseConverterImpl<HVAVO, HVA> hvaConverter;
	
	@Autowired
	private BaseConverterImpl<JenisHVAVO, JenisHVA> jenisHVAConverter;
	
	@Autowired
	private BaseConverterImpl<DetailHVAVO, DetailHVA> detailHVAConverter;

	@Override
	public Map<String,Object> findByJenisPeriode(Long periodeTahun, Integer jenisHVA) {
		Map<String, Object> result = new HashMap<String,Object>();
		Map<String, Object> hva = hvaDao.getHVA(periodeTahun, jenisHVA);
		Map<String, Object> jenisHva = hvaDao.getJenisHVA(periodeTahun, jenisHVA);
		List<Map<String, Object>> detailHva = hvaDao.getDetailHVA(periodeTahun, jenisHVA);
		if(
			CommonUtil.isNotNullOrEmpty(hva) && 
			CommonUtil.isNotNullOrEmpty(jenisHva) && 
			CommonUtil.isNotNullOrEmpty(detailHva)){
			result.put("noRec", hva.get("noRec"));
			result.put("periodeTahun", hva.get("periodeTahun"));
			result.put("jenisHVA", jenisHva);
			result.put("detailHVA", detailHva);
		}
		return result;
	}

	@Override
	public Map<String,Object> saveHVA(HVAVO vo){
		Map<String,Object> result = new HashMap<String,Object>();
		
		HVA hva = hvaConverter.transferVOToModel(vo, new HVA());
		
		if(CommonUtil.isNotNullOrEmpty(vo.getJenisHVA())){
			hva.setJenisHVA(jenisHVAConverter.transferVOToModel(vo.getJenisHVA(), new JenisHVA()));
		}
		if(CommonUtil.isNotNullOrEmpty(vo.getDetailHVA())){
			Set<DetailHVA> detailHVASet = new HashSet<>();
			
			for(DetailHVAVO detailHVAVO : vo.getDetailHVA()){
				DetailHVA detailHVA = detailHVAConverter.transferVOToModel(detailHVAVO, new DetailHVA());
				
				if(CommonUtil.isNotNullOrEmpty(detailHVAVO)){
					detailHVA.setHva(hva);
				}
				if(CommonUtil.isNotNullOrEmpty(detailHVA)){
					detailHVASet.add(detailHVA);
				}
			}
			
			if(CommonUtil.isNotNullOrEmpty(detailHVASet)){
				hva.setDetailHVA(detailHVASet);
			}
		}
		if(CommonUtil.isNotNullOrEmpty(hva)){
			HVA hvaResult = hvaDao.save(hva);
			result.put("noRec", hvaResult.noRec);
		}
		
		return result;
	}
	
	@Override
	public Map<String, Object> getSummaryAnalysis(Long periodeTahun){
		Map<String,Object> result = new HashMap<>();
		
		List<Map<String, Object>> ListResult_1 = new ArrayList<>();
		List<Map<String, Object>> ListResult_2 = new ArrayList<>();
		List<Map<String, Object>> ListResult_3 = new ArrayList<>();
		
		Double TOTAL_PROBABILITY = 0.00;
		Double TOTAL_SEVERITY = 0.00;
		Integer TOTAL_PEMBAGI_PROBABILITY = 0;
		Integer TOTAL_PEMBAGI_SEVERITY = 0;
		
		for(int i=1; i<=4; i++){
			String TYPE = "";
			if(i==1){TYPE="Natural";
			} else if(i==2){TYPE="Technological";
			} else if(i==3){TYPE="Human";
			} else if(i==4){TYPE="Hazmat";
			}
			
			List<Map<String, Object>> listData = hvaDao.getDetailHVA(periodeTahun, i);
			if(CommonUtil.isNotNullOrEmpty(listData)){
				try{
					Double pro = 0.00;
					Double sev = 0.00;
					for(Map<String, Object> data : listData){
						pro = pro+((Integer)data.get("probability"));
						sev = sev+(Integer)data.get("humanImpact")
								 +(Integer)data.get("propertyImpact")
								 +(Integer)data.get("businessImpact")
								 +(Integer)data.get("preparedness")
								 +(Integer)data.get("internalResponse")
								 +(Integer)data.get("externalResponse");
					}
					Integer PEMBAGI_PROBABILITY = listData.size()*3;
					Integer PEMBAGI_SEVERITY = (listData.size()*3)*6;
					TOTAL_PEMBAGI_PROBABILITY = TOTAL_PEMBAGI_PROBABILITY+PEMBAGI_PROBABILITY;
					TOTAL_PEMBAGI_SEVERITY = TOTAL_PEMBAGI_SEVERITY+PEMBAGI_SEVERITY;
					
					Double PROBABILITY = pro/PEMBAGI_PROBABILITY;
					Double SEVERITY = sev/PEMBAGI_SEVERITY;
					Double RISK = PROBABILITY*SEVERITY;
					
					Map<String, Object> map1 = new HashMap<>();
					DecimalFormat df = new DecimalFormat("#.##");
					map1.put("jenis", TYPE);
					map1.put("probability", df.format(PROBABILITY));
					map1.put("severity", df.format(SEVERITY));
					map1.put("risk", df.format(RISK));
					ListResult_1.add(map1);

					Map<String, Object> map2 = new HashMap<>();
					map2.put("axis_x", TYPE);
					map2.put("axis_y", df.format(RISK));
					ListResult_2.add(map2);
					
					TOTAL_PROBABILITY = TOTAL_PROBABILITY+PROBABILITY;
					TOTAL_SEVERITY =  TOTAL_SEVERITY+SEVERITY;
				} catch(Exception ex){
					ex.printStackTrace();
				}
			} else if(CommonUtil.isNullOrEmpty(listData)){
				Map<String, Object> map = new HashMap<>();
				map.put("jenis", TYPE);
				map.put("probability", 0.00);
				map.put("severity", 0.00);
				map.put("risk", 0.00);
				ListResult_1.add(map);
			}
		}
		
		if(TOTAL_PROBABILITY!=0.00 || TOTAL_SEVERITY!=0.00){
			Double FACILITY_PROBABILITY = TOTAL_PROBABILITY/TOTAL_PEMBAGI_PROBABILITY;
			Double FACILITY_SEVERITY = TOTAL_SEVERITY/TOTAL_PEMBAGI_SEVERITY;
			Double FACILITY_RISK = FACILITY_PROBABILITY * FACILITY_SEVERITY;
			
			Map<String, Object> map1 = new HashMap<>();
			DecimalFormat df = new DecimalFormat("#.##");
			map1.put("jenis", "totalForFacility");
			map1.put("probability", df.format(FACILITY_PROBABILITY));
			map1.put("severity", df.format(FACILITY_SEVERITY));
			map1.put("risk", df.format(FACILITY_RISK));
			ListResult_1.add(map1);
			
			Map<String, Object> map2 = new HashMap<>();
			map2.put("axis_x", "Probability");
			map2.put("axis_y", df.format(FACILITY_PROBABILITY));
			ListResult_3.add(map2);
			
			Map<String, Object> map3 = new HashMap<>();
			map3.put("axis_x", "Severity");
			map3.put("axis_y", df.format(FACILITY_SEVERITY));
			ListResult_3.add(map3);
		}
		
		if(CommonUtil.isNotNullOrEmpty(ListResult_1)){
			result.put("summaryOfMedicalHazardsAnalysis", ListResult_1);
			result.put("hazardSpecificRelativeRiskToMedical", ListResult_2);
			result.put("probabilitySeverityHazardsToMedical", ListResult_3);
		}
		
		return result;
	}
}
