/*******************************************************************************
 * Copyright 2016 2Fi Business Solutions Ltd.
 *
 * This code is part of the SPCS project.
 *
 * This code is copyrighted. Under no circumstances should any party, people,
 * or organization should redistribute any portions of this code in any form,
 * either verbatim or through electronic media, to any third parties, unless
 * under explicit written permission by 2Fi Business Solutions Ltd.
 ******************************************************************************/
package com.ffii.tbms.root.web;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
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.servlet.i18n.SessionLocaleResolver;
import org.springframework.web.util.WebUtils;

import com.ffii.core.utils.Params;
import com.ffii.core.utils.SecurityUtils;
import com.ffii.core.web.AbstractController;
import com.ffii.core.web.view.json.JsonView;
import com.ffii.core.utils.LocaleUtils;
import com.ffii.tbms.user.service.UserService;

@Controller
public class LocaleController extends AbstractController {
	@Autowired
	private UserService userService;

	@RequestMapping(value = "/locale", method = RequestMethod.GET)
	public String locale(Model model, HttpServletRequest request, @RequestParam String locale) {
		WebUtils.setSessionAttribute(request, SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME, LocaleUtils.toLocale(locale));

		if (SecurityUtils.getUser() != null) userService.changeLocale(SecurityUtils.getUser().getId(), locale);
		model.addAttribute(Params.SUCCESS, Boolean.TRUE);
		return JsonView.NAME;
	}

}