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.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.jasamedika.medifirst2000.converter.BaseConverterImpl;
import com.jasamedika.medifirst2000.dao.PlanningHistoriPelayananCsDao;
import com.jasamedika.medifirst2000.dao.StrukPlanningDao;
import com.jasamedika.medifirst2000.entities.HistoriPelayananCs;
import com.jasamedika.medifirst2000.entities.JenisRespon;
import com.jasamedika.medifirst2000.entities.Komunikasi;
import com.jasamedika.medifirst2000.entities.Pasien;
import com.jasamedika.medifirst2000.entities.Pegawai;
import com.jasamedika.medifirst2000.entities.PlanningHistoriPelayananCs;
import com.jasamedika.medifirst2000.entities.Produk;
import com.jasamedika.medifirst2000.entities.Ruangan;
import com.jasamedika.medifirst2000.entities.StrukPlanning;
import com.jasamedika.medifirst2000.service.PlanningHistoriPelayananCsService;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.util.DateUtil;
import com.jasamedika.medifirst2000.vo.HistoriPelayananCsVO;
import com.jasamedika.medifirst2000.vo.JenisResponVO;
import com.jasamedika.medifirst2000.vo.KomunikasiVO;
import com.jasamedika.medifirst2000.vo.PasienVO;
import com.jasamedika.medifirst2000.vo.PegawaiVO;
import com.jasamedika.medifirst2000.vo.PlanningHistoriPelayananCsVO;
import com.jasamedika.medifirst2000.vo.ProdukVO;
import com.jasamedika.medifirst2000.vo.RuanganVO;
import com.jasamedika.medifirst2000.vo.StrukPlanningVO;

@Service(" PlanningHistoriPelayananCsService")
public class PlanningHistoriPelayananCsServiceImpl implements PlanningHistoriPelayananCsService {
	@Autowired
	private PlanningHistoriPelayananCsDao planningHistoriPelayananCsDao;
	
	@Autowired
	private StrukPlanningDao strukPlanningDao;

	@Autowired
	private BaseConverterImpl<PlanningHistoriPelayananCsVO, PlanningHistoriPelayananCs> planningHistoriPelayananCsConverter;

	@Autowired
	private BaseConverterImpl<PegawaiVO, Pegawai> pegawaiConverter;

	@Autowired
	private BaseConverterImpl<JenisResponVO, JenisRespon> jenisResponConverter;

	@Autowired
	private BaseConverterImpl<KomunikasiVO, Komunikasi> komunikasiConverter;
	
	@Autowired
	private BaseConverterImpl<StrukPlanningVO, StrukPlanning> strukPlanningConverter;

	@Autowired
	private BaseConverterImpl<RuanganVO, Ruangan> ruanganConverter;
	
	@Autowired
	private BaseConverterImpl<ProdukVO, Produk> produkConverter;
	
	@Autowired
	private BaseConverterImpl<PasienVO, Pasien> pasienConverter;
	
	@Override
	public Map<String, Object> savePlanningHistriPelayananCs(PlanningHistoriPelayananCsVO vo) {
		Map<String, Object> result = new HashMap<>();
		PlanningHistoriPelayananCs planningHistoriCs = new PlanningHistoriPelayananCs();
		
		String noPlanning = noPlanning();
		Date dateNow = DateUtil.now();
		
		if (CommonUtil.isNotNullOrEmpty(vo)) {
			planningHistoriCs = planningHistoriPelayananCsConverter.transferVOToModel(vo, new PlanningHistoriPelayananCs());
			planningHistoriCs.setNoPlanning(noPlanning);
			planningHistoriCs.setNoCM(pasienConverter.transferVOToModel(vo.getNoCM(), new Pasien()));
		}

		if (CommonUtil.isNotNullOrEmpty(vo.getPegawaiPlanning()) && vo.getPegawaiPlanning().getId() != 0) {
			planningHistoriCs.setPegawaiPlanning(
					pegawaiConverter.transferVOToModel(vo.getPegawaiPlanning(), new Pegawai()));
		}

		if (CommonUtil.isNotNullOrEmpty(vo.getJenisResponPlanning())
				&& vo.getJenisResponPlanning().getId() != 0) {
			planningHistoriCs.setJenisResponPlanning(
					jenisResponConverter.transferVOToModel(vo.getJenisResponPlanning(), new JenisRespon()));
		}
		
		if (CommonUtil.isNotNullOrEmpty(vo.getKomunikasiPlanning()) 
				&& vo.getKomunikasiPlanning().getId() != 0) {
			planningHistoriCs.setKomunikasiPlanning(
					komunikasiConverter.transferVOToModel(vo.getKomunikasiPlanning(), new Komunikasi()));
		}
		
		if (CommonUtil.isNotNullOrEmpty(vo.getTglPlanning())) {
			planningHistoriCs.setTglPlanning(vo.getTglPlanning());
		}
		
		if (CommonUtil.isNotNullOrEmpty(vo.getKdProduk())) {
			planningHistoriCs.setKdProduk(produkConverter.transferVOToModel(vo.getKdProduk(), new Produk()));
		}
		
		PlanningHistoriPelayananCs planningHistoriCsResult = this.planningHistoriPelayananCsDao
				.save(planningHistoriCs);
		result.put("NoRecPlanning", planningHistoriCsResult.noRec);

			if (CommonUtil.isNotNullOrEmpty(vo.getStrukPlanning())) {
				
				StrukPlanning strukPlanning = strukPlanningConverter.transferVOToModel(
						vo.getStrukPlanning(), new StrukPlanning());

					strukPlanning.setNoplanning(noPlanning);

					strukPlanning.setTglpegajuan(dateNow);
					
					strukPlanning.setTglplanning(vo.getTglPlanning());
					
					strukPlanning.setKdpegawaipjawab(pegawaiConverter.transferVOToModel(
							vo.getStrukPlanning().getKdpegawaipjawab(), new Pegawai()));
					
					strukPlanning.setKdruangan(ruanganConverter.transferVOToModel(
							vo.getStrukPlanning().getKdruangan(), new Ruangan()));
					
					strukPlanning.setKdruanganasal(ruanganConverter.transferVOToModel(
							vo.getStrukPlanning().getKdruanganasal(), new Ruangan()));
					
					StrukPlanning strukPlanningResult = this.strukPlanningDao.save(strukPlanning);
					result.put("noRecStruk", strukPlanningResult.noRec);
				
			}

		return result;
	}

	@Override
	public String noPlanning() {
		Long count = this.planningHistoriPelayananCsDao.countNoPlanningHistori("PP");

		String monthNow = String.valueOf(DateUtil.getMonthFromDate(new Date()));
		String yearNow = String.valueOf(DateUtil.getYearFromDate(new Date())).substring(2, 4);
		String digit = null;

		count++;
		if (count.toString().length() == 1) {
			digit = "000";
		} else if (count.toString().length() == 2) {
			digit = "00";
		} else if (count.toString().length() == 3) {
			digit = "0";
		}
		if (monthNow.length() == 1)
			monthNow = "0" + monthNow;

		return "PP" + yearNow + monthNow + digit + count;
	}

	@Override
	public Map<String, Object> noDefault() {
		Map<String, Object> result = new HashMap<>();

		List<String> list = this.planningHistoriPelayananCsDao.noPlanningHistori("PP");
		if (CommonUtil.isNotNullOrEmpty(list)) {
			Integer data = Integer.parseInt(list.get(list.size() - 1).substring(2)) + 1;
			result.put("noPlanning", "PP" + data);
		} else {
			result.put("noPlanning", noPlanning());
		}
		return result;
	}

	@Override
	public Map<String, Object> getListPlanningHistoriPelayananCs() {
		Map<String, Object> result = new HashMap<>();

		List<Map<String, Object>> listData = this.planningHistoriPelayananCsDao
				.getListPlanningHistoriPelayananCs();

		if (CommonUtil.isNotNullOrEmpty(listData)) {
			result.put("listData", listData);
		}
		return result;
	}

	
	@Override
	public Map<String, Object> getLoadData(Integer ruanganId) {
		Map<String, Object> result = new HashMap<>();

		Map<String, Object> noPlanning = noDefault();
		if (CommonUtil.isNotNullOrEmpty(noPlanning)) {
			result.put("noPlanning", noPlanning.get("noPlanning"));
		}
		
		return result;
	}
	
	@Override
	public Map<String, Object> getProdukPlanningHistori() {
		Map<String, Object> result = new HashMap<>();
		
		Pageable topTen = new PageRequest(0, 1);
		
		List<Map<String, Object>> data = this.planningHistoriPelayananCsDao.getProdukPlanningHistori(topTen);
		
		if (CommonUtil.isNotNullOrEmpty(data)) {
			result.put("data", data);
		}
		
		return result;
	}

}
