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.IpsrsPreventiveMaintenanceDao;
import com.jasamedika.medifirst2000.entities.IpsrsPemeriksaanAlat;
import com.jasamedika.medifirst2000.entities.IpsrsPemeriksaan;
import com.jasamedika.medifirst2000.entities.IpsrsPreventiveMaintenance;
import com.jasamedika.medifirst2000.entities.LoginUser;
import com.jasamedika.medifirst2000.entities.Pegawai;
import com.jasamedika.medifirst2000.entities.PengecekanPeralatanMapping;
import com.jasamedika.medifirst2000.entities.RegistrasiAset;
import com.jasamedika.medifirst2000.entities.Rekanan;
import com.jasamedika.medifirst2000.service.IpsrsMaintenanceService;
import com.jasamedika.medifirst2000.service.IpsrsPreventiveMaintenanceService;
import com.jasamedika.medifirst2000.service.LoginUserService;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.util.DateUtil;
import com.jasamedika.medifirst2000.vo.IpsrsPemeriksaanAlatVO;
import com.jasamedika.medifirst2000.vo.IpsrsPemeriksaanVO;
import com.jasamedika.medifirst2000.vo.IpsrsPreventiveMaintenanceVO;
import com.jasamedika.medifirst2000.vo.PegawaiVO;
import com.jasamedika.medifirst2000.vo.PengecekanPeralatanMappingVO;
import com.jasamedika.medifirst2000.vo.RegistrasiAsetVO;
import com.jasamedika.medifirst2000.vo.RekananVO;

@Service("ipsrsPreventiveMaintenanceService")
public class IpsrsPreventiveMaintenanceServiceImpl implements IpsrsPreventiveMaintenanceService{

	@Autowired
	private LoginUserService loginUserService;
	
	@Autowired
	private IpsrsPreventiveMaintenanceDao ipsrsPreventiveMaintenanceDao;
	
	@Autowired
	private IpsrsMaintenanceService ipsrsMaintenanceService;
	
	@Autowired
	private BaseConverterImpl <IpsrsPreventiveMaintenanceVO, IpsrsPreventiveMaintenance> ipsrsPreventiveMaintenanceConverter;
	
	@Autowired
	private BaseConverterImpl <RegistrasiAsetVO, RegistrasiAset> registrasiAsetConverter;
	
	@Autowired
	private BaseConverterImpl <PegawaiVO, Pegawai> pegawaiConverter;
	
	@Autowired
	private BaseConverterImpl <IpsrsPemeriksaanVO, IpsrsPemeriksaan> ipsrsPemeriksaanConverter;
	
	@Autowired
	private BaseConverterImpl <PengecekanPeralatanMappingVO, PengecekanPeralatanMapping> pengecekanPeralatanMappingConverter;
	
	@Autowired
	private BaseConverterImpl <IpsrsPemeriksaanAlatVO, IpsrsPemeriksaanAlat> ipsrsPemeriksaanAlatConverter;
	
	@Autowired
	private BaseConverterImpl <RekananVO, Rekanan> rekananConverter;
	
	@Override
	public Map<String, Object> getUserLogin() {
		Map<String, Object> result = new HashMap<String,Object>();
		LoginUser loginUser = loginUserService.getLoginUser();
		result.put("name", loginUser.getPegawai().getNamaLengkap());
		result.put("idPegawai", loginUser.getPegawai().getId());
		result.put("idRuangan", loginUser.getPegawai().getRuanganId());
		return result;
	}
	
	@Override
	public Map<String, Object> getAsetPreventiveMaintenance(){
		Map<String, Object> result = new HashMap<>();
		List<Map<String, Object>> ipsrsPerbaikanDto = new ArrayList<>();
		for(Map<String, Object> mapping : ipsrsPreventiveMaintenanceDao.getAsetPreventiveMaintenance()){
			if(CommonUtil.isNullOrEmpty(mapping.get("produkId"))){
				mapping.put("produkId", "-");
			} 
			if(CommonUtil.isNullOrEmpty(mapping.get("noSeri"))){
				mapping.put("noSeri", "-");
			} 
			if(CommonUtil.isNullOrEmpty(mapping.get("kdProduk"))){
				mapping.put("kdProduk", "-");
			}
			if(CommonUtil.isNullOrEmpty(mapping.get("namaProduk"))){
				mapping.put("namaProduk", "-");
			} 
			if(CommonUtil.isNullOrEmpty(mapping.get("merkProduk"))){
				mapping.put("merkProduk", "-");
			}
			if(CommonUtil.isNullOrEmpty(mapping.get("typeProduk"))){
				mapping.put("typeProduk", "-");
			}
			ipsrsPerbaikanDto.add(mapping);
		}
		if(CommonUtil.isNotNullOrEmpty(ipsrsPerbaikanDto)){
			result.put("dataAset", ipsrsPerbaikanDto);
		}
		return result;
	}
	
	@Override
	public Map<String, Object> getBagianAlat(Integer produkId){
		Map<String, Object> result = new HashMap<>();
		Integer check = ipsrsPreventiveMaintenanceDao.checkProdukId(produkId);
		if(CommonUtil.isNotNullOrEmpty(check)){
			List<Map<String, Object>> list = ipsrsPreventiveMaintenanceDao.getBagianAlat(check);
			if(CommonUtil.isNotNullOrEmpty(list)){
				List<Map<String, Object>> listResult = new ArrayList<>();
				for(Map<String, Object> mapping : list){
					Map<String, Object> data = new HashMap<>();

					Map<String, Object> bagianAlat = new HashMap<>();
					bagianAlat.put("id", mapping.get("id"));
					bagianAlat.put("bagianAlat", mapping.get("bagianAlat"));
					
					Map<String, Object> fisikFungsi = new HashMap<>();
					fisikFungsi.put("id", 0);
					fisikFungsi.put("nama", "");
					
					data.put("pengecekanPeralatanMapping", bagianAlat);
					data.put("pemeriksaanFisik", fisikFungsi);
					data.put("pemeriksaanFungsi", fisikFungsi);
					listResult.add(data);
				}
				result.put("listBagianAlat", listResult);
			}
		} else{
			List<Map<String, Object>> list = ipsrsPreventiveMaintenanceDao.getBagianAlat();
			if(CommonUtil.isNotNullOrEmpty(list)){
				List<Map<String, Object>> listResult = new ArrayList<>();
				for(Map<String, Object> mapping : list){
					Map<String, Object> data = new HashMap<>();

					Map<String, Object> bagianAlat = new HashMap<>();
					bagianAlat.put("id", mapping.get("id"));
					bagianAlat.put("bagianAlat", mapping.get("bagianAlat"));
					
					Map<String, Object> fisikFungsi = new HashMap<>();
					fisikFungsi.put("id", 0);
					fisikFungsi.put("nama", "");
					
					data.put("pengecekanPeralatanMapping", bagianAlat);
					data.put("pemeriksaanFisik", fisikFungsi);
					data.put("pemeriksaanFungsi", fisikFungsi);
					listResult.add(data);
				}
				result.put("listBagianAlat", listResult);
			}
		}
		return result;
	}
	
	@Override
	public Map<String, Object> getUserVerifikasi(){
		Map<String, Object> result = new HashMap<>();
		List<Map<String, Object>> listData = ipsrsPreventiveMaintenanceDao.getUserVerifikasi();
		if(CommonUtil.isNotNullOrEmpty(listData)){
			result.put("userVerifikasi", listData);
		}
		return result;
	}

	@Override
	public Map<String, Object> savePreventiveMaintenance(IpsrsPreventiveMaintenanceVO vo){
		Map<String, Object> result = new HashMap<>();
		IpsrsPreventiveMaintenance ipsrsPreventiveMaintenance = ipsrsPreventiveMaintenanceConverter.transferVOToModel(vo, new IpsrsPreventiveMaintenance());
		
		if(CommonUtil.isNotNullOrEmpty(vo)){
			ipsrsPreventiveMaintenance.setTglPreventive(DateUtil.now());
		}
		if(CommonUtil.isNotNullOrEmpty(vo.getRegistrasiAset())){
			ipsrsPreventiveMaintenance.setRegistrasiAset(registrasiAsetConverter.transferVOToModel(vo.getRegistrasiAset(), new RegistrasiAset()));
		}
		if(CommonUtil.isNotNullOrEmpty(vo.getPegawai())){
			ipsrsPreventiveMaintenance.setPegawai(pegawaiConverter.transferVOToModel(vo.getPegawai(), new Pegawai()));
		}
		Set<IpsrsPemeriksaan> listIpsrsPemeriksaan = new HashSet<>();
		if(CommonUtil.isNotNullOrEmpty(vo.getIpsrsPemeriksaan())){
			for (IpsrsPemeriksaanVO ipsrsPemeriksaanVO : vo.getIpsrsPemeriksaan()){
				IpsrsPemeriksaan ipsrsPemeriksaan = new IpsrsPemeriksaan();
				if(CommonUtil.isNotNullOrEmpty(ipsrsPemeriksaanVO)){
					ipsrsPemeriksaan = ipsrsPemeriksaanConverter.transferVOToModel(ipsrsPemeriksaanVO, new IpsrsPemeriksaan());
					ipsrsPemeriksaan.setIpsrsPreventiveMaintenance(ipsrsPreventiveMaintenance);
				}
				if(CommonUtil.isNotNullOrEmpty(ipsrsPemeriksaanVO.getPengecekanPeralatanMapping())){
					ipsrsPemeriksaan.setPengecekanPeralatanMapping(pengecekanPeralatanMappingConverter.transferVOToModel(ipsrsPemeriksaanVO.getPengecekanPeralatanMapping(), new PengecekanPeralatanMapping()));
				}
				if(CommonUtil.isNotNullOrEmpty(ipsrsPemeriksaanVO.getPemeriksaanFisik())){
					ipsrsPemeriksaan.setPemeriksaanFisik(ipsrsPemeriksaanAlatConverter.transferVOToModel(ipsrsPemeriksaanVO.getPemeriksaanFisik(), new IpsrsPemeriksaanAlat()));
				}
				if(CommonUtil.isNotNullOrEmpty(ipsrsPemeriksaanVO.getPemeriksaanFungsi())){
					ipsrsPemeriksaan.setPemeriksaanFungsi(ipsrsPemeriksaanAlatConverter.transferVOToModel(ipsrsPemeriksaanVO.getPemeriksaanFungsi(), new IpsrsPemeriksaanAlat()));
				}
				listIpsrsPemeriksaan.add(ipsrsPemeriksaan);
			}
			ipsrsPreventiveMaintenance.setIpsrsPemeriksaan(listIpsrsPemeriksaan);
		}
		if(CommonUtil.isNotNullOrEmpty(vo.getRekanan())){
			ipsrsPreventiveMaintenance.setRekanan(rekananConverter.transferVOToModel(vo.getRekanan(), new Rekanan()));
		}
		if(CommonUtil.isNotNullOrEmpty(ipsrsPreventiveMaintenance)){
			IpsrsPreventiveMaintenance IpsrsPreventiveMaintenanceResult = ipsrsPreventiveMaintenanceDao.save(ipsrsPreventiveMaintenance);
			
			if(CommonUtil.isNotNullOrEmpty(ipsrsPreventiveMaintenance.getJenisPenjadwalan())){
				if(ipsrsPreventiveMaintenance.getJenisPenjadwalan()==1){
					ipsrsMaintenanceService.updateStatusPemeliharaan(ipsrsPreventiveMaintenance.getNoRecPenjadwalan());
				} else if(ipsrsPreventiveMaintenance.getJenisPenjadwalan()==2){
					ipsrsMaintenanceService.updateStatusKontakService(ipsrsPreventiveMaintenance.getNoRecPenjadwalan());
				} else if(ipsrsPreventiveMaintenance.getJenisPenjadwalan()==3){
					ipsrsMaintenanceService.updateStatusKalibrasi(ipsrsPreventiveMaintenance.getNoRecPenjadwalan());
				} 
			}
			
			result.put("noRec", IpsrsPreventiveMaintenanceResult.noRec);
		}
		return result;
	}

}
