package com.jasamedika.medifirst2000.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jasamedika.medifirst2000.converter.BaseConverterImpl;
import com.jasamedika.medifirst2000.dao.KelompokTransaksiDao;
import com.jasamedika.medifirst2000.dao.OrderPelayananDao;
import com.jasamedika.medifirst2000.dao.PegawaiDao;
import com.jasamedika.medifirst2000.dao.ProdukDao;
import com.jasamedika.medifirst2000.dao.RegistrasiAsetDao;
import com.jasamedika.medifirst2000.dao.RuanganDao;
import com.jasamedika.medifirst2000.dao.StrukOrderDao;
import com.jasamedika.medifirst2000.dto.AsetDto;
import com.jasamedika.medifirst2000.dto.OrderAmbulanceDto;
import com.jasamedika.medifirst2000.entities.KelompokTransaksi;
import com.jasamedika.medifirst2000.entities.MappingPegawaiPemakaianAmbulance;
import com.jasamedika.medifirst2000.entities.OrderPelayanan;
import com.jasamedika.medifirst2000.entities.Pegawai;
import com.jasamedika.medifirst2000.entities.Produk;
import com.jasamedika.medifirst2000.entities.StrukOrder;
import com.jasamedika.medifirst2000.enums.Status;
import com.jasamedika.medifirst2000.service.OrderAmbulanceService;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.util.DateUtil;
import com.jasamedika.medifirst2000.vo.KelompokTransaksiVO;
import com.jasamedika.medifirst2000.vo.MappingPegawaiPemakaianAmbulanceVO;
import com.jasamedika.medifirst2000.vo.OrderAmbulanceVO;
import com.jasamedika.medifirst2000.vo.OrderPelayananVO;
import com.jasamedika.medifirst2000.vo.PegawaiVO;
import com.jasamedika.medifirst2000.vo.ProdukVO;

@Service("orderAmbulanceService")
public class OrderAmbulanceServiceImpl extends BaseVoServiceImpl implements OrderAmbulanceService {

	@Autowired
	private StrukOrderDao strukOrderDao;

	@Autowired
	private OrderPelayananDao orderPelayananDao;

	@Autowired
	private ProdukDao produkDao;

	@Autowired
	private RegistrasiAsetDao registrasiAssetDao;

	@Autowired
	private PegawaiDao pegawaiDao;

	@Autowired
	private RuanganDao ruanganDao;

	@Autowired
	private KelompokTransaksiDao kelompokTransaksiDao;

	@Autowired
	private BaseConverterImpl<OrderPelayananVO, OrderPelayanan> orderPelayananConverter;

	@Autowired
	private BaseConverterImpl<PegawaiVO, Pegawai> petugasConverter;

	@Autowired
	private BaseConverterImpl<MappingPegawaiPemakaianAmbulanceVO, MappingPegawaiPemakaianAmbulance> mappingPegawaiAmbulanceConverter;

	@Autowired
	private BaseConverterImpl<ProdukVO, Produk> produkConverter;

	@Autowired
	private BaseConverterImpl<KelompokTransaksiVO, KelompokTransaksi> kelompokTransaksiConverter;

	@Override
	public Map<String, Object> saveOrderAmbulance(OrderAmbulanceVO vo, String noRec) {

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

		try {
			System.out.println("==========START SAVE PEMAKAIAN AMBULANCE==========");
			if (CommonUtil.isNotNullOrEmpty(vo.getStrukOrder())) {
				System.out.println(":::START STRUK ORDER:::");
				StrukOrder strukOrder = strukOrderDao.findByNoRec(noRec);
				strukOrder.setKeteranganOrder("Pemakaian Ambulance");
				strukOrder.setNamaPJawabKeluarga(vo.getStrukOrder().getNamaPJawabKeluarga());
				strukOrder.setAlamatTempatTujuan(vo.getStrukOrder().getAlamatTempatTujuan());
				strukOrder.setKeteranganLainnya(vo.getStrukOrder().getKeteranganLainnya());
				strukOrder.setNamaTempatTujuan(vo.getStrukOrder().getNamaTempatTujuan());
				// set kelompok transaksi
				if (CommonUtil.isNotNullOrEmpty(vo.getStrukOrder().getKelompokTransaksi())) {
					strukOrder.setKelompokTransaksi(kelompokTransaksiConverter
							.transferVOToModel(vo.getStrukOrder().getKelompokTransaksi(), new KelompokTransaksi()));
					strukOrder.setKelompokTransaksi(kelompokTransaksiDao.findById(
							Integer.parseInt((String) this.GetSettingDataFixed("KdKelompokTransaksiAmbulance"))));
				}
				// get supir
				if (CommonUtil.isNotNullOrEmpty(vo.getStrukOrder().getPetugas())) {
					strukOrder.setPetugas(
							petugasConverter.transferVOToModel(vo.getStrukOrder().getPetugas(), new Pegawai()));
				}
				// get nama perawat
				if (CommonUtil.isNotNullOrEmpty(vo.getStrukOrder().getMapPegawaiAmbulance())) {
					Set<MappingPegawaiPemakaianAmbulance> listMapPegawaiAmbulance = new HashSet<MappingPegawaiPemakaianAmbulance>();
					for (MappingPegawaiPemakaianAmbulanceVO modelMapPegawaiAmbulance : vo.getStrukOrder()
							.getMapPegawaiAmbulance()) {
						MappingPegawaiPemakaianAmbulance mappingPegawaiAmbulance = mappingPegawaiAmbulanceConverter
								.transferVOToModel(modelMapPegawaiAmbulance, new MappingPegawaiPemakaianAmbulance());
						if (CommonUtil.isNotNullOrEmpty(modelMapPegawaiAmbulance.getPerawat())) {
							mappingPegawaiAmbulance.setPerawat(petugasConverter
									.transferVOToModel(modelMapPegawaiAmbulance.getPerawat(), new Pegawai()));
						}
						mappingPegawaiAmbulance.setStrukOrder(strukOrder);
						listMapPegawaiAmbulance.add(mappingPegawaiAmbulance);
					}
					strukOrder.setMapPegawaiAmbulance(listMapPegawaiAmbulance);
				}
				strukOrder.setStatus(Status.DIPAKAI.getVal());
				strukOrder.setTotalHargaSatuan(vo.getStrukOrder().getTotalHargaSatuan());
				strukOrder.setTotalHarusDibayar(vo.getStrukOrder().getTotalHarusDibayar());
				StrukOrder resultModel = strukOrderDao.save(strukOrder);
				result.put("noRecStrukOrder", resultModel.getNoRec());
				System.out.println(":::END STRUK ORDER:::");
				if (CommonUtil.isNotNullOrEmpty(vo.getOrderPelayanan())) {
					System.out.println(":::START ORDER PELAYANAN:::");
					List<OrderPelayanan> listOrderPelayanan = orderPelayananDao.findByNoRec(noRec);
					OrderPelayanan orderPelayanan = orderPelayananConverter.transferVOToModel(vo.getOrderPelayanan(),
							new OrderPelayanan());
					for (int i = 0; i < listOrderPelayanan.size(); i++) {
						orderPelayanan.setProduk(
								produkConverter.transferVOToModel(vo.getOrderPelayanan().getProduk(), new Produk()));
						orderPelayanan.setProdukAset(produkConverter
								.transferVOToModel(vo.getOrderPelayanan().getProdukAset(), new Produk()));
						orderPelayanan.setNoOrder(listOrderPelayanan.get(i).getNoOrder());
						orderPelayanan.setStrukOrder(listOrderPelayanan.get(i).getStrukOrder());
						orderPelayanan.setRuangan(listOrderPelayanan.get(i).getRuangan());
						orderPelayanan.setRuanganTujuan(listOrderPelayanan.get(i).getRuanganTujuan());
						orderPelayanan.setRuangan(ruanganDao.findById(
								Integer.parseInt((String) this.GetSettingDataFixed("KdRuanganAsetAmbulance"))));
						orderPelayanan.setRuanganTujuan(ruanganDao.findById(
								Integer.parseInt((String) this.GetSettingDataFixed("KdRuanganTujuanAmbulance"))));
						orderPelayanan.setIsCito(listOrderPelayanan.get(i).getIsCito());
						orderPelayanan.setQtyProduk(listOrderPelayanan.get(i).getQtyProduk());
						orderPelayanan.setQtyProdukRetur(listOrderPelayanan.get(i).getQtyProdukRetur());
						orderPelayanan.setTglPelayanan(DateUtil.getShortDate());
						orderPelayanan.getProduk().setNilaiNormal((short) 1);
						orderPelayanan.getProduk().setqProduk(1);
						orderPelayanan.getProduk().setQtyJualTerkecil(1.0);
						orderPelayanan.getProduk().setQtySatuKemasan(1);
						orderPelayanan.getProduk().setQtyTerkecil(1.0);
					}
					OrderPelayanan resultModelOrderPelayanan = orderPelayananDao.save(orderPelayanan);
					result.put("noRecOrderPelayanan", resultModelOrderPelayanan.getNoRec());
					System.out.println(":::END ORDER PELAYANAN:::");
				}
			}
			System.out.println("==========END SAVE PEMAKAIAN AMBULANCE==========");
		} catch (Exception e) {
			e.printStackTrace();
		}

		return result;
	}

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

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

	@Override
	public List<OrderAmbulanceDto> findProdukAmbulance() {

		List<OrderAmbulanceDto> model = new ArrayList<OrderAmbulanceDto>();
		try {
			System.out.println("==========START FIND PRODUK(AMBULANCE)==========");
			model = produkDao.findProdukAmbulance();
			System.out.println("==========END FIND PRODUK(AMBULANCE)==========");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return model;
	}

	@Override
	public List<AsetDto> findAssetAmbulance() {

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

	@Override
	public List<PegawaiVO> getSupirAmbulance() {

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

}
