package com.jasamedika.medifirst2000.service.impl;

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.OrderPelayananConverter;
import com.jasamedika.medifirst2000.converter.PapRencanaOrderLaboratoriumConverter;
import com.jasamedika.medifirst2000.converter.ProdukConverter;
import com.jasamedika.medifirst2000.converter.StrukOrderConverter;
import com.jasamedika.medifirst2000.dao.OrderPelayananDao;
import com.jasamedika.medifirst2000.dao.PapRencanaOrderLaboratoriumDao;
import com.jasamedika.medifirst2000.dao.ProdukDao;
import com.jasamedika.medifirst2000.dao.StrukOrderDao;
import com.jasamedika.medifirst2000.entities.OrderPelayanan;
import com.jasamedika.medifirst2000.entities.PapRencanaOrderLaboratorium;
import com.jasamedika.medifirst2000.entities.Produk;
import com.jasamedika.medifirst2000.entities.Ruangan;
import com.jasamedika.medifirst2000.entities.StrukOrder;
import com.jasamedika.medifirst2000.exception.ServiceVOException;
import com.jasamedika.medifirst2000.service.PapRencanaOrderLaboratoriumService;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.vo.OrderPelayananVO;
import com.jasamedika.medifirst2000.vo.PapRencanaOrderLaboratoriumVO;
import com.jasamedika.medifirst2000.vo.ProdukVO;

/**
 * Implement class for PapRencanaOrderLaboratoriumService
 * 
 * @author Askur
 */
@Service("PapRencanaOrderLaboratoriumService")
public class PapRencanaOrderLaboratoriumServiceImpl implements PapRencanaOrderLaboratoriumService {
	@Autowired
	private PapRencanaOrderLaboratoriumDao papRencanaOrderLaboratoriumDao;
	@Autowired
	private PapRencanaOrderLaboratoriumConverter papRencanaOrderLaboratoriumConverter;
	@Autowired
	private OrderPelayananDao orderPelayananDao;
	@Autowired
	private OrderPelayananConverter orderPelayananConverter;
	@Autowired
	private ProdukDao produkDao;
	@Autowired
	private ProdukConverter produkConverter;
	@Autowired
	private StrukOrderDao strukOrderDao;
	@Autowired
	private StrukOrderConverter strukOrderConverter;
	
	@Override
	public Map<String, Object> addRencanaOrderLaboratorium(PapRencanaOrderLaboratoriumVO vo) throws JpaSystemException, ServiceVOException {
		Map<String,Object> result=new HashMap<String,Object>();
		PapRencanaOrderLaboratorium model=papRencanaOrderLaboratoriumConverter.transferVOToModel(vo, new PapRencanaOrderLaboratorium());
		StrukOrder strukOrder=new StrukOrder();
		strukOrder.setNoOrder(String.valueOf(strukOrderDao.getCountByMonthAndYear(new Date())+1));
		strukOrder.setTglOrder(new Date());
		strukOrder.setIsDelivered((byte)1);
		strukOrder.setQtyJenisProduk(1.0);
		strukOrder.setQtyProduk(1.0);
		strukOrder.setTotalBeaMaterai(0.0);
		strukOrder.setTotalBiayaKirim(0.0);
		strukOrder.setTotalBiayaTambahan(0.0);
		strukOrder.setTotalHargaSatuan(0.0);
		strukOrder.setTotalDiscount(0.0);
		strukOrder.setTotalHarusDibayar(0.0);
		strukOrder.setTotalPph(0.0);
		strukOrder.setTotalPpn(0.0);
		strukOrder.setKeteranganOrder("Permintaan Laboratorium");
		StrukOrder strukOrderSave=strukOrderDao.save(strukOrder);
		Ruangan ruanganTujuan=new Ruangan();
		ruanganTujuan.setId(1);
		Ruangan ruangan=new Ruangan();
		ruangan.setId(1);
		for(ProdukVO produk:vo.getProdukSet()){
			Produk pro=produkConverter.transferVOToModel(produk, new Produk());
			produkDao.save(pro);
			OrderPelayanan orderPelayanan=new OrderPelayanan();
			orderPelayanan.setRuanganTujuan(ruanganTujuan);
			orderPelayanan.setRuangan(ruangan);
			orderPelayanan.setProduk(pro);
			orderPelayanan.setNoOrder(strukOrderSave);
			orderPelayanan.setIsCito((byte)1);
			orderPelayanan.setQtyProduk(1.0);
			orderPelayanan.setQtyProdukRetur(1.0);
			orderPelayanan.setTglPelayanan(new Date());
			orderPelayananDao.save(orderPelayanan);
		}
		model.setStrukOrder(strukOrderSave);
		PapRencanaOrderLaboratorium resultModel=papRencanaOrderLaboratoriumDao.save(model);
		if(CommonUtil.isNotNullOrEmpty(resultModel)){
			result.put("noRec", resultModel.getNoRec());
			result.put("tglInput", resultModel.getTglInput());
			if(CommonUtil.isNotNullOrEmpty(resultModel.getPasien().getId())){
				result.put("pasien.noCm", resultModel.getPasien().getNoCm());
			}
		}
		return result;
	}
	
	@Override
	public Map<String, Object> updateRencanaOrderLaboratorium(PapRencanaOrderLaboratoriumVO vo) throws JpaSystemException, ServiceVOException {
		Map<String,Object> result=new HashMap<String,Object>();
		PapRencanaOrderLaboratorium model=papRencanaOrderLaboratoriumDao.findOne(vo.getNoRec());
		PapRencanaOrderLaboratorium resultModel=papRencanaOrderLaboratoriumDao.save(model);
		if(CommonUtil.isNotNullOrEmpty(resultModel)){
			result.put("noRec", resultModel.getNoRec());
			result.put("tglInput", resultModel.getTglInput());
			result.put("datafound", true);
			if(CommonUtil.isNotNullOrEmpty(resultModel.getPasien().getId())){
				result.put("pasien.noCm", resultModel.getPasien().getNoCm());
			}
		}else{
			result.put("datafound", false);
		}
		return result;
	}

	@Override
	public Map<String,Object> findByNoCmAndTglInput(String noCm, Date tglInput) {
		Map<String,Object> result=new HashMap<String,Object>();
		PapRencanaOrderLaboratorium resultModel=papRencanaOrderLaboratoriumDao.findPapRencanaOrderLaboratoriumByNoCmAndTglInput(noCm, tglInput);
		if(CommonUtil.isNotNullOrEmpty(resultModel)){
			result.put("noRec", resultModel.getNoRec());
			result.put("tglInput", resultModel.getTglInput());
			result.put("datafound", true);
			if(CommonUtil.isNotNullOrEmpty(resultModel.getPasien().getId())){
				result.put("pasien.noCm", resultModel.getPasien().getNoCm());
			}
		}else{
			result.put("datafound", false);
		}
		return result;
	}

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

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

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

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

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

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



}
