package com.jasamedika.medifirst2000.controller;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.orm.jpa.JpaSystemException;
import org.springframework.web.bind.annotation.RequestBody;
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.constants.Constants;
import com.jasamedika.medifirst2000.constants.MessageResource;
import com.jasamedika.medifirst2000.controller.base.LocaleController;
import com.jasamedika.medifirst2000.core.web.WebConstants;
import com.jasamedika.medifirst2000.dao.BridgingDao;
import com.jasamedika.medifirst2000.entities.Profile;
import com.jasamedika.medifirst2000.exception.ServiceVOException;
import com.jasamedika.medifirst2000.service.IndikatorBIOSService;
import com.jasamedika.medifirst2000.util.rest.RestUtil;
import com.jasamedika.medifirst2000.vo.IndikatorBIOSCustomVO;
import com.jasamedika.medifirst2000.vo.IndikatorBIOSVO;

@RestController
@RequestMapping("/bios")

public class BridgingBiosController extends LocaleController<IndikatorBIOSVO>{
	
	private static final Logger LOGGER = LoggerFactory.getLogger(BridgingBiosController.class);
	
	@Autowired
	private BridgingDao bridgingDao;
	
	@Autowired
	private IndikatorBIOSService indikatorBIOSService;

		@RequestMapping(value = "/profile", method = RequestMethod.GET, produces = { MediaType.APPLICATION_JSON_VALUE })
		public Map<String, Object> biosProfile(@RequestParam(value = "param", required = false) String param1,
				HttpServletResponse response) throws IOException {
		List<Profile> x = bridgingDao.biosProfile();
		String text ="";
		if (x.size()>0){
			for(int i=0;i<x.size();i++){
				text=(x.get(i).getAlamatLengkap())+"|";
				text=text+(x.get(i).getKdPos())+"|";
				text=text+(x.get(i).getFixedPhone())+"|";
				text=text+(x.get(i).getFaksimile())+"|";
				text=text+(x.get(i).getAlamatEmail())+"|";
				text=text+(x.get(i).getWebsite());
			}			
		}
		//harus diubah menjadi downloadable
		   
		String fileName = null;
		 
		String osname = System.getProperty("os.name", "").toLowerCase();
		if (osname.startsWith("windows")) {
			fileName = "d:/profile.txt";
		} else if (osname.startsWith("linux")) {
			fileName = "/root/profile.txt";
		} else {
			System.out.println("Sorry, your operating system is different");
		}

		FileWriter fw = new FileWriter(fileName);
		BufferedWriter output = new BufferedWriter(fw);
		output.write(text);
		output.flush();
		output.close();
		// FileOutputStream out = new FileOutputStream(fileName);
		String fileType = "application/text";
		response.setContentType(fileType);
		// Make sure to show the download dialog
		response.setHeader("Content-disposition", "attachment; filename=" + fileName);
		File my_file = new File(fileName);
		// This should send the file to browser
		OutputStream out = response.getOutputStream();
		FileInputStream in = new FileInputStream(my_file);
		byte[] buffer = new byte[4096];
		int length;
		while ((length = in.read(buffer)) > 0) {
			out.write(buffer, 0, length);
		}
		in.close();
		out.flush();

		return null;
	}

	@RequestMapping(value = "/sk", method = RequestMethod.GET, produces = { MediaType.APPLICATION_JSON_VALUE })
	public List<Object[]> biosSK(){
		//List<Object[]> x = bridgingDao.biosSK();
		return null;
	}
	
	@RequestMapping(value = "/save-indikator-bios", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String, Object>> saveIndikatorBIOS(@Valid @RequestBody IndikatorBIOSCustomVO vo,
			HttpServletRequest request) throws ParseException {
		try {
			SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy");

			Map<String, Object> result = indikatorBIOSService.saveIndikatorBIOS(vo);
			if (null != result)
				mapHeaderMessage.put(WebConstants.HttpHeaderInfo.LABEL_SUCCESS,
						getMessage(MessageResource.LABEL_SUCCESS, request));
			return RestUtil.getJsonResponse(result, HttpStatus.CREATED, mapHeaderMessage);

		} catch (ServiceVOException e) {
			LOGGER.error("Got exception {} when add Indikator", e.getMessage());
			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE, e.getMessage());
			return RestUtil.getJsonHttptatus(HttpStatus.INTERNAL_SERVER_ERROR, mapHeaderMessage);
		} catch (JpaSystemException jse) {
			LOGGER.error("Got exception {} when add Indikator", jse.getMessage());
			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE, jse.getMessage());
			return RestUtil.getJsonHttptatus(HttpStatus.CONFLICT, mapHeaderMessage);
		}

	}
	
	@RequestMapping(value = "/save-indikator-bios-pengeluaran", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String, Object>> saveIndikatorBIOS(@Valid @RequestBody List<IndikatorBIOSVO> vo,
			HttpServletRequest request) throws ParseException {
		try {
			SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy");

			Map<String, Object> result = indikatorBIOSService.saveIndikatorBIOSPengeluaran(vo);
			if (null != result)
				mapHeaderMessage.put(WebConstants.HttpHeaderInfo.LABEL_SUCCESS,
						getMessage(MessageResource.LABEL_SUCCESS, request));
			return RestUtil.getJsonResponse(result, HttpStatus.CREATED, mapHeaderMessage);

		} catch (ServiceVOException e) {
			LOGGER.error("Got exception {} when add Indikator", e.getMessage());
			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE, e.getMessage());
			return RestUtil.getJsonHttptatus(HttpStatus.INTERNAL_SERVER_ERROR, mapHeaderMessage);
		} catch (JpaSystemException jse) {
			LOGGER.error("Got exception {} when add Indikator", jse.getMessage());
			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE, jse.getMessage());
			return RestUtil.getJsonHttptatus(HttpStatus.CONFLICT, mapHeaderMessage);
		}

	}
	
	@RequestMapping(value = "/get-layanan-lainnya", method = RequestMethod.GET)
	public ResponseEntity<List<Map<String, Object>>> getBIOSLayananLainnyaOLD( @RequestParam(value = "tanggal_update", required = false) String tanggal_update,
			HttpServletRequest request) {
		try {			
			List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
			result = indikatorBIOSService.layananLainBIOS(tanggal_update);
			mapHeaderMessage.put(WebConstants.HttpHeaderInfo.LABEL_SUCCESS,
					getMessage(MessageResource.LABEL_SUCCESS, request));
			return RestUtil.getNewJsonResponse(result, HttpStatus.OK, mapHeaderMessage,"layanan_lainnya");
		} catch (ServiceVOException e) {
			LOGGER.error("Got exception {} when getAllPegawaiAktifVerfiedByUnitKerja", e.getMessage());
			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE, e.getMessage());
			return RestUtil.getJsonHttptatus(HttpStatus.INTERNAL_SERVER_ERROR, mapHeaderMessage);
		} catch (JpaSystemException jse) {
			LOGGER.error("Got exception {} when getAllPegawaiAktifVerfiedByUnitKerja", jse.getMessage());
			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE, jse.getMessage());
			return RestUtil.getJsonHttptatus(HttpStatus.CONFLICT, mapHeaderMessage);
		}
	}
	
	@RequestMapping(value = "/get-layanan-kesehatan", method = RequestMethod.GET)
	public ResponseEntity<List<Map<String, Object>>> getBIOSLayananKesehatanOLD( @RequestParam(value = "tanggal_update", required = false) String tanggal_update,
			HttpServletRequest request) {
		try {			
			List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
			result = indikatorBIOSService.layananKesehatanBIOS(tanggal_update);
			mapHeaderMessage.put(WebConstants.HttpHeaderInfo.LABEL_SUCCESS,
					getMessage(MessageResource.LABEL_SUCCESS, request));
			return RestUtil.getNewJsonResponse(result, HttpStatus.OK, mapHeaderMessage,"layanan_kesehatan");
		} catch (ServiceVOException e) {
			LOGGER.error("Got exception {} when getAllPegawaiAktifVerfiedByUnitKerja", e.getMessage());
			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE, e.getMessage());
			return RestUtil.getJsonHttptatus(HttpStatus.INTERNAL_SERVER_ERROR, mapHeaderMessage);
		} catch (JpaSystemException jse) {
			LOGGER.error("Got exception {} when getAllPegawaiAktifVerfiedByUnitKerja", jse.getMessage());
			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE, jse.getMessage());
			return RestUtil.getJsonHttptatus(HttpStatus.CONFLICT, mapHeaderMessage);
		}
	}
	
	@RequestMapping(value = "/penerimaan", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<List<Map<String, Object>>> getPenerimaan(@RequestParam(value = "TanggalUpdate", required = false) String tanggal,
			HttpServletRequest request) throws ParseException {
		try {
			

			//List<Map<String, Object>> result = indikatorBIOSService.penerimaanBLU(tanggal);
			List<Map<String, Object>> result = indikatorBIOSService.penerimaanPengeluaranBLUNew(tanggal, "Penerimaan");
			
			mapHeaderMessage.put(WebConstants.HttpHeaderInfo.LABEL_SUCCESS,
					getMessage(MessageResource.LABEL_SUCCESS, request));
			return RestUtil.getNewJsonResponse(result, HttpStatus.OK, mapHeaderMessage,"Penerimaan");

		} catch (ServiceVOException e) {
			LOGGER.error("Got exception {} when add Indikator", e.getMessage());
			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE, e.getMessage());
			return RestUtil.getJsonHttptatus(HttpStatus.INTERNAL_SERVER_ERROR, mapHeaderMessage);
		} catch (JpaSystemException jse) {
			LOGGER.error("Got exception {} when add Indikator", jse.getMessage());
			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE, jse.getMessage());
			return RestUtil.getJsonHttptatus(HttpStatus.CONFLICT, mapHeaderMessage);
		}

	}
	
	@RequestMapping(value = "/pengeluaran", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<List<Map<String, Object>>> getPengeluaran(@RequestParam(value = "TanggalUpdate", required = false) String tanggal,
			HttpServletRequest request) throws ParseException {
		try {
			

			//List<Map<String, Object>> result = indikatorBIOSService.pengeluaranBLU(tanggal);
			List<Map<String, Object>> result = indikatorBIOSService.penerimaanPengeluaranBLUNew(tanggal, "Pengeluaran");
			mapHeaderMessage.put(WebConstants.HttpHeaderInfo.LABEL_SUCCESS,
					getMessage(MessageResource.LABEL_SUCCESS, request));
			return RestUtil.getNewJsonResponse(result, HttpStatus.OK, mapHeaderMessage,"Pengeluaran");

		} catch (ServiceVOException e) {
			LOGGER.error("Got exception {} when add Indikator", e.getMessage());
			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE, e.getMessage());
			return RestUtil.getJsonHttptatus(HttpStatus.INTERNAL_SERVER_ERROR, mapHeaderMessage);
		} catch (JpaSystemException jse) {
			LOGGER.error("Got exception {} when add Indikator", jse.getMessage());
			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE, jse.getMessage());
			return RestUtil.getJsonHttptatus(HttpStatus.CONFLICT, mapHeaderMessage);
		}

	}
	
	@RequestMapping(value = "/saldo", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<List<Map<String, Object>>> getSaldo(@RequestParam(value = "TanggalUpdate", required = false) String tanggal,
			HttpServletRequest request) throws ParseException {
		try {
			

			//List<Map<String, Object>> result = indikatorBIOSService.saldoBLU(tanggal);
			List<Map<String, Object>> result = indikatorBIOSService.saldoBLUNew(tanggal);
			mapHeaderMessage.put(WebConstants.HttpHeaderInfo.LABEL_SUCCESS,
					getMessage(MessageResource.LABEL_SUCCESS, request));
			return RestUtil.getNewJsonResponse(result, HttpStatus.OK, mapHeaderMessage,"SaldoBLU");

		} catch (ServiceVOException e) {
			LOGGER.error("Got exception {} when add Indikator", e.getMessage());
			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE, e.getMessage());
			return RestUtil.getJsonHttptatus(HttpStatus.INTERNAL_SERVER_ERROR, mapHeaderMessage);
		} catch (JpaSystemException jse) {
			LOGGER.error("Got exception {} when add Indikator", jse.getMessage());
			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE, jse.getMessage());
			return RestUtil.getJsonHttptatus(HttpStatus.CONFLICT, mapHeaderMessage);
		}

	}
	
	@RequestMapping(value = "/get-layanan-lainnya-old", method = RequestMethod.GET)
	public ResponseEntity<List<Map<String, Object>>> getBIOSLayananLainnya( @RequestParam(value = "tanggal_update", required = false) String tanggal_update,
			HttpServletRequest request) {
		try {			
			List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
			result = indikatorBIOSService.layananLainBIOSNew(tanggal_update);
			mapHeaderMessage.put(WebConstants.HttpHeaderInfo.LABEL_SUCCESS,
					getMessage(MessageResource.LABEL_SUCCESS, request));
			return RestUtil.getNewJsonResponse(result, HttpStatus.OK, mapHeaderMessage,"layanan_lainnya");
		} catch (ServiceVOException e) {
			LOGGER.error("Got exception {} when getAllPegawaiAktifVerfiedByUnitKerja", e.getMessage());
			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE, e.getMessage());
			return RestUtil.getJsonHttptatus(HttpStatus.INTERNAL_SERVER_ERROR, mapHeaderMessage);
		} catch (JpaSystemException jse) {
			LOGGER.error("Got exception {} when getAllPegawaiAktifVerfiedByUnitKerja", jse.getMessage());
			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE, jse.getMessage());
			return RestUtil.getJsonHttptatus(HttpStatus.CONFLICT, mapHeaderMessage);
		}
	}
	
	@RequestMapping(value = "/get-layanan-kesehatan-old", method = RequestMethod.GET)
	public ResponseEntity<List<Map<String, Object>>> getBIOSLayananKesehatan( @RequestParam(value = "tanggal_update", required = false) String tanggal_update,
			HttpServletRequest request) {
		try {			
			List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
			result = indikatorBIOSService.layananKesehatanBIOSNew(tanggal_update);
			mapHeaderMessage.put(WebConstants.HttpHeaderInfo.LABEL_SUCCESS,
					getMessage(MessageResource.LABEL_SUCCESS, request));
			return RestUtil.getNewJsonResponse(result, HttpStatus.OK, mapHeaderMessage,"layanan_kesehatan");
		} catch (ServiceVOException e) {
			LOGGER.error("Got exception {} when getAllPegawaiAktifVerfiedByUnitKerja", e.getMessage());
			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE, e.getMessage());
			return RestUtil.getJsonHttptatus(HttpStatus.INTERNAL_SERVER_ERROR, mapHeaderMessage);
		} catch (JpaSystemException jse) {
			LOGGER.error("Got exception {} when getAllPegawaiAktifVerfiedByUnitKerja", jse.getMessage());
			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE, jse.getMessage());
			return RestUtil.getJsonHttptatus(HttpStatus.CONFLICT, mapHeaderMessage);
		}
	}
	
}
