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.stereotype.Service;

import com.jasamedika.medifirst2000.converter.BaseConverterImpl;
import com.jasamedika.medifirst2000.dao.MesinDao;
import com.jasamedika.medifirst2000.dao.ProdukDao;
import com.jasamedika.medifirst2000.dao.SatuanStandarDao;
import com.jasamedika.medifirst2000.dto.MesinDto;
import com.jasamedika.medifirst2000.entities.Mesin;
import com.jasamedika.medifirst2000.entities.Produk;
import com.jasamedika.medifirst2000.entities.SatuanStandar;
import com.jasamedika.medifirst2000.service.MesinService;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.vo.MesinVO;
import com.jasamedika.medifirst2000.vo.ProdukVO;
import com.jasamedika.medifirst2000.vo.SatuanStandarVO;

@Service("mesinService")
public class MesinServiceImpl implements MesinService {

	@Autowired
	private MesinDao mesinDao;

	@Autowired
	private ProdukDao produkDao;
	
	@Autowired
	private SatuanStandarDao satuanStandarDao;

	@Autowired
	private BaseConverterImpl<MesinVO, Mesin> mesinConverter;

	@Autowired
	private BaseConverterImpl<ProdukVO, Produk> produkConverter;

	@Autowired
	private BaseConverterImpl<SatuanStandarVO, SatuanStandar> satuanStandarConverter;

	@Override
	public Map<String, Object> saveMesin(MesinVO vo) {

		Map<String, Object> result = new HashMap<String, Object>();

		try {
			System.out.println("===============START SAVE MESIN===============");
			Mesin mesin = mesinConverter.transferVOToModel(vo, new Mesin());
			mesin.setKodeExternal(vo.getKodeExternal());
			mesin.setNamaExternal(vo.getNamaExternal());
			mesin.setStatusEnabled(vo.getStatusEnabled());
			// get Mesin from Produk
			if (CommonUtil.isNotNullOrEmpty(vo.getMesin())) {
				mesin.setMesin(produkConverter.transferVOToModel(vo.getMesin(), new Produk()));
			}
			// get Satuan Standar from Produk
			if (CommonUtil.isNotNullOrEmpty(vo.getSatuan())) {
				mesin.setSatuan(satuanStandarConverter.transferVOToModel(vo.getSatuan(), new SatuanStandar()));
			}
			Mesin resultModel = mesinDao.save(mesin);
			result.put("idMesin", resultModel.getId());
			System.out.println("===============END SAVE MESIN===============");
		} catch (Exception e) {
			e.printStackTrace();
		}

		return result;
	}

	@Override
	public Map<String, Object> findAllMesin() {

		Map<String, Object> result = new HashMap<String, Object>();
		List<Mesin> resultModel = (List<Mesin>) mesinDao.findAll();
		if (CommonUtil.isNotNullOrEmpty(resultModel)) {
			result.put("data", resultModel);
			result.put("dataFound", true);
		} else {
			result.put("dataFound", false);
		}
		return result;
	}
	
	@Override
	public Map<String, Object> findAllMesinLaundry() {

		Map<String, Object> result = new HashMap<String, Object>();
		List<Map<String, Object>> resultModel = this.mesinDao.getMesinLaundry();
		if (CommonUtil.isNotNullOrEmpty(resultModel)) {
			result.put("data", resultModel);
			result.put("dataFound", true);
		} else {
			result.put("dataFound", false);
		}
		return result;
	}

	@Override
	public boolean deleteMesin(Integer id) {

		if (CommonUtil.isNotNullOrEmpty(id)) {
			Mesin mesin = mesinDao.findOne(id);
			mesinDao.delete(mesin);
			return true;
		} else {
			return false;
		}
	}

	@Override
	public List<MesinDto> findMesin() {

		List<MesinDto> model = new ArrayList<MesinDto>();
		try {
			System.out.println("==================START FIND MESIN(LOUNDRY)======================");
			model = produkDao.findMesin();
			System.out.println("==================END FIND MESIN(LOUNDRY)======================");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return model;
	}
	
	@Override
	public Map<String, Object> findSatuanStandar() {

		Map<String, Object> result = new HashMap<String, Object>();

		try {
			System.out.println("==================START FIND SATUAN STANDAR(KG)======================");
			List<SatuanStandar> resultModel = satuanStandarDao.findSatuanStandarKg();
			if (CommonUtil.isNotNullOrEmpty(resultModel)) {
				result.put("data", resultModel);
				result.put("dataFound", true);
			} else {
				result.put("dataFound", false);
			}
			System.out.println("==================END FIND SATUAN STANDAR(KG)======================");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

}
