package com.oqpo.api.web.resolver;

import com.oqpo.api.config.JwtTokenProvider;
import com.oqpo.api.web.dto.response.token.TokenResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.MethodParameter;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

import javax.servlet.http.HttpServletRequest;

@Component
public class ArgumentResolver implements HandlerMethodArgumentResolver {
    @Autowired
    private JwtTokenProvider jwtTokenProvider;

    private final String USER_ID = "userId";
    private final String SYSTEM_ADMIN_YN = "systemAdminYn";
    private final String SUPER_ADMIN_YN = "superAdminYn";
    private final String AUTH_TP_CD = "authTpCd";
    private final String AFFL_SHOP_ID = "afflShopId";
    private final String BRAND_ID = "brandId";
    private final String AUTH_TP_NM = "authTpNm";

    @Override
    public boolean supportsParameter(MethodParameter parameter) {
        if (parameter.getParameterName().equals(USER_ID)) {
            return true;
        } else if (parameter.getParameterName().equals(SYSTEM_ADMIN_YN)) {
            return true;
        } else if (parameter.getParameterName().equals(SUPER_ADMIN_YN)) {
            return true;
        }  else if (parameter.getParameterName().equals(AUTH_TP_CD)) {
            return true;
        }  else if (parameter.getParameterName().equals(AFFL_SHOP_ID)) {
            return true;
        }  else if (parameter.getParameterName().equals(BRAND_ID)) {
            return true;
        }  else if (parameter.getParameterName().equals(AUTH_TP_NM)) {
            return true;
        }  else if (parameter.getParameterType() == TokenResponse.class) {
            return true;
        }
        return false;
    }

    @Override
    public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
        try {
            HttpServletRequest request = webRequest.getNativeRequest(HttpServletRequest.class);
            String token = jwtTokenProvider.resolveToken(request);
            TokenResponse tokenResponse = jwtTokenProvider.getTokenInfo(token);

            if (tokenResponse == null) return null;

            if (parameter.getParameterName().equals(USER_ID)) return tokenResponse.getUserId();
            if (parameter.getParameterName().equals(SYSTEM_ADMIN_YN)) return tokenResponse.getSystemAdminYn();
            if (parameter.getParameterName().equals(SUPER_ADMIN_YN)) return tokenResponse.getSuperAdminYn();
            if (parameter.getParameterName().equals(AUTH_TP_CD)) return tokenResponse.getAuthTpCd();
            if (parameter.getParameterName().equals(AFFL_SHOP_ID)) return tokenResponse.getAfflShopId();
            if (parameter.getParameterName().equals(BRAND_ID)) return tokenResponse.getBrandId();
            if (parameter.getParameterName().equals(AUTH_TP_NM)) return tokenResponse.getAuthTpNm();
            return tokenResponse;
        } catch(Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
