package com.javahonk.controller;

import java.io.File;

import org.apache.log4j.Logger;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class SpringMVCController {

	private static final Logger logger = Logger
			.getLogger(SpringMVCController.class);

	@RequestMapping(value = "/firstPage")
	public String firstPage(ModelMap model) {

		logger.info("Log4j info is working");

		String path = Thread.currentThread().getContextClassLoader().getResource("Sample.xml").getPath();
		File file = new File(path);
		String absoluteFilePath = file.getAbsolutePath();
		System.out.println(absoluteFilePath);
		model.addAttribute("message", "Spring MVC RESTFul Service");

		return "index";
	}

	@RequestMapping(value = "/getSampleJSONData/{name}")
	public @ResponseBody
	String getSampleJSONData(@PathVariable String name) {

		JSONObject jo = new JSONObject();
		jo.put("values", new JSONArray(new Long[][] { { 1025409600000L, 23L },
				{ 1028088000000L, 19L }, { 1030766400000L, 21L },
				{ 1033358400000L, 22L } }));
		jo.put("key", "North America");

		JSONArray ja = new JSONArray();

		for (int i = 0; i < 10; i++) {
			ja.put(jo);
		}

		return ja.toString();
	}

}
