package com.jasamedika.medifirst2000.service.impl;

import java.util.ArrayList;
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 com.jasamedika.medifirst2000.converter.BaseConverterImpl;
import com.jasamedika.medifirst2000.dao.PengkajianAwalBaruDao;
import com.jasamedika.medifirst2000.dao.SettingDataFixedDao;
import com.jasamedika.medifirst2000.entities.AntrianPasienDiPeriksa;
import com.jasamedika.medifirst2000.entities.Pasien;
import com.jasamedika.medifirst2000.entities.PengkajianAwalBaru;
import com.jasamedika.medifirst2000.entities.Ruangan;
import com.jasamedika.medifirst2000.service.PengkajianAwalBaruService;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.util.DateUtil;
import com.jasamedika.medifirst2000.vo.AntrianPasienDiPeriksaVO;
import com.jasamedika.medifirst2000.vo.PasienVO;
import com.jasamedika.medifirst2000.vo.PengkajianAwalBaruVO;
import com.jasamedika.medifirst2000.vo.RuanganVO;

@Service("PengkajianAwalBaruService")
public class PengkajianAwalBaruServiceImpl implements PengkajianAwalBaruService{

	@Autowired
	private PengkajianAwalBaruDao pengkajianAwalBaruDao;
	
	@Autowired
	private BaseConverterImpl<PengkajianAwalBaruVO, PengkajianAwalBaru> pengkajianAwalBaruConveter;
	
	@Autowired
	private BaseConverterImpl<RuanganVO, Ruangan> ruanganConveter;
	
	@Autowired
	private BaseConverterImpl<PasienVO, Pasien> pasienConveter;
	
	@Autowired
	private BaseConverterImpl<AntrianPasienDiPeriksaVO, AntrianPasienDiPeriksa> antrianPasienDiPeriksaConveter;
	
	@Autowired
	private SettingDataFixedDao settingDataFixedDao;
	
	public String kdPap(String flag) {
		String kdPap = pengkajianAwalBaruDao.countKdPap(flag, DateUtil.now());
		
		String monthNow=String.valueOf(DateUtil.getMonthFromDate(new Date()));
		String yearNow=String.valueOf(DateUtil.getYearFromDate(new Date())).substring(2, 4);
		String digit=null, noUsulan = null;
		
		Long number = null;
		if(CommonUtil.isNotNullOrEmpty(kdPap)) {
			number = Long.valueOf(kdPap.substring(6))+1;
		} else {
			number = (long) 1;
		}
		
		if(number.toString().length()==1){
			digit="000";}
		else if(number.toString().length()==2){
			digit="00";}
		else if(number.toString().length()==3){
			digit="0";}
		if(monthNow.length()==1)
			monthNow = "0"+monthNow;

		if(flag.equalsIgnoreCase("RI")){
			noUsulan="RI"+yearNow+monthNow+digit+number;
		} else if(flag.equalsIgnoreCase("RJ")){
			noUsulan="RJ"+yearNow+monthNow+digit+number;
		} else if (flag.equalsIgnoreCase("GD")){
			noUsulan="GD"+yearNow+monthNow+digit+number;
		}
		return noUsulan;
	}
	
	@Override
	public Map<String, Object> save(PengkajianAwalBaruVO vo){
		Map<String, Object> result = new HashMap<>();
		
		PengkajianAwalBaru pengkajianAwalBaru = new PengkajianAwalBaru();
		if(CommonUtil.isNotNullOrEmpty(vo)){
			pengkajianAwalBaru = pengkajianAwalBaruConveter.transferVOToModel(vo, new PengkajianAwalBaru());
			pengkajianAwalBaru.setTglRegistrasi(DateUtil.now());
			
			if(CommonUtil.isNotNullOrEmpty(vo.getRuangan())){
				Integer departemenId = pengkajianAwalBaruDao.getDepartementByRuangan(vo.getRuangan().getId());
				if(departemenId==Integer.parseInt(
						settingDataFixedDao.getSettingDataFixed("KdDepartemenInstalasiRawatInap"))){
					pengkajianAwalBaru.setKdPap(kdPap("RI"));
				} else if(departemenId==Integer.parseInt(
						settingDataFixedDao.getSettingDataFixed("KdDepartemenInstalasiRawatJalan"))){
					pengkajianAwalBaru.setKdPap(kdPap("RJ"));
				} else if(departemenId==Integer.parseInt(
						settingDataFixedDao.getSettingDataFixed("KdDepartemenInstalasiGawatDarurat"))){
					pengkajianAwalBaru.setKdPap(kdPap("GD"));
				}
			}
		}
		if(CommonUtil.isNotNullOrEmpty(vo.getPasien())){
			pengkajianAwalBaru.setPasien(pasienConveter.transferVOToModel(vo.getPasien(), new Pasien()));
		}
		if(CommonUtil.isNotNullOrEmpty(vo.getRuangan())){
			pengkajianAwalBaru.setRuangan(ruanganConveter.transferVOToModel(vo.getRuangan(), new Ruangan()));
		}
		if(CommonUtil.isNotNullOrEmpty(vo.getNoRegistrasi())){
			pengkajianAwalBaru.setNoRegistrasi(antrianPasienDiPeriksaConveter.transferVOToModel(vo.getNoRegistrasi(), new AntrianPasienDiPeriksa()));
		}
		if(CommonUtil.isNotNullOrEmpty(pengkajianAwalBaru)){
			PengkajianAwalBaru pengkajianAwalBaruResult = pengkajianAwalBaruDao.save(pengkajianAwalBaru);
			result.put("noRec", pengkajianAwalBaruResult.noRec);
		}
		
		return result;
	}
	
	@Override
	public Map<String, Object> getOne(String noRec){
		Map<String, Object> result = pengkajianAwalBaruDao.getOne(noRec);
		return result;
	}
	
	@Override
	public Map<String, Object> getAll(String noCm){
		Map<String, Object> result = new HashMap<>();
		
		List<Map<String, Object>> listData = pengkajianAwalBaruDao.getAll(noCm);
		if(CommonUtil.isNotNullOrEmpty(listData)){
			result.put("listData", listData);
		}
		
		return result;
	}
	
	@Override
	public Map<String, Object> getOneLanjutan(String noRec){
		Map<String, Object> result = pengkajianAwalBaruDao.getOneLajutan(noRec);
		return result;
	}
	
	@Override
	public Map<String, Object> getAllLanjutan(String noCm){
		Map<String, Object> result = new HashMap<>();
		
		List<Map<String, Object>> listData = pengkajianAwalBaruDao.getAllLanjutan(noCm);
		if(CommonUtil.isNotNullOrEmpty(listData)){
			result.put("listData", listData);
		}
		
		return result;
	}
}
