package com.caimei.www.controller;

import com.caimei.www.pojo.page.BaseLink;
import com.caimei.www.pojo.page.TopMenu;
import com.caimei.www.service.page.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;

import java.util.List;

/**
 * 页面全局变量
 *
 * @author : Charles
 * @date : 2020/6/18
 */
@Controller
public class BaseController {
	@Value("${caimei.spiServer}")
    private String spiServer;
    /** 打包时间 */
    @Value("${spring.application.build-time}")
    private String buildTime;
    /** 错误页面 */
	private static final String ERROR_PATH = "error/404";

    private BaseService baseService;
    @Autowired
    public void setBaseService(BaseService baseService) {
        this.baseService = baseService;
    }

	/**
	 * 页面公共请求
	 * @param model
	 * @param request
	 * @return
	 */
	@ModelAttribute
	public Model init(final Model model, ServerHttpRequest request) {
		// 判断请求设备
		List<String> strings = request.getHeaders().get("User-Agent");
		String agent = strings!=null ? strings.get(0) : "";
		model.addAttribute("agent", agent);
	    // 静态文件版本号
	    model.addAttribute("version", buildTime);
		// spi服务器地址
		model.addAttribute("spiServer", spiServer);
		// 搜索热门关键字
		List<String> searchHotWord = baseService.getSearchHotWord();
		model.addAttribute("searchHotWord", searchHotWord);
		// 头部菜单
		List<TopMenu> menuList = baseService.getNavMenu();
		model.addAttribute("topMenuList", menuList);
		// 底部帮助页
		List<BaseLink> helpPages = baseService.getHelpPages();
		model.addAttribute("helpPages", helpPages);
		// 友情链接
		List<BaseLink> friendLinks = baseService.getFriendLinks();
		model.addAttribute("friendLinks", friendLinks);

		return model;
	}

	protected String errorPath(){
		return ERROR_PATH;
	}
}
