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.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

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.controller.base.LocaleController;
import com.jasamedika.medifirst2000.dao.BridgingDao;
import com.jasamedika.medifirst2000.entities.Profile;
import com.jasamedika.medifirst2000.vo.IndikatorBIOSVO;

@RestController
@RequestMapping("/bios")

public class BridgingBiosController extends LocaleController<IndikatorBIOSVO> {

	@Autowired
	private BridgingDao bridgingDao;

	@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;
	}

}
