package com.jasamedika.medifirst2000.service.impl;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
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.google.gson.Gson;
import com.jasamedika.medifirst2000.converter.DetailDiagnosaTindakanPasienConverter;
import com.jasamedika.medifirst2000.converter.DiagnosaTindakanConverter;
import com.jasamedika.medifirst2000.converter.DiagnosaTindakanPasienConverter;
import com.jasamedika.medifirst2000.converter.PasienConverter;
import com.jasamedika.medifirst2000.dao.AntrianPasienDiPeriksaDao;
import com.jasamedika.medifirst2000.dao.DiagnosaTindakanPasienDao;
import com.jasamedika.medifirst2000.dao.PasienDaftarDao;
import com.jasamedika.medifirst2000.dao.PemakaianAsuransiDao;
import com.jasamedika.medifirst2000.entities.AntrianPasienDiPeriksa;
import com.jasamedika.medifirst2000.entities.DetailDiagnosaPasien;
import com.jasamedika.medifirst2000.entities.DetailDiagnosaTindakanPasien;
import com.jasamedika.medifirst2000.entities.DiagnosaTindakan;
import com.jasamedika.medifirst2000.entities.DiagnosaTindakanPasien;
import com.jasamedika.medifirst2000.entities.Pasien;
import com.jasamedika.medifirst2000.entities.PasienDaftar;
import com.jasamedika.medifirst2000.entities.PelayananPasien;
import com.jasamedika.medifirst2000.entities.PemakaianAsuransi;
import com.jasamedika.medifirst2000.service.DiagnosaTindakanPasienService;
import com.jasamedika.medifirst2000.service.ProdukService;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.util.JsonUtil;
import com.jasamedika.medifirst2000.vo.DetailDiagnosaTindakanPasienVO;
import com.jasamedika.medifirst2000.vo.DiagnosaTindakanPasienVO;


/**
 * Implement class for PreskripsiDietService
 * 
 * @author Adik
 * @param <T>
 */


@Service("diagnosaTindakanPasienService")
public class DiagnosaTindakanPasienServiceImpl extends BaseVoServiceImpl implements DiagnosaTindakanPasienService {
	@Autowired
	private PasienConverter pasienConverter;
	@Autowired
	private AntrianPasienDiPeriksaDao antrianPasienDiPeriksaDao;
	@Autowired
	private DiagnosaTindakanPasienConverter diagnosaTindakanPasienConverter;
	
	@Autowired
	private DiagnosaTindakanPasienDao diagnosaTindakanPasienDao;	
	
	@Autowired
	private DetailDiagnosaTindakanPasienConverter detailDiagnosaTindakanPasienConverter;
	
	@Autowired
	private DiagnosaTindakanConverter diagnosaTindakanConverter;
	@Autowired
	private PasienDaftarDao pasienDaftarDao;
	@Autowired
	private ProdukService  produkService;
	

	@Autowired
	private PemakaianAsuransiDao pemakaianAsuransiDao;
	
		@Override
	public Map<String, Object> saveDiagnosaTindakanPasien(DiagnosaTindakanPasienVO vo) {
		DiagnosaTindakanPasien diagnosaTindakanPasien=new DiagnosaTindakanPasien();
		if(CommonUtil.isNotNullOrEmpty(vo)){
			diagnosaTindakanPasien=diagnosaTindakanPasienConverter.transferVOToModel(vo, new DiagnosaTindakanPasien());
		}AntrianPasienDiPeriksa pasienDaftar=null;
		pasienDaftar= antrianPasienDiPeriksaDao.findByNoRec(vo.getNoRec());
		if(CommonUtil.isNotNullOrEmpty(vo.getPasien())&& pasienDaftar == null){
			pasienDaftar =antrianPasienDiPeriksaDao.finByNoIdAndTglRegistrasi(Short.parseShort("0"), vo.getPasien().getId(),
					vo.getTanggalPendaftaran());
			diagnosaTindakanPasien.setPasien(pasienDaftar);
		}
		else
			diagnosaTindakanPasien.setPasien(pasienDaftar);
		diagnosaTindakanPasienDao.delete(diagnosaTindakanPasienDao.findByPasienDaftarNoRec(pasienDaftar.getNoRec()));
		diagnosaTindakanPasien.setTanggalPendaftaran(vo.getTanggalPendaftaran());
		diagnosaTindakanPasien.getDiagnosisTindakan().clear();
		List<PelayananPasien> list = new ArrayList<>();
		for(DetailDiagnosaTindakanPasienVO detailDiagnosaTindakanPasienVO:vo.getDiagnosisTindakan()){
			if(CommonUtil.isNotNullOrEmpty(detailDiagnosaTindakanPasienVO)){
				DetailDiagnosaTindakanPasien detailDiagnosaTindakanPasien=detailDiagnosaTindakanPasienConverter.transferVOToModel(detailDiagnosaTindakanPasienVO, new DetailDiagnosaTindakanPasien());
				if(CommonUtil.isNotNullOrEmpty(detailDiagnosaTindakanPasienVO.getDiagnosaTindakan())){
					detailDiagnosaTindakanPasien.setDiagnosaTindakan(diagnosaTindakanConverter.transferVOToModel(detailDiagnosaTindakanPasienVO.getDiagnosaTindakan(),new DiagnosaTindakan()));
				}
				detailDiagnosaTindakanPasien.setDiagnosaTindakanPasien(diagnosaTindakanPasien);
				detailDiagnosaTindakanPasien.setJumlah(detailDiagnosaTindakanPasienVO.getJumlah());
				diagnosaTindakanPasien.getDiagnosisTindakan().add(detailDiagnosaTindakanPasien);
				
				if(detailDiagnosaTindakanPasienVO.getNoRec()==null)
				{
					List<PelayananPasien> pelayananPasien = produkService
							.postDiagnosa(detailDiagnosaTindakanPasienVO.getDiagnosaTindakan().getId().toString(), pasienDaftar);
					list.addAll(pelayananPasien);
				}
				//produkService.postDiagnosa(detailDiagnosaTindakanPasien.getDiagnosaTindakan().getId().toString(),pasienDaftar);
			}
		}
		
		DiagnosaTindakanPasien resultPasien=diagnosaTindakanPasienDao.save(diagnosaTindakanPasien);
		
		URL url;
		/*try {
			String location =GetSettingDataFixed("UrlKeuangan").toString() + "/service/transaksi/akuntansi/jurnal-otomatis";
			url = new URL(GetSettingDataFixed("UrlKeuangan").toString() + "/service/transaksi/akuntansi/jurnal-otomatis");

			HttpURLConnection myURLConnection = (HttpURLConnection) url.openConnection();
			myURLConnection.setRequestMethod("POST");

//			myURLConnection.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
			myURLConnection.setRequestProperty("Accept", "application/json");
			myURLConnection.setRequestProperty("Content-Type", "application/json");
			PemakaianAsuransi pemakaianAsuransi=	 pemakaianAsuransiDao.findByAntrianPasienDiperiksa (pasienDaftar.getNoRec());
			List<AntrianPasienDiPeriksa> antrianDiperiksas = antrianPasienDiPeriksaDao.findByNoRegisterNonRawatInap(pasienDaftar.getPasienDaftar().getNoRegistrasi());
			String kdInstalasi ="";
			if(antrianDiperiksas.size()!=0)
			{
				kdInstalasi= GetSettingDataFixed("KdDepartemenInstalasiRawatJalan");				
			}
			else
				kdInstalasi = GetSettingDataFixed("KdDepartemenInstalasiRawatInap");
			Integer kdRekanan = 0;
			if(pemakaianAsuransi !=null)
			{
				kdRekanan =Integer.parseInt( GetSettingDataFixed("KdRekananBpjs"));
				
			}
			String urlParameters = "[";
			for (PelayananPasien pelayananPasien : list) {
				if(urlParameters=="[")
					
				{
					SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
					urlParameters+=	"{\"kdDepartemen\":\""+ kdInstalasi+ "\",\"kdRekanan\":\""+ kdRekanan+ "\",\"jenisPelayanan\":\""+ pasienDaftar.getPasienDaftar().getJenisPelayanan() +"\",\"noAntrianDiperiksa\":\""+ pasienDaftar.getNoRec() +"\",\"noIdPelayanan\":\""+ pelayananPasien.getNoRec() +"\", \"idProduk\":\""+pelayananPasien.getProduk().getId()+"\",\"harga\":\""+ pelayananPasien.getHargaJual() +"\",\"tglInput\":\""+format.format( pelayananPasien.getTglPelayanan()) +"\",\"keterangan\":\"Pelayanan Tindakan\"}";
				}
				else
				{
					SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
					urlParameters+=	",{\"kdDepartemen\":\""+ kdInstalasi+ "\",\"kdRekanan\":\""+ kdRekanan+ "\",\"jenisPelayanan\":\""+ pasienDaftar.getPasienDaftar().getJenisPelayanan() +"\",\"noAntrianDiperiksa\":\""+ pasienDaftar.getNoRec() +"\",\"noIdPelayanan\":\""+ pelayananPasien.getNoRec() +"\", \"idProduk\":\""+pelayananPasien.getProduk().getId()+"\",\"harga\":\""+ pelayananPasien.getHargaJual() +"\",\"tglInput\":\""+ format.format( pelayananPasien.getTglPelayanan()) +"\",\"keterangan\":\"Pelayanan Tindakan\"}";
				}
				
}
			urlParameters += "]";
			byte[] postData = urlParameters.getBytes(StandardCharsets.UTF_8);
			System.out.println(urlParameters);
			System.out.println(postData);
			myURLConnection.setDoOutput(true);
			try (DataOutputStream wr = new DataOutputStream(myURLConnection.getOutputStream())) {
				wr.write(postData);
			}
			int responseCode = myURLConnection.getResponseCode();
			BufferedReader reader = new BufferedReader(new InputStreamReader(myURLConnection.getInputStream()));

			String read = null;
			String resultData = "";
			while ((read = reader.readLine()) != null) {
				resultData += read;
				System.out.println(read);
			}

			Gson gson = new Gson();
			String json = gson.toJson(resultData);
			Map<String, Object> hasil = JsonUtil.Convert(resultData);
		} catch (IOException e) {
			e.printStackTrace();
		}*/
		
		Map<String,Object> resultDiagnosaTindakanPasien=new HashMap<String,Object>();
		resultDiagnosaTindakanPasien.put("noRec", resultPasien.getNoRec());
		resultDiagnosaTindakanPasien.put("namaPasien", resultPasien.getPasien().getPasienDaftar().getPasien().getNamaPasien());
		resultDiagnosaTindakanPasien.put("noCm", resultPasien.getPasien().getPasienDaftar().getPasien().getNoCm());
		return resultDiagnosaTindakanPasien;
	}

		@Override
		public Map<String,Object> findByNoCmAndTglInput(String noCm, Date tglInput) {
			Map<String,Object> result=new HashMap<String,Object>();
			List<DiagnosaTindakanPasien> resultModel=diagnosaTindakanPasienDao.findDiagnosaTindakanPasienByNoCmAndTglInput(noCm, tglInput);
			if(CommonUtil.isNotNullOrEmpty(resultModel)){
				result.put("diagnosaTindakanPasien", resultModel);
				result.put("dataFound", true);
			} else {
				result.put("dataFound", false);
			}
			return result;
		}

		@Override
		public Map<String,Object> findDiagnosaTindakanPasienByAntrian(String noRec) {
			Map<String,Object> result=new HashMap<String,Object>();
			List<DiagnosaTindakanPasien> list=diagnosaTindakanPasienDao.findDiagnosaTindakanPasienByAntrian(noRec);
			if(CommonUtil.isNotNullOrEmpty(list)){
				try {
					result.put("diagnosaTindakanPasien", JsonUtil.ToMaps(list));
				} catch (IllegalArgumentException e) {
					e.printStackTrace();
				} catch (IllegalAccessException e) {
					e.printStackTrace();
				}
				result.put("dataFound", true);
			} else {
				result.put("dataFound", false);
			}
			return result;
		}
}
