package com.zehong.framework.interceptor;

import com.hikvision.artemis.sdk.config.ArtemisConfig;
import com.zehong.common.utils.StringUtils;
import com.zehong.framework.config.HikvisionProperties;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ArtemisInterceptor implements HandlerInterceptor {

    private final HikvisionProperties properties;

    public ArtemisInterceptor(HikvisionProperties properties) {
        this.properties = properties;
    }



    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String path = request.getRequestURI();
        // 只拦截以 /artemis 开头的请求
        if (path.contains("/artemis")) {
            String platform = request.getParameter("platform");
            HikvisionProperties.PlatformConfig config = StringUtils.isNotEmpty(platform) ? properties.getPlatformConfig(platform) : properties.getPlatformConfig("huaxin");
            // 设置 ArtemisConfig（需要加锁）
            synchronized (ArtemisInterceptor.class) {
                // 代理API网关nginx服务器ip端口
                ArtemisConfig.host = config.getHost();
                // 秘钥appkey
                ArtemisConfig.appKey = config.getAppKey();
                // 秘钥appSecret
                ArtemisConfig.appSecret = config.getAppSecret();
            }
        }
        return true;
    }
}

