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.OrderPelayananDao;
import com.jasamedika.medifirst2000.dao.PegawaiDao;
import com.jasamedika.medifirst2000.dao.PemakaianKendaraanDinasDao;
import com.jasamedika.medifirst2000.dao.RegistrasiAsetDao;
import com.jasamedika.medifirst2000.dto.AsetDto;
import com.jasamedika.medifirst2000.entities.OrderPelayanan;
import com.jasamedika.medifirst2000.entities.Pegawai;
import com.jasamedika.medifirst2000.entities.PemakaianKendaraanDinas;
import com.jasamedika.medifirst2000.entities.Produk;
import com.jasamedika.medifirst2000.entities.RegistrasiAset;
import com.jasamedika.medifirst2000.enums.Status;
import com.jasamedika.medifirst2000.exception.ServiceVOException;
import com.jasamedika.medifirst2000.service.PemakaianKendaraanDinasService;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.vo.PegawaiVO;
import com.jasamedika.medifirst2000.vo.PemakaianKendaraanDinasVO;
import com.jasamedika.medifirst2000.vo.ProdukVO;
import com.jasamedika.medifirst2000.vo.RegistrasiAsetVO;

@Service("PemakaianKendaraanDinasService")
public class PemakaianKendaraanDinasServiceImpl implements PemakaianKendaraanDinasService {

	@Autowired
	private PemakaianKendaraanDinasDao pemakaianKendaraanDinasDao;

	@Autowired
	private BaseConverterImpl<PemakaianKendaraanDinasVO, PemakaianKendaraanDinas> pemakaianKendaraanDinasConverter;

	@Autowired
	private RegistrasiAsetDao assetDao;

	@Autowired
	private OrderPelayananDao orderPelayananDao;

	@Autowired
	private PegawaiDao pegawaiDao;

	@Autowired
	private BaseConverterImpl<ProdukVO, Produk> produkConverter;

	@Autowired
	private BaseConverterImpl<PegawaiVO, Pegawai> pegawaiConverter;

	@Autowired
	private BaseConverterImpl<RegistrasiAsetVO, RegistrasiAset> assetConverter;

	@Override
	public Map<String, Object> savePemakaianKendaraanDinas(PemakaianKendaraanDinasVO vo, String noRec)
			throws JpaSystemException, ServiceVOException {

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

		try {
			System.out.println("==================START SAVE PEMAKAIAN KENDARAAN DINAS======================");
			if (CommonUtil.isNotNullOrEmpty(vo.getOrderPelayanan())) {
				OrderPelayanan orderPelayanan = orderPelayananDao.findByNoRecStrukOrder(noRec);
				orderPelayanan.getStrukOrder().setStatus(Status.DIPAKAI.getVal());
				PemakaianKendaraanDinas kendaraanDinas = pemakaianKendaraanDinasConverter.transferVOToModel(vo,
						new PemakaianKendaraanDinas());
				// get produk aset
				if (CommonUtil.isNotNullOrEmpty(vo.getOrderPelayanan().getProdukAset())) {
					orderPelayanan.setProdukAset(
							produkConverter.transferVOToModel(vo.getOrderPelayanan().getProdukAset(), new Produk()));
				}
				// get no registrasi asset & no kendaraan dinas
				if (CommonUtil.isNotNullOrEmpty(vo.getAsset())) {
					kendaraanDinas.setAsset(assetConverter.transferVOToModel(vo.getAsset(), new RegistrasiAset()));
				}

				// get supir
				if (CommonUtil.isNotNullOrEmpty(vo.getSupir())) {
					kendaraanDinas.setSupir(pegawaiConverter.transferVOToModel(vo.getSupir(), new Pegawai()));
				}

				kendaraanDinas.setOrderPelayanan(orderPelayanan);
				PemakaianKendaraanDinas resultModel = pemakaianKendaraanDinasDao.save(kendaraanDinas);
				result.put("noRecPemakaianKendaraanDinas", resultModel.getNoRec());
				OrderPelayanan resultModelOrderPelayanan = orderPelayananDao.save(orderPelayanan);
				result.put("noRecOrderPelayanan", resultModelOrderPelayanan.getNoRec());
			}

			System.out.println("==================END SAVE PEMAKAIAN KENDARAAN DINAS======================");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	@Override
	public List<AsetDto> findAssetKendaraanDinas() {

		List<AsetDto> model = new ArrayList<AsetDto>();
		try {
			System.out.println("==================START FIND ASSET KENDARAAN DINAS======================");
			model = assetDao.findAssetKendaraanDinas();
			System.out.println("==================END FIND ASSET KENDARAAN DINAS======================");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return model;
	}

	@Override
	public List<PegawaiVO> getSupirKendaraanDinas() {

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

}
