package com.jasamedika.medifirst2000.service.impl;

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.ProdukDao;
import com.jasamedika.medifirst2000.dao.RuanganDao;
import com.jasamedika.medifirst2000.dao.SatuanStandarDao;
import com.jasamedika.medifirst2000.dao.StrukPelayananDao;
import com.jasamedika.medifirst2000.dto.PaketDanNonPaketDto;
import com.jasamedika.medifirst2000.entities.AsalProduk;
import com.jasamedika.medifirst2000.entities.LoginUser;
import com.jasamedika.medifirst2000.entities.Paket;
import com.jasamedika.medifirst2000.entities.Produk;
import com.jasamedika.medifirst2000.entities.Ruangan;
import com.jasamedika.medifirst2000.entities.SatuanStandar;
import com.jasamedika.medifirst2000.entities.StrukPelayanan;
import com.jasamedika.medifirst2000.entities.StrukPelayananDetail;
import com.jasamedika.medifirst2000.service.PenerimaanSterilisasiAlatInternalService;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.util.DateUtil;
import com.jasamedika.medifirst2000.vo.AsalProdukVO;
import com.jasamedika.medifirst2000.vo.LoginUserVO;
import com.jasamedika.medifirst2000.vo.PaketVO;
import com.jasamedika.medifirst2000.vo.PenerimaanSterilisasiAlatInternalVO;
import com.jasamedika.medifirst2000.vo.ProdukVO;
import com.jasamedika.medifirst2000.vo.RuanganVO;
import com.jasamedika.medifirst2000.vo.SatuanStandarVO;
import com.jasamedika.medifirst2000.vo.StrukPelayananDetailVO;
import com.jasamedika.medifirst2000.vo.StrukPelayananVO;

@Service("PenerimaanSterilisasiAlatInternalService")
public class PenerimaanSterilisasiAlatInternalServiceImpl implements PenerimaanSterilisasiAlatInternalService {

	@Autowired
	private StrukPelayananDao strukPelayananDao;

	@Autowired
	private SatuanStandarDao satuanStandarDao;

	@Autowired
	private RuanganDao ruanganDao;

	@Autowired
	private ProdukDao produkDao;

	@Autowired
	private BaseConverterImpl<RuanganVO, Ruangan> ruanganConverter;

	@Autowired
	private BaseConverterImpl<StrukPelayananVO, StrukPelayanan> strukPelayananConverter;

	@Autowired
	private BaseConverterImpl<StrukPelayananDetailVO, StrukPelayananDetail> strukPelayananDetailConverter;

	@Autowired
	private BaseConverterImpl<SatuanStandarVO, SatuanStandar> satuanStandarConverter;

	@Autowired
	private BaseConverterImpl<PaketVO, Paket> paketConverter;

	@Autowired
	private BaseConverterImpl<ProdukVO, Produk> produkConverter;

	@Autowired
	private BaseConverterImpl<AsalProdukVO, AsalProduk> asalProdukConverter;

	@Autowired
	private BaseConverterImpl<LoginUserVO, LoginUser> loginUserConverter;

	@Override
	public Map<String, Object> savePenerimaanSterilisasiAlatInternal(PenerimaanSterilisasiAlatInternalVO vo) {

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

		try {
			System.out.println("================START SAVE PENERIMAAN STERILISASI ALAT INTERNAL====================");

			StrukPelayanan strukPelayanan = strukPelayananConverter.transferVOToModel(vo.getStrukPelayanan(),
					new StrukPelayanan());
			// set tgl terima sterilisasi alat
			strukPelayanan.setTglterimakiriman(DateUtil.getShortDate());
			strukPelayanan.setTglstruk(DateUtil.getShortDate());

			// set ruangan penerimaan sterilisasi alat
			if (CommonUtil.isNotNullOrEmpty(vo.getStrukPelayanan().getKdruanganasal())) {
				strukPelayanan.setKdruanganasal(
						ruanganConverter.transferVOToModel(vo.getStrukPelayanan().getKdruanganasal(), new Ruangan()));
			}

			// set ruangan penerimaan sterilisasi alat
			if (CommonUtil.isNotNullOrEmpty(vo.getStrukPelayanan().getKdruangan())) {
				strukPelayanan.setKdruangan(
						ruanganConverter.transferVOToModel(vo.getStrukPelayanan().getKdruangan(), new Ruangan()));
			}

			// get pegawai penerima
			if (CommonUtil.isNotNullOrEmpty(vo.getStrukPelayanan().getKdpegawaipenerima())) {
				//strukPelayanan.setKdpegawaipenerima(loginUserConverter.transferVOToModel(vo.getStrukPelayanan().getKdpegawaipenerima(), new LoginUser()));
			}

			// set struk pelayanan detail
			if (CommonUtil.isNotNullOrEmpty(vo.getStrukPelayanan())) {
				Set<StrukPelayananDetail> listStrukPelayananDetail = new HashSet<StrukPelayananDetail>();
				for (StrukPelayananDetailVO model : vo.getStrukPelayanan().getStrukPelayananDetail()) {
					StrukPelayananDetail strukPelayananDetail = strukPelayananDetailConverter.transferVOToModel(model,
							new StrukPelayananDetail());
					// get paket & non-paket
					if (CommonUtil.isNotNullOrEmpty(model.getKdpaket())) {
						strukPelayananDetail
								.setKdpaket(paketConverter.transferVOToModel(model.getKdpaket(), new Paket()));
					}
					// get satuan
					if (CommonUtil.isNotNullOrEmpty(model.getKdsatuanstandar())) {
						strukPelayananDetail.setKdsatuanstandar(satuanStandarConverter
								.transferVOToModel(model.getKdsatuanstandar(), new SatuanStandar()));
					}
					// set struk pelayanan detail
					strukPelayananDetail.setNostruk(strukPelayanan);
					strukPelayananDetail.setHargadiscount(0.0);
					strukPelayananDetail.setHargadiscountgive(0.0);
					strukPelayananDetail.setHargadiscountsave(0.0);
					strukPelayananDetail.setHarganetto(0.0);
					strukPelayananDetail.setHargapph(0.0);
					strukPelayananDetail.setHargappn(0.0);
					strukPelayananDetail.setHargasatuandijamin(0.0);
					strukPelayananDetail.setHargasatuanppenjamin(0.0);
					strukPelayananDetail.setHargasatuanpprofile(0.0);
					strukPelayananDetail.setHargatambahan(0.0);
					strukPelayananDetail.setQtyorang((byte) 0);
					strukPelayananDetail.setIsonsiteservice((byte) 0);
					strukPelayananDetail.setQtyprodukoutext(0.0);
					strukPelayananDetail.setQtyprodukoutint(0.0);
					strukPelayananDetail.setQtyprodukretur(0.0);
					strukPelayananDetail.setSatuan("");
					strukPelayananDetail.setTglpelayanan(strukPelayanan.getTglstruk());
					strukPelayananDetail.setHargasatuanpprofile(0.0);
					// set jumlah dari non-paket
					strukPelayananDetail.setQtyproduk(model.getQtyproduk());

					// set produk
					if (CommonUtil.isNotNullOrEmpty(model.getKdproduk())) {
						strukPelayananDetail
								.setKdproduk(produkConverter.transferVOToModel(model.getKdproduk(), new Produk()));
					}
					// set asal produk
					if (CommonUtil.isNotNullOrEmpty(model.getKdasalproduk())) {
						strukPelayananDetail.setKdasalproduk(
								asalProdukConverter.transferVOToModel(model.getKdasalproduk(), new AsalProduk()));
					}

					strukPelayananDetail.setNostruk(strukPelayanan);
					listStrukPelayananDetail.add(strukPelayananDetail);
				}
				strukPelayanan.setStrukPelayananDetail(listStrukPelayananDetail);
			}
			StrukPelayanan resultModel = strukPelayananDao.save(strukPelayanan);
			result.put("noRecStrukPelayanan", resultModel.getNoRec());
			System.out.println("================END SAVE PENERIMAAN STERILISASI ALAT INTERNAL====================");
		} catch (Exception e) {
			e.printStackTrace();
		}

		return result;
	}

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

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

		try {
			System.out.println("==================START FIND SATUAN STANDAR======================");
//			List<SatuanStandar> resultModel = satuanStandarDao.findSatuanStandarCssd();
//			if (CommonUtil.isNotNullOrEmpty(resultModel)) {
//				result.put("data", resultModel);
//				result.put("SatuanStandar", true);
//			} else {
//				result.put("SatuanStandar", false);
//			}
			System.out.println("==================END FIND SATUAN STANDAR======================");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

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

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

		try {
			System.out.println("==================START FIND RUANG CSSD======================");
			List<Ruangan> resultModel = ruanganDao.findRuangCssd();
			if (CommonUtil.isNotNullOrEmpty(resultModel)) {
				result.put("data", resultModel);
				result.put("RuangCssd", true);
			} else {
				result.put("RuangCssd", false);
			}
			System.out.println("==================END FIND RUANG CSSD======================");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

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

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

		try {
			System.out.println("==================START FIND PAKET======================");
			List<PaketDanNonPaketDto> resultModel = produkDao.findPaket();
			if (CommonUtil.isNotNullOrEmpty(resultModel)) {
				result.put("data", resultModel);
				result.put("Paket", true);
			} else {
				result.put("Paket", false);
			}
			System.out.println("==================END FIND PAKET======================");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

}
