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.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jasamedika.medifirst2000.converter.BaseConverterImpl;
import com.jasamedika.medifirst2000.dao.HistoriPelayananCsDao;
import com.jasamedika.medifirst2000.dao.StrukHistoriDao;
import com.jasamedika.medifirst2000.entities.Alamat;
import com.jasamedika.medifirst2000.entities.HistoriPelayananCs;
import com.jasamedika.medifirst2000.entities.JenisKelamin;
import com.jasamedika.medifirst2000.entities.JenisRespon;
import com.jasamedika.medifirst2000.entities.KelompokProduk;
import com.jasamedika.medifirst2000.entities.KelompokTransaksi;
import com.jasamedika.medifirst2000.entities.Komunikasi;
import com.jasamedika.medifirst2000.entities.Pasien;
import com.jasamedika.medifirst2000.entities.Pegawai;
import com.jasamedika.medifirst2000.entities.PlanningPegawaiMuSaRe;
import com.jasamedika.medifirst2000.entities.Ruangan;
import com.jasamedika.medifirst2000.entities.StrukHistori;
import com.jasamedika.medifirst2000.entities.StrukPlanning;
import com.jasamedika.medifirst2000.service.HistoriPelayananCsService;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.util.DateUtil;
import com.jasamedika.medifirst2000.util.JsonUtil;
import com.jasamedika.medifirst2000.vo.AlamatVO;
import com.jasamedika.medifirst2000.vo.HistoriPelayananCsVO;
import com.jasamedika.medifirst2000.vo.JenisKelaminVO;
import com.jasamedika.medifirst2000.vo.JenisResponVO;
import com.jasamedika.medifirst2000.vo.KelompokProdukVO;
import com.jasamedika.medifirst2000.vo.KelompokTransaksiVO;
import com.jasamedika.medifirst2000.vo.KomunikasiVO;
import com.jasamedika.medifirst2000.vo.PasienVO;
import com.jasamedika.medifirst2000.vo.PegawaiVO;
import com.jasamedika.medifirst2000.vo.RuanganVO;
import com.jasamedika.medifirst2000.vo.StrukHistoriVO;
import com.jasamedika.medifirst2000.vo.StrukPlanningVO;


@Service("HistoriPelayananCsService")
public class HistoriPelayananCsServiceImpl implements HistoriPelayananCsService {

	@Autowired
	private HistoriPelayananCsDao historiPelayananCsDao;
	
	@Autowired
	private StrukHistoriDao strukHistoriDao;
	
	@Autowired
	private BaseConverterImpl<HistoriPelayananCsVO, HistoriPelayananCs> historiPelayananCsConverter;
	
	@Autowired
	private BaseConverterImpl<StrukHistoriVO, StrukHistori> strukHistoriConverter;
	
	@Autowired
	private BaseConverterImpl<PegawaiVO, Pegawai> pegawaiConverter;
	
	@Autowired
	private BaseConverterImpl<RuanganVO, Ruangan> ruanganConverter;
	
	@Autowired
	private BaseConverterImpl<JenisResponVO, JenisRespon> jenisResponConverter;
	
	@Autowired
	private BaseConverterImpl<KomunikasiVO, Komunikasi> komunikasiConverter;
	
	@Autowired
	private BaseConverterImpl<JenisKelaminVO, JenisKelamin> jenisKelaminConverter;
	
	@Autowired
	private BaseConverterImpl<PasienVO, Pasien> pasienConverter;
	
	@Autowired
	private BaseConverterImpl<KelompokTransaksiVO, KelompokTransaksi> transaksiConveter;
	
	@Autowired
	private BaseConverterImpl<AlamatVO, Alamat> alamatConveter;
	
	@Autowired
	private BaseConverterImpl<KelompokProdukVO, KelompokProduk> kelompokProdukConveter;

	@Override
	public String noHistori() {
		Long count = historiPelayananCsDao.countNoHistori("HP");
		
		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 "HP" + yearNow + monthNow + digit + count;
	}
	
	@Override
	public Map<String, Object> noDefault() {
		Map<String, Object> result = new HashMap<>();
		
		List<String> list = this.historiPelayananCsDao.noHistori("HP");
		if(CommonUtil.isNotNullOrEmpty(list)){
			Integer data = Integer.parseInt(list.get(list.size()-1).substring(2))+1;
			result.put("noHistori", "HP" + data);
		} else{
			result.put("noHistori", noHistori());
		}
		return result;
	}
	
	
	public Map<String, Object> saveStrukHistori(StrukHistoriVO shvo) {
		Map<String, Object> result = new HashMap<>();
		
		StrukHistori strukHistori = new StrukHistori();
		
		if(CommonUtil.isNotNullOrEmpty(shvo)) {
			strukHistori = strukHistoriConverter.transferVOToModel(shvo, new StrukHistori());
		}
		
		if(CommonUtil.isNotNullOrEmpty(strukHistori)){
			StrukHistori strukHistoriResult = strukHistoriDao.save(strukHistori);
			result.put("noRecStrukHistori", strukHistoriResult.noRec);
		}
		return result;
	}
	
	@Override
	public Map<String, Object> saveHistriPelayananCs(HistoriPelayananCsVO hpcsvo){
		Map<String, Object> result = new HashMap<>();
		
		HistoriPelayananCs historiPelayananCs = new HistoriPelayananCs();
		historiPelayananCs = historiPelayananCsConverter.transferVOToModel(hpcsvo, historiPelayananCs);
		
		//HistoriPelayananCs historiPelayananCsStatus = historiPelayananCsConverter.transferVOToModel(hpcsvo, new HistoriPelayananCs());
		String noHistori = noHistori();
		Date dateNow = DateUtil.now();
		
		if(CommonUtil.isNotNullOrEmpty(hpcsvo)){
			historiPelayananCs.setNoHistori(noHistori);
		}
		
		if(CommonUtil.isNotNullOrEmpty(hpcsvo.getPegawaiTujuan()) && hpcsvo.getPegawaiTujuan().getId() != 0)  {
			historiPelayananCs.setPegawaiTujuan(pegawaiConverter.transferVOToModel(hpcsvo.getPegawaiTujuan(), new Pegawai()));
		}
		
		if(CommonUtil.isNotNullOrEmpty(hpcsvo.getRuanganTujuan()) && hpcsvo.getRuanganTujuan().getId() != 0) {
			historiPelayananCs.setRuanganTujuan(ruanganConverter.transferVOToModel(hpcsvo.getRuanganTujuan(), new Ruangan()));
		}
		
		if(CommonUtil.isNotNullOrEmpty(hpcsvo.getJenisRespon()) && hpcsvo.getJenisRespon().getId() != 0) {
			historiPelayananCs.setJenisRespon(jenisResponConverter.transferVOToModel(hpcsvo.getJenisRespon(), new JenisRespon()));
		}
		
		if(CommonUtil.isNotNullOrEmpty(hpcsvo.getKomunikasi()) && hpcsvo.getKomunikasi().getId() != 0) {
			historiPelayananCs.setKomunikasi(komunikasiConverter.transferVOToModel(hpcsvo.getKomunikasi(), new Komunikasi()));
		}
		
		if(CommonUtil.isNotNullOrEmpty(hpcsvo.getTglPelayananAwal())) {
			historiPelayananCs.setTglPelayananAwal(hpcsvo.getTglPelayananAwal());
		}
		
		if(CommonUtil.isNotNullOrEmpty(hpcsvo.getKelamin()) && hpcsvo.getKelamin().getId() != 0) {
			historiPelayananCs.setKelamin(jenisKelaminConverter.transferVOToModel(hpcsvo.getKelamin(), new JenisKelamin()));
		}
		
		if(CommonUtil.isNotNullOrEmpty(hpcsvo.getNoCM())) {
			historiPelayananCs.setNoCM(pasienConverter.transferVOToModel(hpcsvo.getNoCM(), new Pasien()));
		}
		//historiPelayananCs.setNoCMId(hpcsvo.getNoCMId());
		
		historiPelayananCs.setKelompokTransaksi(transaksiConveter.transferVOToModel(hpcsvo.getKelompokTransaksi(), new KelompokTransaksi()));
		
		historiPelayananCs.setAlamatCustomer(alamatConveter.transferVOToModel(hpcsvo.getAlamatCustomer(), new Alamat()));
		
		historiPelayananCs.setKelompokProduk(kelompokProdukConveter.transferVOToModel(hpcsvo.getKelompokProduk(), new KelompokProduk()));
		
		if(CommonUtil.isNotNullOrEmpty(historiPelayananCs)) {
			HistoriPelayananCs historiPelayananCsStatusResult = historiPelayananCsDao.save(historiPelayananCs);
			result.put("NoRec", historiPelayananCsStatusResult.noRec);
			
			/*
			 * save StrukHistori here
			 */
			StrukHistoriVO vo = new StrukHistoriVO();
			
			StrukHistori strukHistori = new StrukHistori();
			
			strukHistori = strukHistoriConverter.transferVOToModel(vo, new StrukHistori());
			
			if(CommonUtil.isNotNullOrEmpty(noHistori)){
				strukHistori.setNonHistori(noHistori);
			}
			
			if(CommonUtil.isNotNullOrEmpty(hpcsvo.getRuanganTujuan()) && hpcsvo.getRuanganTujuan().getId() != 0) {
				strukHistori.setRuangan(ruanganConverter.transferVOToModel(hpcsvo.getRuanganTujuan(), new Ruangan()));
			}
			
			if(CommonUtil.isNotNullOrEmpty(hpcsvo.getTglPelayananAwal())) {
				strukHistori.setTglAwal(hpcsvo.getTglPelayananAwal());
			}
			
			if(CommonUtil.isNotNullOrEmpty(dateNow)){
				strukHistori.setTglHistori(dateNow);
			}
			
			if(CommonUtil.isNotNullOrEmpty(hpcsvo.getPegawaiTerima()) && hpcsvo.getPegawaiTerima().getId() != 0)  {
				strukHistori.setPegawaiTerima(pegawaiConverter.transferVOToModel(hpcsvo.getPegawaiTerima(), new Pegawai()));
			}
				
			if(CommonUtil.isNotNullOrEmpty(strukHistori)){
				StrukHistori strukHistoriResult = strukHistoriDao.save(strukHistori);
				result.put("norecstruk", strukHistoriResult.noRec);
				//saveStrukHistori(strukHistoriVO);
			}
			

		}
		
		return result;
	}
	
	@Override
	public Map<String, Object> getListHistoriPelayananCs(Integer ruanganId){
		Map<String, Object> result = new HashMap<>();
		
		List<Map<String, Object>> listData = this.historiPelayananCsDao.getListHistoriPelayananCs(ruanganId);
		
		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> noHistori = noDefault();
		if(CommonUtil.isNotNullOrEmpty(noHistori)){
			result.put("noHistori", noHistori.get("noHistori"));
		}
		/*
		List<Map<String, Object>> listPegawai = this.historiPelayananCsDao.getListPegawai(ruanganId);
		if(CommonUtil.isNotNullOrEmpty(listPegawai)){
			result.put("listPegawai", listPegawai);
		}
		*/
		return result;
	}
	
	@Override
	public Map<String, Object> getListPasienByNoCm(String noCm) {
		Map<String, Object> result = new HashMap<>();
		
		List<Map<String, Object>> listPasien = this.historiPelayananCsDao.getListPasienByNoCm(noCm);
		
		if(CommonUtil.isNotNullOrEmpty(listPasien)) {
			result.put("listPasien", listPasien);
		}
		
		return result;
	}
	
	@Override
	public Map<String, Object>getListHistoriCsByNorec(String norec) {
		Map<String, Object> result = new HashMap<>();
		
		List<Map<String, Object>> list = this.historiPelayananCsDao.getListDataHistoriCsByNoRec(norec);
		
		if(CommonUtil.isNotNullOrEmpty(list)) {
			result.put("list", list);
		}
		
		return result;
	}
	
	@Override
	public Map<String, Object> getListProdukInformasi() {
		Map<String, Object> result = new HashMap<>();
		
		List<Map<String, Object>> listproduk = this.historiPelayananCsDao.getListProdukInformasi();
		
		if(CommonUtil.isNotNullOrEmpty(listproduk)) {
			result.put("listproduk", listproduk);
		}
		
		return result;
	}
	
	
}
