package com.jasamedika.medifirst2000.service.impl;

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.JenisLinenDao;
import com.jasamedika.medifirst2000.entities.JenisLinen;
import com.jasamedika.medifirst2000.service.JenisLinenService;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.vo.JenisLinenVO;

@Service("jenisLinenService")
public class JenisLinenServiceImpl implements JenisLinenService {

	@Autowired
	private JenisLinenDao jenisLinenDao;

	@Autowired
	private BaseConverterImpl<JenisLinenVO, JenisLinen> jenisLinenConverter;

	@Override
	public Map<String, Object> saveJenisLinen(JenisLinenVO vo) {

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

		try {
			System.out.println("==================START SAVE JENIS LINEN======================");

			JenisLinen jenisLinen = jenisLinenConverter.transferVOToModel(vo, new JenisLinen());
			jenisLinen.setKdJenisLinen(generateKodeJenisLinen());
			jenisLinen.setKodeExternal(vo.getKodeExternal());
			jenisLinen.setNamaExternal(vo.getNamaExternal());
			jenisLinen.setStatusEnabled(vo.getStatusEnabled());
			JenisLinen resultModel = jenisLinenDao.save(jenisLinen);
			result.put("id Jenis Linen", resultModel.getId());
			System.out.println("==================END SAVE JENIS LINEN======================");
		} catch (Exception e) {
			e.printStackTrace();
		}

		return result;
	}

	@Override
	public Map<String, Object> updateJenisLinen(JenisLinenVO vo) {

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

		try {
			System.out.println("==================START UPDATE JENIS LINEN======================");

			JenisLinen jenisLinen = jenisLinenConverter.transferVOToModel(vo, new JenisLinen());
			jenisLinen.getKodeExternal();
			jenisLinen.getNamaExternal();
			jenisLinen.getStatusEnabled();

			JenisLinen resultModel = jenisLinenDao.save(jenisLinen);
			result.put("id", resultModel.getId());
			System.out.println("==================END UPDATE JENIS LINEN======================");
		} catch (Exception e) {
			e.printStackTrace();
		}

		return result;
	}

	@Override
	public String generateKodeJenisLinen() {

		Integer countByKodeJenisLinen = 0;
		String kdJenisLinen = null;

		countByKodeJenisLinen = jenisLinenDao.getCountByKdJenisLinen();
		countByKodeJenisLinen++;

		if (CommonUtil.isNotNullOrEmpty(countByKodeJenisLinen)) {
			kdJenisLinen = "000" + countByKodeJenisLinen.toString();

		}

		return kdJenisLinen;
	}

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

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

	@Override
	public boolean deleteJenisLinen(Integer id) {

		if (CommonUtil.isNotNullOrEmpty(id)) {
			JenisLinen jenisLinen = jenisLinenDao.findOne(id);
			jenisLinenDao.delete(jenisLinen);
			return true;
		} else {
			return false;
		}
	}

}
