package cn.com.feishu.provider;

import cn.com.ctop.common.module.utils.HttpUtils;
import cn.com.feishu.constant.FeishuConstant;
import com.alibaba.fastjson.JSONObject;

import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URLEncoder;

public class AuthProvider {
    /**
     *  获取授权url
     * @param state
     * @return
     * @throws UnsupportedEncodingException
     * @throws MalformedURLException
     */
    public static String getAuthUrl(String state) throws UnsupportedEncodingException {
        String redirectUrl = FeishuConstant.REDIRECT_URL;
        return FeishuConstant.FEISHU_API_URL+FeishuConstant.AUTH_INDEX+"?redirect_uri="+ URLEncoder.encode(redirectUrl,"UTF-8")+"&app_id="+FeishuConstant.FEISHU_APP_ID+"&state="+state;
    }

    /**
     * 根据code获取用户登录信息
     * @param code
     * @return
     * @throws Exception
     */
    public static JSONObject getFeishuUserInfo(String code) throws Exception {
        JSONObject params = new JSONObject();
        params.put("app_access_token",getAppAccessToken());
        params.put("grant_type","authorization_code");
        params.put("code",code);
        return HttpUtils.feishuPostRequest(FeishuConstant.FEISHU_API_URL+FeishuConstant.AUTH_ACCESS_TOKEN,params);
    }

    public static JSONObject refreshAccessToken(String refreshToken) throws Exception{
        JSONObject params = new JSONObject();
        params.put("app_access_token",getAppAccessToken());
        params.put("grant_type","refresh_token");
        params.put("refresh_token",refreshToken);
        return HttpUtils.feishuPostRequest(FeishuConstant.FEISHU_API_URL+FeishuConstant.REFRESH_AUTH_ACCESS_TOKEN,params);
    }

    public static JSONObject getUserInfo(String accessToken){
        JSONObject params = new JSONObject();
        params.put("user_access_token",accessToken);
        return HttpUtils.feishuGetRequest("Bearer "+accessToken,FeishuConstant.FEISHU_API_URL+FeishuConstant.USER_INFO,params);
    }

    /**
     * 获取企业自建应用token信息
     * 调用接口获取应用资源时使用
     * @return
     * @throws Exception
     */
    public static String getAppAccessToken() throws Exception {
        JSONObject params = new JSONObject();
        params.put("app_id",FeishuConstant.FEISHU_APP_ID);
        params.put("app_secret",FeishuConstant.FEISHU_APP_SECRET);
        JSONObject resultNode = HttpUtils.feishuPostRequest(FeishuConstant.FEISHU_API_URL+FeishuConstant.APP_ACCESS_TOKEN,params);
        if (resultNode.getIntValue("code")==0){
            return resultNode.getString("app_access_token");
        }else {
            throw new Exception();
        }
    }

    /**
     * 获取企业自建应用租户token
     * 调用接口获取企业资源时使用
     * @return
     * @throws Exception
     */
    public static String getTenantAccessToken() throws Exception {
        JSONObject params = new JSONObject();
        params.put("app_id",FeishuConstant.FEISHU_APP_ID);
        params.put("app_secret",FeishuConstant.FEISHU_APP_SECRET);
        JSONObject resultNode = HttpUtils.feishuPostRequest(FeishuConstant.FEISHU_API_URL+FeishuConstant.TENANT_ACCESS_TOKEN,params);
        if (resultNode.getIntValue("code")==0){
            return resultNode.getString("tenant_access_token");
        }else {
            throw new Exception();
        }
    }
}
