package com.jasamedika.medifirst2000.controller;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.util.ArrayList;

import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.jasamedika.medifirst2000.vo.custom.BridgeEKlaimVO;

@RestController
@RequestMapping("/e-klaim")

public class BridgingEKlaimController {

	@RequestMapping(value = "/ri", method = RequestMethod.GET, produces = { MediaType.APPLICATION_JSON_VALUE })
	public ArrayList<BridgeEKlaimVO> eKlaimRi(@RequestParam(value = "startdate", required = true) String start, @RequestParam(value = "enddate", required = true) String end) throws IOException {

		java.net.URL obj = new java.net.URL("http://localhost/E-Klaim/ws.php?mode=debug");
		HttpURLConnection con = (HttpURLConnection) obj.openConnection();
		con.setRequestMethod("POST");
		con.setRequestProperty("Content-Type", "application/json");
		con.setDoInput(true);
		con.setDoOutput(true);
		String strJson = "{\"metadata\":{\"method\":\"pull_claim\"},\"data\":{\"start_dt\":\""+start+"\",\"stop_dt\":\""+end+"\",\"jenis_rawat\":\"1\"}}";
		OutputStream os = con.getOutputStream();
        os.write(strJson.getBytes("UTF-8"));
        os.close();
        
        InputStream in = new BufferedInputStream(con.getInputStream());
        String result = org.apache.commons.io.IOUtils.toString(in, "UTF-8");
        ArrayList<BridgeEKlaimVO> ret = new ArrayList<BridgeEKlaimVO>();
        try {
        	
			JSONObject jsonObject = new JSONObject(result);
			JSONObject metadata = (JSONObject) jsonObject.get("metadata");
			System.out.println(metadata.get("code"));
			System.out.println(metadata.get("message"));
			JSONObject response = (JSONObject) jsonObject.get("response");
			String dataKlaim =  response.get("data").toString();
			System.out.println(dataKlaim);
			String data[] = dataKlaim.split("\n");
			for(int i=1;i<data.length;i++){
				String fields[] = data[i].split("\t");
				BridgeEKlaimVO x = new BridgeEKlaimVO();
				x.setKODE_RS(fields[0]);
				x.setKELAS_RS(fields[1]);
				x.setKELAS_RAWAT(fields[2]);
				x.setKODE_TARIF(fields[3]);
				x.setPTD(fields[4]);
				x.setADMISSION_DATE(fields[5]);
				x.setDISCHARGE_DATE(fields[6]);
				x.setBIRTH_DATE(fields[7]);
				x.setBIRTH_WEIGHT(fields[8]);
				x.setSEX(fields[9]);
				x.setDISCHARGE_STATUS(fields[10]);
				x.setDIAGLIST(fields[11]);
				x.setPROCLIST(fields[12]);
				x.setADL1(fields[13]);
				x.setADL2(fields[14]);
				x.setIN_SP(fields[15]);
				x.setIN_SR(fields[16]);
				x.setIN_SI(fields[17]);
				x.setIN_SD(fields[18]);
				x.setINACBG(fields[19]);
				x.setSUBACUTE(fields[20]);
				x.setCHRONIC(fields[21]);
				x.setSP(fields[22]);
				x.setSR(fields[23]);
				x.setSI(fields[24]);
				x.setSD(fields[25]);
				x.setDESKRIPSI_INACBG(fields[26]);
				x.setTARIF_INACBG(fields[27]);
				x.setTARIF_SUBACUTE(fields[28]);
				x.setTARIF_CHRONIC(fields[29]);
				x.setDESKRIPSI_SP(fields[30]);
				x.setTARIF_SP(fields[31]);
				x.setDESKRIPSI_SR(fields[32]);
				x.setTARIF_SR(fields[33]);
				x.setDESKRIPSI_SI(fields[34]);
				x.setTARIF_SI(fields[35]);
				x.setDESKRIPSI_SD(fields[36]);
				x.setTARIF_SD(fields[37]);
				x.setTOTAL_TARIF(fields[38]);
				x.setTARIF_RS(fields[39]);
				x.setTARIF_POLI_EKS(fields[40]);
				x.setLOS(fields[41]);
				x.setICU_INDIKATOR(fields[42]);
				x.setICU_LOS(fields[43]);
				x.setVENT_HOUR(fields[44]);
				x.setNAMA_PASIEN(fields[45]);
				x.setMRN(fields[46]);
				x.setUMUR_TAHUN(fields[47]);
				x.setUMUR_HARI(fields[48]);
				x.setDPJP(fields[48]);
				x.setSEP(fields[50]);
				x.setNOKARTU(fields[51]);
				x.setPAYOR_ID(fields[52]);
				x.setCODER_ID(fields[53]);
				x.setVERSI_INACBG(fields[54]);
				x.setVERSI_GROUPER(fields[55]);
				x.setC1(fields[56]);
				x.setC2(fields[57]);
				x.setC3(fields[58]);
				x.setC4(fields[59]);
				ret.add(x);
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
        in.close();
        con.disconnect();        
		return ret;
	}

	@RequestMapping(value = "/rj", method = RequestMethod.GET, produces = { MediaType.APPLICATION_JSON_VALUE })
	public ArrayList<BridgeEKlaimVO> eKlaimRj(@RequestParam(value = "startdate", required = true) String start, @RequestParam(value = "enddate", required = true) String end) throws IOException {

		java.net.URL obj = new java.net.URL("http://localhost/E-Klaim/ws.php?mode=debug");
		HttpURLConnection con = (HttpURLConnection) obj.openConnection();
		con.setRequestMethod("POST");
		con.setRequestProperty("Content-Type", "application/json");
		con.setDoInput(true);
		con.setDoOutput(true);
		String strJson = "{\"metadata\":{\"method\":\"pull_claim\"},\"data\":{\"start_dt\":\""+start+"\",\"stop_dt\":\""+end+"\",\"jenis_rawat\":\"2\"}}";
		OutputStream os = con.getOutputStream();
        os.write(strJson.getBytes("UTF-8"));
        os.close();
        
        InputStream in = new BufferedInputStream(con.getInputStream());
        String result = org.apache.commons.io.IOUtils.toString(in, "UTF-8");
        ArrayList<BridgeEKlaimVO> ret = new ArrayList<BridgeEKlaimVO>();
        try {
        	
			JSONObject jsonObject = new JSONObject(result);
			JSONObject metadata = (JSONObject) jsonObject.get("metadata");
			System.out.println(metadata.get("code"));
			System.out.println(metadata.get("message"));
			JSONObject response = (JSONObject) jsonObject.get("response");
			String dataKlaim =  response.get("data").toString();
			System.out.println(dataKlaim);
			String data[] = dataKlaim.split("\n");
			for(int i=1;i<data.length;i++){
				String fields[] = data[i].split("\t");
				BridgeEKlaimVO x = new BridgeEKlaimVO();
				x.setKODE_RS(fields[0]);
				x.setKELAS_RS(fields[1]);
				x.setKELAS_RAWAT(fields[2]);
				x.setKODE_TARIF(fields[3]);
				x.setPTD(fields[4]);
				x.setADMISSION_DATE(fields[5]);
				x.setDISCHARGE_DATE(fields[6]);
				x.setBIRTH_DATE(fields[7]);
				x.setBIRTH_WEIGHT(fields[8]);
				x.setSEX(fields[9]);
				x.setDISCHARGE_STATUS(fields[10]);
				x.setDIAGLIST(fields[11]);
				x.setPROCLIST(fields[12]);
				x.setADL1(fields[13]);
				x.setADL2(fields[14]);
				x.setIN_SP(fields[15]);
				x.setIN_SR(fields[16]);
				x.setIN_SI(fields[17]);
				x.setIN_SD(fields[18]);
				x.setINACBG(fields[19]);
				x.setSUBACUTE(fields[20]);
				x.setCHRONIC(fields[21]);
				x.setSP(fields[22]);
				x.setSR(fields[23]);
				x.setSI(fields[24]);
				x.setSD(fields[25]);
				x.setDESKRIPSI_INACBG(fields[26]);
				x.setTARIF_INACBG(fields[27]);
				x.setTARIF_SUBACUTE(fields[28]);
				x.setTARIF_CHRONIC(fields[29]);
				x.setDESKRIPSI_SP(fields[30]);
				x.setTARIF_SP(fields[31]);
				x.setDESKRIPSI_SR(fields[32]);
				x.setTARIF_SR(fields[33]);
				x.setDESKRIPSI_SI(fields[34]);
				x.setTARIF_SI(fields[35]);
				x.setDESKRIPSI_SD(fields[36]);
				x.setTARIF_SD(fields[37]);
				x.setTOTAL_TARIF(fields[38]);
				x.setTARIF_RS(fields[39]);
				x.setTARIF_POLI_EKS(fields[40]);
				x.setLOS(fields[41]);
				x.setICU_INDIKATOR(fields[42]);
				x.setICU_LOS(fields[43]);
				x.setVENT_HOUR(fields[44]);
				x.setNAMA_PASIEN(fields[45]);
				x.setMRN(fields[46]);
				x.setUMUR_TAHUN(fields[47]);
				x.setUMUR_HARI(fields[48]);
				x.setDPJP(fields[48]);
				x.setSEP(fields[50]);
				x.setNOKARTU(fields[51]);
				x.setPAYOR_ID(fields[52]);
				x.setCODER_ID(fields[53]);
				x.setVERSI_INACBG(fields[54]);
				x.setVERSI_GROUPER(fields[55]);
				x.setC1(fields[56]);
				x.setC2(fields[57]);
				x.setC3(fields[58]);
				x.setC4(fields[59]);
				ret.add(x);
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
        in.close();
        con.disconnect();        
		return ret;
	}

}
