package com.jasamedika.medifirst2000.service.impl;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
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.JadwalSupirAmbulanceDao;
import com.jasamedika.medifirst2000.dao.PegawaiDao;
import com.jasamedika.medifirst2000.entities.JadwalSupirAmbulance;
import com.jasamedika.medifirst2000.entities.Pegawai;
import com.jasamedika.medifirst2000.entities.Ruangan;
import com.jasamedika.medifirst2000.entities.ShiftKerja;
import com.jasamedika.medifirst2000.exception.ServiceVOException;
import com.jasamedika.medifirst2000.service.JadwalSupirAmbulanceService;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.vo.JadwalSupirAmbulanceVO;
import com.jasamedika.medifirst2000.vo.PegawaiVO;
import com.jasamedika.medifirst2000.vo.RuanganVO;
import com.jasamedika.medifirst2000.vo.ShiftKerjaVO;

@Service
public class JadwalSupirAmbulanceServiceImpl extends BaseVoServiceImpl implements JadwalSupirAmbulanceService {
	@Autowired
	private JadwalSupirAmbulanceDao jadwalSupirAmbulanceDao;

	@Autowired
	private PegawaiDao pegawaiDao;

	@Autowired
	private BaseConverterImpl<JadwalSupirAmbulanceVO, JadwalSupirAmbulance> jadwalSupirAmbulanceConverter;

	@Autowired
	private BaseConverterImpl<PegawaiVO, Pegawai> pegawaiConverter;

	@Autowired
	private BaseConverterImpl<ShiftKerjaVO, ShiftKerja> shiftKerjaConverter;

	@Autowired
	private BaseConverterImpl<RuanganVO, Ruangan> ruanganConverter;

	@Override
	public Map<String, Object> addJadwalSupirAmbulance(JadwalSupirAmbulanceVO vo) {
		JadwalSupirAmbulance jadwalSupirAmbulance = new JadwalSupirAmbulance();
		if (CommonUtil.isNotNullOrEmpty(vo)) {
			jadwalSupirAmbulance = jadwalSupirAmbulanceConverter.transferVOToModel(vo, new JadwalSupirAmbulance());
		}
		if (CommonUtil.isNotNullOrEmpty(vo.getPegawai())) {
			jadwalSupirAmbulance.setPegawai(pegawaiConverter.transferVOToModel(vo.getPegawai(), new Pegawai()));
		}
		if (CommonUtil.isNotNullOrEmpty(vo.getShiftKerja())) {
			jadwalSupirAmbulance
					.setShiftKerja(shiftKerjaConverter.transferVOToModel(vo.getShiftKerja(), new ShiftKerja()));
		}
		if (CommonUtil.isNotNullOrEmpty(vo.getRuangan())) {
			jadwalSupirAmbulance.setRuangan(ruanganConverter.transferVOToModel(vo.getRuangan(), new Ruangan()));
		}

		JadwalSupirAmbulance resultModel = jadwalSupirAmbulanceDao.save(jadwalSupirAmbulance);

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

		if (CommonUtil.isNotNullOrEmpty(resultModel)) {
			result.put("noRec", resultModel.getNoRec());
		}

		return result;
	}

	@Override
	public Map<String, Object> updateJadwalSupirAmbulance(JadwalSupirAmbulanceVO vo) {
		JadwalSupirAmbulance jadwalSupirAmbulance = new JadwalSupirAmbulance();
		if (CommonUtil.isNotNullOrEmpty(vo)) {
			jadwalSupirAmbulance = jadwalSupirAmbulanceConverter.transferVOToModel(vo, new JadwalSupirAmbulance());
		}
		if (CommonUtil.isNotNullOrEmpty(vo.getPegawai())) {
			jadwalSupirAmbulance.setPegawai(pegawaiConverter.transferVOToModel(vo.getPegawai(), new Pegawai()));
		}
		if (CommonUtil.isNotNullOrEmpty(vo.getShiftKerja())) {
			jadwalSupirAmbulance
					.setShiftKerja(shiftKerjaConverter.transferVOToModel(vo.getShiftKerja(), new ShiftKerja()));
		}
		if (CommonUtil.isNotNullOrEmpty(vo.getRuangan())) {
			jadwalSupirAmbulance.setRuangan(ruanganConverter.transferVOToModel(vo.getRuangan(), new Ruangan()));
		}

		JadwalSupirAmbulance resultModel = jadwalSupirAmbulanceDao.save(jadwalSupirAmbulance);

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

		if (CommonUtil.isNotNullOrEmpty(resultModel)) {
			result.put("noRec", resultModel.getNoRec());
		}

		return result;
	}

	@Override
	public Map<String, Object> saveAllJadwalSupirAmbulance(List<JadwalSupirAmbulanceVO> vos) {
		Map<String, Object> result = new HashMap<String, Object>();
		List<JadwalSupirAmbulance> listResult = new ArrayList<JadwalSupirAmbulance>();
		if (CommonUtil.isNotNullOrEmpty(vos)) {
			for (JadwalSupirAmbulanceVO vo : vos) {
				JadwalSupirAmbulance jadwalSupirAmbulance = new JadwalSupirAmbulance();
				if (CommonUtil.isNotNullOrEmpty(vo)) {
					jadwalSupirAmbulance = jadwalSupirAmbulanceConverter.transferVOToModel(vo,
							new JadwalSupirAmbulance());
				}
				if (CommonUtil.isNotNullOrEmpty(vo.getPegawai())) {
					jadwalSupirAmbulance.setPegawai(pegawaiConverter.transferVOToModel(vo.getPegawai(), new Pegawai()));
				}
				if (CommonUtil.isNotNullOrEmpty(vo.getShiftKerja())) {
					jadwalSupirAmbulance
							.setShiftKerja(shiftKerjaConverter.transferVOToModel(vo.getShiftKerja(), new ShiftKerja()));
				}
				if (CommonUtil.isNotNullOrEmpty(vo.getRuangan())) {
					jadwalSupirAmbulance.setRuangan(ruanganConverter.transferVOToModel(vo.getRuangan(), new Ruangan()));
				}

				JadwalSupirAmbulance resultModel = jadwalSupirAmbulanceDao.save(jadwalSupirAmbulance);

				listResult.add(resultModel);
			}
		}
		if (CommonUtil.isNotNullOrEmpty(listResult)) {
			result.put("model", listResult);
		}
		return result;
	}

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

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

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

	@Override
	public JadwalSupirAmbulanceVO findById(String key) throws JpaSystemException {
		return null;
	}

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

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

	@Override
	public Map<String, Object> findAllJadwalSupirAmbulance() {
		Map<String, Object> result = new HashMap<String, Object>();

		List<JadwalSupirAmbulance> list = new ArrayList<JadwalSupirAmbulance>();
		list = (List<JadwalSupirAmbulance>) jadwalSupirAmbulanceDao.findAll();

		if (CommonUtil.isNotNullOrEmpty(list)) {
			result.put("data", list);
			result.put("dataFound", true);
		} else {
			result.put("dataFound", false);
		}
		return result;
	}

	@Override
	public List<PegawaiVO> getSupirAmbulance() {

		List<PegawaiVO> model = new ArrayList<PegawaiVO>();
		try {
			System.out.println("==================START GET SUPIR AMBULANCE======================");
			model = pegawaiDao.getSupirAmbulance();
			System.out.println("==================END GET SUPIR AMBULANCE======================");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return model;
	}

	@Override
	public Map<String, Object> findJadwalByTanggal(Integer bulan, Integer tahun) {
		List<JadwalSupirAmbulance> list = new ArrayList<JadwalSupirAmbulance>();
		Date fromDate = new Date();
		Date toDate = new Date();

		Calendar calendar = Calendar.getInstance();
		calendar.clear();
		calendar.set(Calendar.YEAR, tahun);
		calendar.set(Calendar.MONTH, bulan);
		fromDate = calendar.getTime();

		calendar = Calendar.getInstance();
		calendar.clear();
		calendar.set(Calendar.YEAR, tahun);
		calendar.set(Calendar.MONTH, bulan + 1);
		toDate = calendar.getTime();

		list = jadwalSupirAmbulanceDao.getJadwalSupirAmbulanceByDate(fromDate, toDate);

		System.out.println("==================================================================================");
		System.out.println(fromDate.toString());
		System.out.println(toDate.toString());

		Map<String, Object> result = new HashMap<String, Object>();
		if (CommonUtil.isNotNullOrEmpty(list)) {
			result.put("data", list);
			result.put("dataFound", true);
		} else {
			result.put("dataFound", false);
		}

		return result;
	}
}
