package com.jasamedika.medifirst2000.controller;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
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.dao.BridgingDao;
import com.jasamedika.medifirst2000.vo.custom.BridgeBUKBorVO;
import com.jasamedika.medifirst2000.vo.custom.BridgeKunjunganPasienVO;

@RestController
@RequestMapping("/sirsbuk")

public class BridgingSirsBukController {

	@Autowired
	private BridgingDao myDao;

	@RequestMapping(value = "/irj", method = RequestMethod.GET, produces = { MediaType.APPLICATION_JSON_VALUE })
	public ArrayList<BridgeKunjunganPasienVO> kunjunganIrj(@RequestParam(value = "tanggal", required = true) String tanggal) throws ParseException {
		ArrayList<BridgeKunjunganPasienVO> output = new ArrayList<BridgeKunjunganPasienVO>();
		List<Object[]> returnVal = null;
		String tanggalan[] = tanggal.split("-");
		if(tanggalan.length==3){
			boolean valid = false;
			if(tanggalan[0].length()==1){
				tanggalan[0]="0"+tanggalan[0];
			}
			if(tanggalan[1].length()==1){
				tanggalan[1]="0"+tanggalan[1];
			}
			if(tanggalan[2].length()==2){
				tanggalan[2]="20"+tanggalan[2];
			}
			if((tanggalan[0].length()==2)&(tanggalan[1].length()==2)&(tanggalan[2].length()==4)){
				valid=true;
			}else{
				System.out.println("tanggalan salah:"+ tanggalan[0]+"-"+tanggalan[1] +"-"+tanggalan[2]);
			}
			if(valid){
				
				SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
				Date d = (Date) sdf.parse(tanggalan[0]+"/"+tanggalan[1]+"/"+tanggalan[2]);
				returnVal = myDao.kunjunganIrj(d);
				String namaRuang = null;
				for(int j = 0; j<returnVal.size();j++){
					if(output.size()>0){
						namaRuang = (String)returnVal.get(j)[2];
						System.out.println(namaRuang);
						for(int k =0; k<output.size();k++){
							if(output.get(k).getKLINIK().equals(namaRuang)){
								if(((Integer)returnVal.get(j)[3]==2)|((Integer)returnVal.get(j)[3]==4)){
									int counterJKN = output.get(k).getJKN();
									counterJKN ++;
									output.get(k).setJKN(counterJKN);									
								}else{
									int counterNonJKN = output.get(k).getNONJKN();
									counterNonJKN ++;
									output.get(k).setNONJKN(counterNonJKN);																	
								}

							}else{
								BridgeKunjunganPasienVO temp2 = new BridgeKunjunganPasienVO();
								temp2.setKLINIK((String)returnVal.get(j)[2]);
								if(((Integer)returnVal.get(j)[3]==2)|((Integer)returnVal.get(j)[3]==4)){
									int counterJKN3 = temp2.getJKN();
									counterJKN3 ++;
									temp2.setJKN(counterJKN3);
									
								}else{
									int counterNonJKN3 = temp2.getNONJKN();
									counterNonJKN3 ++;
									temp2.setNONJKN(counterNonJKN3);											
								}
								output.add(temp2);
							}
						}
					}else{
						System.out.println("Inisialisasi");
						BridgeKunjunganPasienVO temp = new BridgeKunjunganPasienVO();
						temp.setKLINIK((String)returnVal.get(j)[2]);
						if(((Integer)returnVal.get(j)[3]==2)|((Integer)returnVal.get(j)[3]==4)){
							int counterJKN2 = temp.getJKN();
							counterJKN2 ++;
							temp.setJKN(counterJKN2);							
						}else{
							int counterNonJKN2 = temp.getNONJKN();
							counterNonJKN2 ++;
							temp.setNONJKN(counterNonJKN2);							
						}
						output.add(temp);
					}
				}
			}
		}else{
			System.out.println("tanggalan salah!");
		}
		
		return output;
	}

	@RequestMapping(value = "/iri", method = RequestMethod.GET, produces = { MediaType.APPLICATION_JSON_VALUE })
	public ArrayList<BridgeKunjunganPasienVO> kunjunganIri(@RequestParam(value = "tanggal", required = true) String tanggal) throws ParseException {
		ArrayList<BridgeKunjunganPasienVO> output = new ArrayList<BridgeKunjunganPasienVO>();
		List<Object[]> returnVal = null;
		String tanggalan[] = tanggal.split("-");
		if(tanggalan.length==3){
			boolean valid = false;
			if(tanggalan[0].length()==1){
				tanggalan[0]="0"+tanggalan[0];
			}
			if(tanggalan[1].length()==1){
				tanggalan[1]="0"+tanggalan[1];
			}
			if(tanggalan[2].length()==2){
				tanggalan[2]="20"+tanggalan[2];
			}
			if((tanggalan[0].length()==2)&(tanggalan[1].length()==2)&(tanggalan[2].length()==4)){
				valid=true;
			}else{
				System.out.println("tanggalan salah:"+ tanggalan[0]+"-"+tanggalan[1] +"-"+tanggalan[2]);
			}
			if(valid){
				
				SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
				Date d = (Date) sdf.parse(tanggalan[0]+"/"+tanggalan[1]+"/"+tanggalan[2]);
				returnVal = myDao.kunjunganIri(d);
				String namaRuang = null;
				for(int j = 0; j<returnVal.size();j++){
					if(output.size()>0){
						namaRuang = (String)returnVal.get(j)[2];
						System.out.println(namaRuang);
						for(int k =0; k<output.size();k++){
							if(output.get(k).getKLINIK().equals(namaRuang)){
								if(((Integer)returnVal.get(j)[3]==2)|((Integer)returnVal.get(j)[3]==4)){
									int counterJKN = output.get(k).getJKN();
									counterJKN ++;
									output.get(k).setJKN(counterJKN);									
								}else{
									int counterNonJKN = output.get(k).getNONJKN();
									counterNonJKN ++;
									output.get(k).setNONJKN(counterNonJKN);																	
								}

							}else{
								BridgeKunjunganPasienVO temp2 = new BridgeKunjunganPasienVO();
								temp2.setKLINIK((String)returnVal.get(j)[2]);
								if(((Integer)returnVal.get(j)[3]==2)|((Integer)returnVal.get(j)[3]==4)){
									int counterJKN3 = temp2.getJKN();
									counterJKN3 ++;
									temp2.setJKN(counterJKN3);
									
								}else{
									int counterNonJKN3 = temp2.getNONJKN();
									counterNonJKN3 ++;
									temp2.setNONJKN(counterNonJKN3);											
								}
								output.add(temp2);
							}
						}
					}else{
						System.out.println("Inisialisasi");
						BridgeKunjunganPasienVO temp = new BridgeKunjunganPasienVO();
						temp.setKLINIK((String)returnVal.get(j)[2]);
						if(((Integer)returnVal.get(j)[3]==2)|((Integer)returnVal.get(j)[3]==4)){
							int counterJKN2 = temp.getJKN();
							counterJKN2 ++;
							temp.setJKN(counterJKN2);							
						}else{
							int counterNonJKN2 = temp.getNONJKN();
							counterNonJKN2 ++;
							temp.setNONJKN(counterNonJKN2);							
						}
						output.add(temp);
					}
				}
			}
		}else{
			System.out.println("tanggalan salah!");
		}
		
		return output;
	}

	@RequestMapping(value = "/igd", method = RequestMethod.GET, produces = { MediaType.APPLICATION_JSON_VALUE })
	public ArrayList<BridgeKunjunganPasienVO> kunjunganIgd(@RequestParam(value = "tanggal", required = true) String tanggal) throws ParseException {
		ArrayList<BridgeKunjunganPasienVO> output = new ArrayList<BridgeKunjunganPasienVO>();
		List<Object[]> returnVal = null;
		String tanggalan[] = tanggal.split("-");
		if(tanggalan.length==3){
			boolean valid = false;
			if(tanggalan[0].length()==1){
				tanggalan[0]="0"+tanggalan[0];
			}
			if(tanggalan[1].length()==1){
				tanggalan[1]="0"+tanggalan[1];
			}
			if(tanggalan[2].length()==2){
				tanggalan[2]="20"+tanggalan[2];
			}
			if((tanggalan[0].length()==2)&(tanggalan[1].length()==2)&(tanggalan[2].length()==4)){
				valid=true;
			}else{
				System.out.println("tanggalan salah:"+ tanggalan[0]+"-"+tanggalan[1] +"-"+tanggalan[2]);
			}
			if(valid){
				
				SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
				Date d = (Date) sdf.parse(tanggalan[0]+"/"+tanggalan[1]+"/"+tanggalan[2]);
				returnVal = myDao.kunjunganIgd(d);
				String namaRuang = null;
				for(int j = 0; j<returnVal.size();j++){
					if(output.size()>0){
						namaRuang = (String)returnVal.get(j)[2];
						System.out.println(namaRuang);
						for(int k =0; k<output.size();k++){
							if(output.get(k).getKLINIK().equals(namaRuang)){
								if(((Integer)returnVal.get(j)[3]==2)|((Integer)returnVal.get(j)[3]==4)){
									int counterJKN = output.get(k).getJKN();
									counterJKN ++;
									output.get(k).setJKN(counterJKN);									
								}else{
									int counterNonJKN = output.get(k).getNONJKN();
									counterNonJKN ++;
									output.get(k).setNONJKN(counterNonJKN);																	
								}

							}else{
								BridgeKunjunganPasienVO temp2 = new BridgeKunjunganPasienVO();
								temp2.setKLINIK((String)returnVal.get(j)[2]);
								if(((Integer)returnVal.get(j)[3]==2)|((Integer)returnVal.get(j)[3]==4)){
									int counterJKN3 = temp2.getJKN();
									counterJKN3 ++;
									temp2.setJKN(counterJKN3);
									
								}else{
									int counterNonJKN3 = temp2.getNONJKN();
									counterNonJKN3 ++;
									temp2.setNONJKN(counterNonJKN3);											
								}
								output.add(temp2);
							}
						}
					}else{
						System.out.println("Inisialisasi");
						BridgeKunjunganPasienVO temp = new BridgeKunjunganPasienVO();
						temp.setKLINIK((String)returnVal.get(j)[2]);
						if(((Integer)returnVal.get(j)[3]==2)|((Integer)returnVal.get(j)[3]==4)){
							int counterJKN2 = temp.getJKN();
							counterJKN2 ++;
							temp.setJKN(counterJKN2);							
						}else{
							int counterNonJKN2 = temp.getNONJKN();
							counterNonJKN2 ++;
							temp.setNONJKN(counterNonJKN2);							
						}
						output.add(temp);
					}
				}
			}
		}else{
			System.out.println("tanggalan salah!");
		}
		if(output.size()==0){
			BridgeKunjunganPasienVO temp0 = new BridgeKunjunganPasienVO();
			temp0.setKLINIK("Gawat Darurat(Emergency)");
			temp0.setJKN(0);
			temp0.setNONJKN(0);
			output.add(temp0);
		}
		
		return output;
	}


	@RequestMapping(value = "/bor", method = RequestMethod.GET, produces = { MediaType.APPLICATION_JSON_VALUE })
	public BridgeBUKBorVO bor(@RequestParam(value = "bulan", required = true) String bulanTahun) throws ParseException  {
		//String ret = "";
		SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");

		List<Object[]> bed = myDao.getQtyBed();
		Integer totalBed =0;
		ArrayList<Integer> ruanganId= new ArrayList<Integer>();
		for(int i = 0; i<bed.size();i++){
			totalBed = totalBed + ((Byte)bed.get(i)[1]).intValue();
			if(ruanganId.size()==0){
				ruanganId.add((Integer)bed.get(i)[0]);
			}else{
				boolean found=false;
				for(int j = 0;j<ruanganId.size();j++){
					if(ruanganId.get(j)==(Integer)bed.get(i)[0]){
						found = true;
						break;
					}
				}
				if(!found){
					ruanganId.add((Integer)bed.get(i)[0]);
				}
			}
		}
		System.out.println("totalBed"+ String.valueOf(totalBed));
		String bb[]= bulanTahun.split("-");
		String bulan = bb[0].trim();
		String tahun = bb[1].trim();

		int month = Integer.parseInt(bulan);		
		int year = Integer.parseInt(tahun);
		
		GregorianCalendar mycal = new GregorianCalendar(year,month, 1);
		int daysInMonth= mycal.getActualMaximum(Calendar.DAY_OF_MONTH);
		
		Date d1 = (Date) sdf.parse("01"+"/"+bulan+"/"+tahun);		
		Date d2 = (Date) sdf.parse(String.valueOf(daysInMonth)+"/"+bulan+"/"+tahun);
		
		List<Object[]>  x = myDao.getListPasienRanap(d1,d2);
		System.out.println( "Jumlah Pasien total" + String.valueOf(x.size()) );
		
		int cnt[] = new int[daysInMonth];
		for(int i  = 0;i<x.size();i++){
			for(int j  = 0;j<daysInMonth;j++){
				Date tgl1 = (Date)x.get(i)[0];			
				Date tgl2 = (Date)x.get(i)[1];
				String tgl1s[],tgl2s[];
				try{
					tgl1s= sdf.format(tgl1).split("/");
					String tmp1[] =tgl1s;
					if(Integer.valueOf(tmp1[0])==j){
						
					}
				}catch(Exception ex){
					tgl1s= new String[0];
				}
				try{
					tgl2s= sdf.format(tgl2).split("/");
				}catch(Exception ex){
					tgl2s= (String.valueOf(daysInMonth)+"/"+bulan+"/"+tahun).split("/");
				}
				
				if((j>=Integer.valueOf(tgl1s[0])+1)&(j<=Integer.valueOf(tgl2s[0])+1)){
					cnt[j]++;
				}
			}			
		}
		int totalCnt = 0;
		for(int i=0;i<cnt.length;i++){
			totalCnt=totalCnt+cnt[i];
		}
		BridgeBUKBorVO returnvalue = new BridgeBUKBorVO();
		NumberFormat formatter = new DecimalFormat("#0.00"); 
		
		returnvalue.setBOR(Double.valueOf(formatter.format((double)(totalCnt*100)/(double)(totalBed*daysInMonth))));
		
		return returnvalue ;
	}
		
	//tidak dipakai, tapi belum dibuang. sudah digantikan dengan yang diatas.
	/*
	@RequestMapping(value = "/bor", method = RequestMethod.GET, produces = { MediaType.APPLICATION_JSON_VALUE })
	public BridgeBUKBorVO borCalc(@RequestParam(value = "bulan", required = true) String bulanTahun) throws ParseException {
		List<Object[]> ret = myDao.getQtyBed();
		Integer total =0;
		ArrayList<Integer> ruanganId= new ArrayList<Integer>();
		for(int i = 0; i<ret.size();i++){
			total = total + ((Byte)ret.get(i)[1]).intValue();
			if(ruanganId.size()==0){
				ruanganId.add((Integer)ret.get(i)[0]);
			}else{
				boolean found=false;
				for(int j = 0;j<ruanganId.size();j++){
					if(ruanganId.get(j)==(Integer)ret.get(i)[0]){
						found = true;
						break;
					}
				}
				if(!found){
					ruanganId.add((Integer)ret.get(i)[0]);
				}
			}
		}
		
		System.out.println(total);
		System.out.println(ruanganId.size());
		String bb[]= bulanTahun.split("-");
		String bulan = bb[0].trim();
		String tahun = bb[1].trim();

		int month = Integer.parseInt(bulan);		
		int year = Integer.parseInt(tahun);
		
		GregorianCalendar mycal = new GregorianCalendar(year,month, 1);
		int daysInMonth= mycal.getActualMaximum(Calendar.DAY_OF_MONTH);
		Integer occupied=0;
		for(int hari =1;hari<daysInMonth+1; hari++){
			SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
			Date d = (Date) sdf.parse(String.valueOf(hari)+"/"+bulan+"/"+tahun);
			for (int i = 0;i<ruanganId.size();i++){
				Integer tmp = pp.getCountByMonthAndYearRuangan(d, ruanganId.get(i));
				if(tmp==null){
					tmp=0;
				}
				occupied=occupied+tmp;
			}
			//System.out.println(String.valueOf(hari)+" terisi:"+occupied.toString());
		}
		BridgeBUKBorVO returnvalue = new BridgeBUKBorVO();
		System.out.println((occupied*100));
		System.out.println(total);
		NumberFormat formatter = new DecimalFormat("#0.00"); 
		
		returnvalue.setBOR(Double.valueOf(formatter.format((double)(occupied*100)/(double)(total*1))));
		
	return returnvalue;
	}
	*/
}
