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.DaftarSewaAsramaService;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.vo.SewaAsramaVO;

@Service("daftarSewaAsramaService")
public class DaftarSewaAsramaServiceImpl extends BaseVoServiceImpl implements DaftarSewaAsramaService {

	@Autowired
	private OrderPelayananDao orderPelayananDao;

	@Autowired
	private StrukOrderDao strukOrderDao;

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

		System.out.println("=======START FIND DAFTAR SEWA ASRAMA BY PERIODE=======");
		Map<String, Object> result = new HashMap<String, Object>();
		List<OrderPelayanan> resultModel = orderPelayananDao.findDaftarSewaAsramaByPeriode(periodeAwal, periodeAkhir);
		if (CommonUtil.isNotNullOrEmpty(resultModel)) {
			result.put("data", resultModel);
			result.put("dataFound", true);
		} else {
			result.put("dataFound", false);
		}
		System.out.println("=======END FIND DAFTAR SEWA ASRAMA BY PERIODE=======");
		return result;
	}

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

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

	@Override
	public Map<String, Object> saveStatusSewaAsrama(String noOrder, SewaAsramaVO vo) {

		Map<String, Object> result = new HashMap<String, Object>();
		try {
			System.out.println("==================START SAVE STATUS SEWA ASRAMA======================");
			// set status from TERISI to CHECKOUT
			StrukOrder strukOrder = strukOrderDao.findStatusKamar(noOrder);
			// set status untuk struk order
			if (strukOrder.getStatus().equals("Terisi")) {
				strukOrder.setStatus(Status.CHECKOUT.getVal());
			}

			if (CommonUtil.isNotNullOrEmpty(vo.getStrukOrder())) {
				strukOrder.setTglKeluar(vo.getStrukOrder().getTglKeluar());
			}

			StrukOrder resultModelStrukOrder = strukOrderDao.save(strukOrder);
			result.put("noRecStrukOrder", resultModelStrukOrder.getNoRec());
			System.out.println("==================END SAVE STATUS SEWA ASRAMA======================");
		} catch (Exception e) {
			e.printStackTrace();
		}

		return result;
	}

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

		Map<String, Object> result = new HashMap<String, Object>();
		try {
			System.out.println("==================START GET ALL SEWA ASRAMA======================");
			List<OrderPelayanan> resultModel = orderPelayananDao.getAllSewaAsrama();
			if (CommonUtil.isNotNullOrEmpty(resultModel)) {
				result.put("data", resultModel);
				result.put("dataFound", true);
			} else {
				result.put("dataFound", false);
			}
			System.out.println("==================END GET ALL SEWA ASRAMA======================");
		} catch (Exception e) {
			e.printStackTrace();
		}

		return result;
	}

}
