package com.shanghaichengdi.ghjgitem.config;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.shanghaichengdi.ghjgitem.constant.Constants;
import com.shanghaichengdi.ghjgitem.vo.response.ReturnMsg;
import com.shanghaichengdi.ghjgitem.domain.SysDatashareAuthNew;
import com.shanghaichengdi.ghjgitem.domain.SysDatashareServiceNew;
import com.shanghaichengdi.ghjgitem.domain.SysDatashareUser;
import com.shanghaichengdi.ghjgitem.service.RedisService;
import com.shanghaichengdi.ghjgitem.service.SysDatashareAuthNewService;
import com.shanghaichengdi.ghjgitem.service.SysDatashareServiceNewService;
import com.shanghaichengdi.ghjgitem.util.CharsetKit;
import java.util.List;
import java.util.Objects;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.web.servlet.HandlerInterceptor;

@Slf4j
@Component
public class PermissionsVerifyInterceptor implements HandlerInterceptor {

  @Resource
  private RedisService redisService;
  @Resource
  private SysDatashareAuthNewService authNewService;
  @Resource
  private SysDatashareServiceNewService serviceNewService;

  @Override
  public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
      throws Exception {
    String token = request.getHeader("Authorization");
    String valueString = redisService.getValue(Constants.TOKEN + Constants.SPLIT, token);
    SysDatashareUser user = JSONObject.parseObject(valueString, SysDatashareUser.class);
    String requestURI = request.getRequestURI().replace("/ghThreeDataShare", "");
    List<SysDatashareServiceNew> list = serviceNewService.list(
        Wrappers.<SysDatashareServiceNew>lambdaQuery()
            .eq(SysDatashareServiceNew::getName, requestURI));
    if (CollectionUtils.isEmpty(list)) {
      response.setCharacterEncoding(CharsetKit.UTF_8);
      response.setContentType("application/json;charset=utf-8");
      response.setHeader("Access-Control-Allow-Origin", "*");
      response.setHeader("Cache-Control", "no-cache");
      response.setStatus(HttpServletResponse.SC_NOT_FOUND);
      ReturnMsg errorMsg = ReturnMsg.error(HttpServletResponse.SC_NOT_FOUND,
          "接口" + requestURI + "不存在,请检查接口大小写输入是否正确");
      response.getWriter().write(JSONObject.toJSONString(errorMsg));
      return false;
    }
    SysDatashareAuthNew relevancy = authNewService.selectUserServiceRelevancy(
        user.getId(), requestURI);
    if (Objects.isNull(relevancy)) {
      response.setCharacterEncoding(CharsetKit.UTF_8);
      response.setContentType("application/json;charset=utf-8");
      response.setHeader("Access-Control-Allow-Origin", "*");
      response.setHeader("Cache-Control", "no-cache");
      response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
      ReturnMsg errorMsg = ReturnMsg.error(HttpServletResponse.SC_UNAUTHORIZED,
          "您没有访问接口" + requestURI + "的权限");
      response.getWriter().write(JSONObject.toJSONString(errorMsg));
      return false;
    }
    return true;
  }
}
