package com.backendsys.modules.common.aspect;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.backendsys.modules.cms.navigation.entity.Navigation;
import com.backendsys.modules.cms.navigation.service.NavigationService;
import com.backendsys.modules.common.config.security.utils.HttpRequestUtil;
import com.backendsys.modules.common.utils.CookieUtil;
import com.backendsys.modules.system.service.SysLanguageService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.ui.Model;
import com.backendsys.modules.cms.siteinfo.entity.SiteInfo;
import com.backendsys.modules.cms.siteinfo.service.SiteInfoService;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.method.HandlerMethod;

import java.io.IOException;
import java.lang.reflect.Method;
import java.util.List;

/**
 * 视图自动添加：站点信息、导航
 */
@Aspect
@Component
public class PagesAspect {

	@Autowired
	private HttpRequestUtil httpRequestUtil;

	@Autowired
	private SysLanguageService sysLanguageService;
	@Autowired
	private SiteInfoService siteInfoService;
	@Autowired
	private NavigationService navigationService;

	@Before("@annotation(org.springframework.web.bind.annotation.GetMapping) && @annotation(pages)")
	public void beforeGet(JoinPoint joinPoint, Pages pages) throws IOException {

		HttpServletRequest request = httpRequestUtil.getRequest();
		HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();

		// 从 JoinPoint 获取控制器方法的参数
		Object[] args = joinPoint.getArgs();
		for (Object arg : args) {
			if (arg instanceof Model) {
				Model model = (Model) arg;

				// [Cookie] 获取/设置默认语言 (默认是：中文)
				String lang = CookieUtil.getCurrentLang(request);
				model.addAttribute("lang", lang);

				// [Get] 获取系统语言列表
				model.addAttribute("sysLanguage", sysLanguageService.selectLanguageList());

				// [Get] 获取站点信息
				SiteInfo siteInfo = siteInfoService.selectSiteInfo();
				if (siteInfo == null) {
					request.setAttribute("errorMessage", "站点信息不存在 " + "(" + lang + ")");
					response.sendRedirect(request.getContextPath() + "/error");
				}
				model.addAttribute("siteInfo", siteInfoService.selectSiteInfo());

				// [Get] 获取导航
				model.addAttribute("navigation", navigationService.selectNavigationTemplate(lang));

				// 其他信息
				model.addAttribute("timestamp", DateUtil.current());            			// 当前时间戳
				model.addAttribute("request_uri", request.getRequestURI());                 // 当前访问路径
				model.addAttribute("request_query_string", request.getQueryString());    	// 当前访问路径参数

				break;
			}
		}

	}

}
