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.EkstensionNoTeleponDao;
import com.jasamedika.medifirst2000.entities.EkstensionNoTelepon;
import com.jasamedika.medifirst2000.entities.Ruangan;
import com.jasamedika.medifirst2000.entities.StatusYaTidak;
import com.jasamedika.medifirst2000.service.EkstensionNoTeleponService;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.vo.EkstensionNoTeleponVO;
import com.jasamedika.medifirst2000.vo.RuanganVO;
import com.jasamedika.medifirst2000.vo.StatusYaTidakVO;

@Service("EkstensionNoTeleponService")
public class EkstensionNoTeleponServiceImpl implements EkstensionNoTeleponService {
	
	@Autowired
	private EkstensionNoTeleponDao ekstensionNoTeleponDao;
	
	@Autowired
	private BaseConverterImpl<EkstensionNoTeleponVO, EkstensionNoTelepon> ekstensionNoTeleponConverter;
	
	@Autowired
	private BaseConverterImpl<RuanganVO, Ruangan> ruanganConverter;
	
	@Autowired
	private BaseConverterImpl<StatusYaTidakVO, StatusYaTidak> statusYaTidakConverter;
	
	@Override
	public Map<String, Object> addEkstension(EkstensionNoTeleponVO vo) {
		Map<String, Object> result = new HashMap<String, Object>();
		EkstensionNoTelepon ekstensionNoTelepon = new EkstensionNoTelepon();
		if(CommonUtil.isNotNullOrEmpty(vo)){
			ekstensionNoTelepon = ekstensionNoTeleponConverter.transferVOToModel(vo, new EkstensionNoTelepon());
		}
		if(CommonUtil.isNotNullOrEmpty(vo.getRuangan())){
			ekstensionNoTelepon.setRuangan(ruanganConverter.transferVOToModel(vo.getRuangan(), new Ruangan()));
		}
		if(CommonUtil.isNotNullOrEmpty(vo.getStatusPelanggan())){
			ekstensionNoTelepon.setStatusPelanggan(statusYaTidakConverter.transferVOToModel(vo.getStatusPelanggan(), new StatusYaTidak()));
		}
		if(CommonUtil.isNotNullOrEmpty(vo.getStatusSemuaRuangan())){
			ekstensionNoTelepon.setStatusSemuaRuangan(statusYaTidakConverter.transferVOToModel(vo.getStatusSemuaRuangan(), new StatusYaTidak()));
		}
		
		EkstensionNoTelepon model = new EkstensionNoTelepon();
		if(CommonUtil.isNotNullOrEmpty(ekstensionNoTelepon)){
			model = ekstensionNoTeleponDao.save(ekstensionNoTelepon);
		}
		if(CommonUtil.isNotNullOrEmpty(model)){
			result.put("data", model.getId());
		}
		return result;
	}

	@Override
	public Map<String, Object> updateEkstension(EkstensionNoTeleponVO vo) {
		Map<String, Object> result = new HashMap<String, Object>();
		EkstensionNoTelepon ekstensionNoTelepon = new EkstensionNoTelepon();
		if(CommonUtil.isNotNullOrEmpty(vo)){
			ekstensionNoTelepon = ekstensionNoTeleponConverter.transferVOToModel(vo, new EkstensionNoTelepon());
		}
		if(CommonUtil.isNotNullOrEmpty(vo.getRuangan())){
			ekstensionNoTelepon.setRuangan(ruanganConverter.transferVOToModel(vo.getRuangan(), new Ruangan()));
		}
		if(CommonUtil.isNotNullOrEmpty(vo.getStatusPelanggan())){
			ekstensionNoTelepon.setStatusPelanggan(statusYaTidakConverter.transferVOToModel(vo.getStatusPelanggan(), new StatusYaTidak()));
		}
		if(CommonUtil.isNotNullOrEmpty(vo.getStatusSemuaRuangan())){
			ekstensionNoTelepon.setStatusSemuaRuangan(statusYaTidakConverter.transferVOToModel(vo.getStatusSemuaRuangan(), new StatusYaTidak()));
		}
		
		EkstensionNoTelepon model = new EkstensionNoTelepon();
		if(CommonUtil.isNotNullOrEmpty(ekstensionNoTelepon)){
			model = ekstensionNoTeleponDao.save(ekstensionNoTelepon);
		}
		if(CommonUtil.isNotNullOrEmpty(model)){
			result.put("data", model.getId());
		}
		return result;
	}

	@Override
	public Map<String, Object> deleteEkstension(Integer id) {
		Map<String, Object> result = new HashMap<String, Object>();
		if(CommonUtil.isNotNullOrEmpty(id)){
			ekstensionNoTeleponDao.delete(id);
			result.put("dataFound", true);
		}
		else{
			result.put("dataFound", false);
		}
		
		return result;
	}

	@Override
	public Map<String, Object> findAll() {
		Map<String, Object> result = new HashMap<String, Object>();
		List<EkstensionNoTelepon> list = new ArrayList<EkstensionNoTelepon>();
		list = (List<EkstensionNoTelepon>) ekstensionNoTeleponDao.findAll();
		if(CommonUtil.isNotNullOrEmpty(list)){
			result.put("data", list);
			result.put("dataFound", true);
		}
		else{
			result.put("dataFound", false);
		}
		return result;
	}
	
	@Override
	public Map<String, Object> findAllTrueEkstension() {
		Map<String, Object> result = new HashMap<String, Object>();
		List<EkstensionNoTelepon> list = new ArrayList<EkstensionNoTelepon>();
		list = (List<EkstensionNoTelepon>) ekstensionNoTeleponDao.findAllTrueEkstension(2);
		if(CommonUtil.isNotNullOrEmpty(list)){
			result.put("data", list);
			result.put("dataFound", true);
		}
		else{
			result.put("dataFound", false);
		}
		return result;
	}

}
