package com.jasamedika.medifirst2000.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.codec.binary.Base64;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jasamedika.medifirst2000.converter.BaseConverterImpl;
import com.jasamedika.medifirst2000.dao.IpsrsDataAlatDao;
import com.jasamedika.medifirst2000.dao.IpsrsPerbaikanDao;
import com.jasamedika.medifirst2000.dao.IpsrsSertifikatKalibrasiDao;
import com.jasamedika.medifirst2000.entities.IpsrsSertifikatKalibrasi;
import com.jasamedika.medifirst2000.service.IpsrsDataAlatService;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.util.DateUtil;
import com.jasamedika.medifirst2000.vo.IpsrsSertifikatKalibrasiVO;

@Service("ipsrsDataAlatService")
public class IpsrsDataAlatServiceImpl implements IpsrsDataAlatService{
	
	@Autowired
	private IpsrsDataAlatDao ipsrsDataAlatDao;
	
	@Autowired
	private IpsrsSertifikatKalibrasiDao ipsrsSertifikatKalibrasiDao;
	
	@Autowired
	private IpsrsPerbaikanDao ipsrsPerbaikanDao;
	
	@Autowired
	private BaseConverterImpl<IpsrsSertifikatKalibrasiVO, IpsrsSertifikatKalibrasi> ipsrsSertifikatKalibrasiConverter;
	
	@Override
	public Map<String, Object> getAllRegistrasiAset(){
		Map<String, Object> result = new HashMap<>();
		List<Map<String, Object>> listData = ipsrsDataAlatDao.getAllRegistrasiAset();
		if(CommonUtil.isNotNullOrEmpty(listData)){
			result.put("listRegistrasiAset", listData);
		}
		return result;
	}
	
	@Override
	public Map<String, Object> getOneRegistrasiAset(String noRec){
		Map<String, Object> result = new HashMap<>();
		Map<String, Object> data = ipsrsDataAlatDao.getOneRegistrasiAset(noRec);
		if(CommonUtil.isNotNullOrEmpty(data)){
			result.put("registrasiAset", data);
		}
		return result;
	}
	
	@Override
	public Map<String, Object> getHistoryAlat(String noRec){
		Map<String, Object> result = new HashMap<>();
		List<Map<String, Object>> listData = ipsrsPerbaikanDao.getHistoryAlat(noRec);
		if(CommonUtil.isNotNullOrEmpty(listData)){
			result.put("listData", listData);
		}
		return result;
	}
	
	@Override
	public Map<String, Object> uploadSertifikatKalibrasi(IpsrsSertifikatKalibrasiVO vo){
		Map<String, Object> result = new HashMap<>();
		IpsrsSertifikatKalibrasi ipsrsSertifikatKalibrasi = ipsrsSertifikatKalibrasiConverter.transferVOToModel(vo, new IpsrsSertifikatKalibrasi());
		
		if(CommonUtil.isNotNullOrEmpty(vo.getFileUpload())){
			try{
				byte[] decodedBytes = Base64.decodeBase64(vo.getFileUpload());
				ipsrsSertifikatKalibrasi.setFile(decodedBytes);
			}catch (Exception ex){
				ex.printStackTrace();
			}
		}
		if(CommonUtil.isNotNullOrEmpty(vo)){
			IpsrsSertifikatKalibrasi ipsrsSertifikatKalibrasiResult = ipsrsSertifikatKalibrasiDao.save(ipsrsSertifikatKalibrasi);
			result.put("id", ipsrsSertifikatKalibrasiResult.getId());
		}
		return result;
	}
	
	@Override
	public Map<String, Object> getSertifikatKalibrasi(String noRec){
		Map<String, Object> result = new HashMap<>();
		List<Map<String, Object>> listData = ipsrsSertifikatKalibrasiDao.getSertifikatKalibrasi(noRec);
		if(CommonUtil.isNotNullOrEmpty(listData)){
			result.put("listData", listData);
		}
		return result;
	}
	
	@Override
	public Map<String, Object> downloadSertifikatKalibrasi(Integer id){
		if(CommonUtil.isNotNullOrEmpty(id)){
			return ipsrsSertifikatKalibrasiDao.downloadSertifikatKalibrasi(id);
		} else {
			return null;
		}
	}

	@Override
	public Map<String, Object> getAllJenisProdukByIpsrs() {
		Map<String, Object> result = new HashMap<>();
		List<Map<String, Object>> listData = this.ipsrsDataAlatDao.getJenisProdukByIpsrs();
		if(CommonUtil.isNotNullOrEmpty(listData)){
			result.put("listData", listData);
		}
		return result;
	}
	
	
}
