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.dao.OrderPelayananDao;
import com.jasamedika.medifirst2000.dao.PengembalianKendaraanDinasDao;
import com.jasamedika.medifirst2000.dto.PemakaianKendaraanDinasDto;
import com.jasamedika.medifirst2000.dto.PengembalianKendaraanDinasDto;
import com.jasamedika.medifirst2000.entities.OrderPelayanan;
import com.jasamedika.medifirst2000.entities.PengembalianKendaraanDinas;
import com.jasamedika.medifirst2000.service.DaftarKendaraanDinasService;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.util.DateUtil;

@Service("daftarKendaraanDinasService")
public class DaftarKendaraanDinasServiceImpl implements DaftarKendaraanDinasService {

	@Autowired
	private OrderPelayananDao orderPelayananDao;

	@Autowired
	private PengembalianKendaraanDinasDao pengembalianKendaraanDinasDao;

	@Override
	public Map<String, Object> findDaftarKendaraanDinasByPeriode(String periodeAwal, String periodeAkhir) {

		Map<String, Object> result = new HashMap<String, Object>();
		try {
			System.out.println("=====================START SEARCH DAFTAR ORDER KENDARAAN DINAS====================");
			List<OrderPelayanan> resultModel = orderPelayananDao.findDaftarKendaraanDinasByPeriode(periodeAwal,
					periodeAkhir);
			if (CommonUtil.isNotNullOrEmpty(resultModel)) {
				result.put("data", resultModel);
				result.put("dataFound", true);
			} else {
				result.put("dataFound", false);
			}
			System.out.println("=====================END SEARCH DAFTAR ORDER KENDARAAN DINAS====================");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

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

		Map<String, Object> resultList = new HashMap<String, Object>();
		try {
			System.out.println("=======START FIND DAFTAR PEMAKAIAN KENDARAAN DINAS BY NO REC=======");
			PemakaianKendaraanDinasDto dto = new PemakaianKendaraanDinasDto();
			List<Map<String, Object>> resultMap = orderPelayananDao.findPemakaianKendaraanDinasByNoRec(noRec);
			for (int i = 0; i < resultMap.size(); i++) {
				dto.setNoRec(resultMap.get(i).get("noRec").toString());
				dto.setNoOrder(resultMap.get(i).get("noOrder").toString());
				dto.setTglPelayananAwal(DateUtil.toDate(resultMap.get(i).get("tglKeberangkatanAwal").toString()));
				dto.setTglPelayananAkhir(DateUtil.toDate(resultMap.get(i).get("tglKeberangkatanAkhir").toString()));
				dto.setUnitPemesanId(Integer.parseInt(resultMap.get(i).get("unitPemesanId").toString()));
				dto.setUnitPemesan(resultMap.get(i).get("unitPemesan").toString());
				dto.setAlamatTempatTujuan(resultMap.get(i).get("tujuan").toString());
			}
			dto.setListPegawai(new ArrayList<Map<String, Object>>());
			for (Map<String, Object> petugas : resultMap) {
				Map<String, Object> pegawai = new HashMap<String, Object>();
				pegawai.put("id", petugas.get("petugasBerangkatId").toString());
				pegawai.put("nama", petugas.get("petugasBerangkat").toString());
				dto.getListPegawai().add(pegawai);
			}
			resultList.put("data", dto);
			System.out.println("=======END FIND DAFTAR PEMAKAIAN KENDARAAN DINAS BY NO REC=======");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return resultList;
	}

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

		Map<String, Object> resultList = new HashMap<String, Object>();
		try {
			System.out.println("=======START FIND DAFTAR PENGEMBALIAN KENDARAAN DINAS BY NO ORDER=======");
			PengembalianKendaraanDinasDto dto = new PengembalianKendaraanDinasDto();
			List<Map<String, Object>> resultMap = pengembalianKendaraanDinasDao
					.findPengembalianKendaraanDinasByNoRec(noRec);
			for (int i = 0; i < resultMap.size(); i++) {
				dto.setNoRec(resultMap.get(i).get("noRec").toString());
				dto.setNoOrder(resultMap.get(i).get("noOrder").toString());
				dto.setNamaProdukId(Integer.parseInt(resultMap.get(i).get("namaProdukId").toString()));
				dto.setNamaProduk(resultMap.get(i).get("namaProduk").toString());
				dto.setNoRegisterAset(resultMap.get(i).get("noRegisAset").toString());
				dto.setNoPolisi(resultMap.get(i).get("noPolisi").toString());
				dto.setTglPelayananAwal(DateUtil.toDate(resultMap.get(i).get("tglKeberangkatan").toString()));
				dto.setNamaRuanganId(Integer.parseInt(resultMap.get(i).get("namaRuanganId").toString()));
				dto.setNamaRuangan(resultMap.get(i).get("namaRuangan").toString());
				dto.setAlamatTempatTujuan(resultMap.get(i).get("tujuan").toString());
				dto.setSupirId(Integer.parseInt(resultMap.get(i).get("supirId").toString()));
				dto.setSupir(resultMap.get(i).get("supir").toString());
			}
			dto.setListPetugas(new ArrayList<Map<String, Object>>());
			for (Map<String, Object> petugas : resultMap) {
				Map<String, Object> pegawai = new HashMap<String, Object>();
				pegawai.put("id", petugas.get("petugasBerangkatId").toString());
				pegawai.put("nama", petugas.get("petugasBerangkat").toString());
				dto.getListPetugas().add(pegawai);
			}
			resultList.put("data", dto);
			System.out.println("=======END FIND DAFTAR PENGEMBALIAN KENDARAAN DINAS BY NO ORDER=======");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return resultList;
	}

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

		Map<String, Object> result = new HashMap<String, Object>();
		try {
			System.out.println("==================START GET ALL KENDARAAN DINAS & PEJABAT======================");
			List<PengembalianKendaraanDinas> resultModel = pengembalianKendaraanDinasDao
					.getAllKendaraanDinasDanPejabat();
			if (CommonUtil.isNotNullOrEmpty(resultModel)) {
				result.put("data", resultModel);
				result.put("dataFound", true);
			} else {
				result.put("dataFound", false);
			}
			System.out.println("==================END GET ALL KENDARAAN DINAS & PEJABAT======================");
		} catch (Exception e) {
			e.printStackTrace();
		}

		return result;
	}

	@Override
	public Map<String, Object> getPenggunaanKendaranDinasByNoRec(String noRec) {
		Map<String, Object> result = new HashMap<>();
		
		List<Map<String, Object>> data = new ArrayList<>();
		List<Map<String, Object>> listData = this.orderPelayananDao.getPemakaianKendaraanDinasByNoRec(noRec);
		
		for(Map<String, Object>item:listData){
			if(CommonUtil.isNotNullOrEmpty(item.get("noRec"))){
				item.put("pegawai", this.orderPelayananDao.getPemakaianKendaraanDinasByPetugasNoRec((String)item.get("noRec")));
			}
			data.add(item);
		}
		
		result.put("data", data);
		return result;
	}

	@Override
	public Map<String, Object> getPengembalianKendaraanDinasByNoRec(String noRec) {
		Map<String, Object> result = new HashMap<>();
		
		List<Map<String, Object>> data  = new ArrayList<>();
		List<Map<String, Object>> listData = this.pengembalianKendaraanDinasDao.getPengembalianKendaraanDinasByNoRec(noRec);
		
		for(Map<String, Object>item:listData){
			item.put("pegawai", this.orderPelayananDao.getPemakaianKendaraanDinasByPetugasNoRec((String)item.get("noRec")));
			data.add(item);
		}
		result.put("data", data);
		
		return result;
	}

	@Override
	public Map<String, Object> getAllPengembalianKendaraanDinas(String startDate, String endDate) {
		Map<String, Object> result = new HashMap<>();
		List<Map<String, Object>> list = this.pengembalianKendaraanDinasDao.getAllPengembalianKendaraanDinas(startDate, endDate);
		result.put("data", list);
		return result;
	}

	@Override
	public Map<String, Object> getPengembalianKendaraanDetailByNoRec(String noRec) {
		Map<String, Object> result = new HashMap<>();
		
		List<Map<String, Object>> data = new ArrayList<>();
		List<Map<String, Object>> list = this.pengembalianKendaraanDinasDao.getDetailPengembalianKendaraanDinasbyNoRec(noRec);
		
		for(Map<String, Object>item:list){
			item.put("permintaan", this.pengembalianKendaraanDinasDao.getPengembalianKendaraanDinasByNoRec((String)item.get("noRec")));
			item.put("petugas", this.orderPelayananDao.getPemakaianKendaraanDinasByPetugasNoRec((String)item.get("noRec")));
			data.add(item);
		}
		
		result.put("data", data);
		
		return result;
	}
	
	
	
	
}
