package controller.rest;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
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.ResponseBody;

import bean.AppVariables;
import bean.ParamBean;
import common.controller.BaseController;
import dao.ParamDAO;

@Controller
@RequestMapping("param")
public class ParamRestController extends BaseController {

	@Autowired
	ParamDAO paramDAO;
	
	@RequestMapping(value="", method=RequestMethod.GET)
	@ResponseBody
	private ResponseEntity<Object> getParams(HttpServletRequest request) {
		setRequest(request);
		if (getUserData()==null)
			return new ResponseEntity<Object>(null, HttpStatus.UNAUTHORIZED);
		else {
			AppVariables appVariables = (AppVariables) getSessionAttribute("appVariables");
			ParamBean params = paramDAO.getParams(appVariables);
			if (params!=null)
				return new ResponseEntity<Object>(params, HttpStatus.OK);
			else
				return new ResponseEntity<Object>(null, HttpStatus.INTERNAL_SERVER_ERROR);
		}			
	}
	
	@RequestMapping(value="", method=RequestMethod.POST)
	@ResponseBody
	private ResponseEntity<Object> setParams(@RequestBody ParamBean params, HttpServletRequest request) {
		setRequest(request);
		if (getUserData()==null)
			return new ResponseEntity<Object>(null, HttpStatus.UNAUTHORIZED);
		else {
			AppVariables appVariables = (AppVariables) getSessionAttribute("appVariables");
			String res = paramDAO.setParams(params, appVariables);
			return new ResponseEntity<Object>(null, HttpStatus.OK);
		}				
	}
	
}
