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.dao.OrderPelayananDao;
import com.jasamedika.medifirst2000.dao.StrukOrderDao;
import com.jasamedika.medifirst2000.entities.OrderPelayanan;
import com.jasamedika.medifirst2000.entities.StrukOrder;
import com.jasamedika.medifirst2000.enums.Status;
import com.jasamedika.medifirst2000.service.DaftarPesanAmbulanceService;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.util.DateUtil;

@Service("daftarPesanAmbulanceService")
public class DaftarPesanAmbulanceServiceImpl implements DaftarPesanAmbulanceService {

	@Autowired
	private OrderPelayananDao orderPelayananDao;

	@Autowired
	private StrukOrderDao strukOrderDao;

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

		Map<String, Object> result = new HashMap<String, Object>();
		try {
			System.out.println("=======START FIND DAFTAR ORDER AMBULANCE BY NO ORDER=======");
			List<OrderPelayanan> resultModel = orderPelayananDao.findOrderByNoOrder(noOrder);
			if (CommonUtil.isNotNullOrEmpty(resultModel)) {
				result.put("data", resultModel);
				result.put("dataFound", true);
			} else {
				result.put("dataFound", false);
			}
			System.out.println("=======END FIND DAFTAR ORDER AMBULANCE BY NO ORDER=======");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

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

		Map<String, Object> result = new HashMap<String, Object>();
		try {
			System.out.println("=====================START SEARCH DAFTAR ORDER AMBULANCE====================");
			List<OrderPelayanan> resultModel = orderPelayananDao.findDaftarPesanAmbulanceByPeriode(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 AMBULANCE====================");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	@Override
	public Map<String, Object> saveStatusAmbulance(String noOrder, Integer id) {

		Map<String, Object> result = new HashMap<String, Object>();
		try {
			System.out.println("==================START SAVE STATUS AMBULANCE======================");
			// set status ambulance from DIPAKAI to SELESAI DIPAKAI
			StrukOrder strukOrder = strukOrderDao.findStatusDipakai(noOrder);
			if (strukOrder.getStatus().equals("DIPAKAI")) {
				strukOrder.setStatus(Status.SELESAI.getVal());
			}

			// set tgl keluar ambulance
			strukOrder.setTglKeluar(DateUtil.getShortDate());
			StrukOrder resultModelStrukOrder = strukOrderDao.save(strukOrder);
			result.put("noRecStrukOrder", resultModelStrukOrder.getNoRec());
			System.out.println("==================END SAVE STATUS AMBULANCE======================");
		} catch (Exception e) {
			e.printStackTrace();
		}

		return result;
	}

}
