/*******************************************************************************
 * Copyright 2019 2Fi Business Solutions Ltd.
 *
 *
 *
 * 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 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 javax.servlet.http.HttpServletRequest;

import com.ffii.core.Settings;
import com.ffii.core.setting.service.SettingsService;
import com.ffii.core.utils.StringUtils;
import com.ffii.core.web.AbstractController;
import com.ffii.core.web.view.json.JsonView;

@Controller
public class HomeController extends AbstractController {

	@Autowired
	private SettingsService settingsService;

	@RequestMapping(value = "/", method = RequestMethod.GET)
	public String root(Model model) throws Exception {
		return "redirect:/home";
	}

	@RequestMapping(value = "/home", method = RequestMethod.GET)
	public String home(Model model, HttpServletRequest request) throws Exception {
		if (StringUtils.equals(request.getLocalAddr(), request.getRemoteAddr())) {
			model.addAttribute("jsv", String.valueOf(System.currentTimeMillis()));
		} else {
			model.addAttribute("jsv", settingsService.getString(Settings.JS_VERSION, "1"));
		}
		return "root/home";
	}

	@RequestMapping(value = "/get-sys-time", method = { RequestMethod.GET, RequestMethod.POST })
	public String getSysTime(Model model) throws Exception {
		model.addAttribute("sysTime", System.currentTimeMillis());
		return JsonView.NAME;
	}
	@RequestMapping(value = "/home/old-viewport", method = RequestMethod.GET)
	public String newViewport(Model model) throws Exception {
		return "root/oldhome";
	}

}