package com.jasamedika.medifirst2000.service.impl;

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.jpa.JpaSystemException;
import org.springframework.stereotype.Service;

import com.jasamedika.medifirst2000.converter.BaseConverterImpl;
import com.jasamedika.medifirst2000.dao.AlatDao;
import com.jasamedika.medifirst2000.dao.DepartemenDao;
import com.jasamedika.medifirst2000.dao.ProdukDao;
import com.jasamedika.medifirst2000.dto.AlatDto;
import com.jasamedika.medifirst2000.entities.Alat;
import com.jasamedika.medifirst2000.entities.Departemen;
import com.jasamedika.medifirst2000.entities.Produk;
import com.jasamedika.medifirst2000.entities.RegistrasiAset;
import com.jasamedika.medifirst2000.entities.SatuanStandar;
import com.jasamedika.medifirst2000.exception.ServiceVOException;
import com.jasamedika.medifirst2000.service.AlatService;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.util.StringUtil;
import com.jasamedika.medifirst2000.vo.AlatVO;

@Service("AlatService")
public class AlatServiceImpl extends BaseVoServiceImpl implements AlatService<AlatVO> {
	
	@Autowired
	private AlatDao alatDao;
	
	@Autowired
	private DepartemenDao departemenDao;

	@Autowired
	private ProdukDao produkDao;

	@Autowired
	private BaseConverterImpl<AlatVO, Alat> alatConverter;
	
	@Override
	public Map<String,Object> addAlat(AlatVO vo) {
		
		Alat alat=new Alat();		
		if(CommonUtil.isNotNullOrEmpty(vo)){
			alat=alatConverter.transferVOToModel(vo, new Alat());
		}
		
		alat.setKdAlat(StringUtil.formatNumber(String.valueOf(alatDao.getCount()+1), 3));
		Alat alatResult=alatDao.save(alat);
		Map<String,Object> result=new HashMap<String,Object>();
		if (CommonUtil.isNotNullOrEmpty(alatResult)) {
			result.put("id", alatResult.getId());
		}
		
		return result;
		
	}
	
	@Override
	public Map<String,Object> updateAlat(AlatVO vo) {
		
		Alat alat=new Alat();		
		if(CommonUtil.isNotNullOrEmpty(vo)){
			alat=alatConverter.transferVOToModel(vo, new Alat());
		}
		
		Alat alatResult=alatDao.save(alat);
		Map<String,Object> result=new HashMap<String,Object>();
		if (CommonUtil.isNotNullOrEmpty(alatResult)) {
			result.put("id", alatResult.getId());
		}
		
		return result;
		
	}
	
	@Override
	public Map<String,Object> findAllAlat() {
		Map<String,Object> result=new HashMap<String,Object>();
		List<Alat> resultModel=(List<Alat>) alatDao.findAll();
		if(CommonUtil.isNotNullOrEmpty(resultModel)){
			result.put("formula", resultModel);
			result.put("dataFound", true);
		}else{
			result.put("dataFound", false);
		}
		return result;
	}
	
	@Override
	public boolean deleteAlat(Integer id) {
		if (CommonUtil.isNotNullOrEmpty(id)){
			Alat jenisLimbahB3Masuk = alatDao.findOne(id);
			alatDao.delete(jenisLimbahB3Masuk);
			return true;
		}else{
			return false;
		}
	}

	@Override
	public AlatVO add(AlatVO vo) throws JpaSystemException, ServiceVOException {
		return null;
	}

	@Override
	public AlatVO update(AlatVO vo) throws JpaSystemException, ServiceVOException {
		return null;
	}

	@Override
	public Boolean delete(Integer key) throws JpaSystemException {
		return null;
	}

	@Override
	public AlatVO findById(Integer key) throws JpaSystemException {
		return null;
	}

	@Override
	public List<AlatVO> findAll() throws JpaSystemException {
		return null;
	}

	@Override
	public Map<String, Object> findAllWithPageAndLimitAndSortByAndDirectionParameter(Integer page, Integer limit,
			String sort, String dir) {
		return null;
	}
	

	@Override
	public List<Map<String, Object>> getAllProdukAset() {
		return alatDao.getAllProdukAset();
	}

	@Override
	public List<Map<String, Object>> getAllSatuanStandar() {
		return alatDao.getAllSatuanStandar();
	}

	@Override
	public Map<String, Object> getDepartemenLaundry() {
		Map<String, Object> map=new HashMap<String, Object>();
		Departemen laundry=departemenDao.findOne(15);
		map.put("id", laundry.getId());
		map.put("namaDepartemen", laundry.getNamaDepartemen());
		return map;
	}

	@Override
	public Map<String, Object> saveAlat(AlatDto vo) {
		Map<String, Object> result = new HashMap<String, Object>();
		Alat alat=new Alat();
		//Alat resultAlat=alatDao.findOne(vo.getAlatId());
		if(vo.getAlatId()!=null)
			alat.setId(vo.getAlatId());
		Departemen departemen=new Departemen();
		departemen.setId(vo.getDepartemenId());
		alat.setDepartemen(departemen);
		SatuanStandar satuanStandar=new SatuanStandar();
		satuanStandar.setId(vo.getSatuanStandarId());
		alat.setSatuanStandar(satuanStandar);
		RegistrasiAset registrasiAset=new RegistrasiAset();
		registrasiAset.setNoRec(vo.getNoRecRegistrasiAset());
		alat.setRegistrasiAset(registrasiAset);
		alat.setKapasitasAlat(vo.getKapasitasMesin());
		Produk produkAset=produkDao.findOne(vo.getProdukAsetId());
		alat.setProdukAset(produkAset);
		alat.setNamaAlat(produkAset.getNamaProduk());
		alat.setStatusEnabled(vo.getStatusEnabled());
		Integer count=alatDao.getCount();
		Alat resultDomain=null;
		List<Map<String,Object>> listMap=listMap=alatDao.findMesinRegisterAset(produkAset.getId());
		if(listMap!=null && !listMap.isEmpty()){
			result.put("findAlat",true);
			if(vo.getAlatId()!=null){
				result.put("idAlat",true);
				List<Map<String,Object>> listMap2=listMap=alatDao.findMesinRegisterAset(produkAset.getId(),vo.getAlatId());
				for(Map<String,Object> mapL2:listMap2){
					Integer id2=(Integer)mapL2.get("id");
					String kdAlat2=(String)mapL2.get("kdAlat");
					alat.setId(id2);
					alat.setKdAlat(kdAlat2);
				}
				resultDomain = alatDao.save(alat);
			}else{
				result.put("idAlat",false);
				result.put("noMesin","");
			}
		}else{
			result.put("findAlat",false);
			result.put("idAlat",false);
			alat.setKdAlat(String.valueOf(count+1));			
			resultDomain = alatDao.save(alat);
		}
		if (CommonUtil.isNotNullOrEmpty(resultDomain)) {
			result.put("noMesin", resultDomain.getId());
		}

		return result;
	}

	@Override
	public Map<String, Object> cekMesiId(Integer produkAsetId,Integer alatId) {
		Map<String, Object> result = new HashMap<String, Object>();
		List<Map<String,Object>> listMap=listMap=alatDao.findMesinRegisterAset(produkAsetId);
		if(listMap!=null && !listMap.isEmpty()){
			result.put("findAlat",true);
			if(alatId!=null){
				result.put("idAlat",true);
			}else{
				result.put("idAlat",false);
			}
		}else{
			result.put("findAlat",false);
			result.put("idAlat",false);
		}
		return result;
	}
	
	@Override
	public Map<String, Object> getByAlatId(Integer alatId) {
		Map<String, Object> map=new HashMap<String,Object>();
		map=alatDao.getAlatById(alatId);
		return map;
	}

	@Override
	public List<Map<String, Object>> getAllAlat() {
		List<Map<String, Object>> list=new ArrayList<Map<String, Object>>();
		list=alatDao.getAllAlat();
		return list;
	}

	@Override
	public Map<String,Object> getMaxId() {
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("noMesin", alatDao.getMaxNoMesin());
		return map;
	}

	@Override
	public Map<String, Object> findAssetLaundry() {
		Map<String, Object> result = new HashMap<>();
		result.put("data", this.alatDao.getAsetLaundry());
		return result;
	}

	@Override
	public Map<String, Object> getMesinLaundry() {
		Map<String, Object> result = new HashMap<>();
		result.put("data", this.alatDao.getMesinLaundry());
		return result;
	}
	
	

}
