package com.xjrsoft.common.utils;


import com.aliyun.dingtalkoauth2_1_0.models.GetUserTokenRequest;
import com.aliyun.dingtalkoauth2_1_0.models.GetUserTokenResponse;
import lombok.SneakyThrows;
import org.springframework.stereotype.Component;

@Component
public class DingtalkUtil {


    /**
     * 使用 Token 初始化账号Client
     * @return Client
     * @throws Exception
     */
    public static com.aliyun.dingtalkcontact_1_0.Client createClient() throws Exception {
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config();
        config.protocol = "https";
        config.regionId = "central";
        return new com.aliyun.dingtalkcontact_1_0.Client(config);
    }

    @SneakyThrows
    public static GetUserTokenResponse getToken(String unionId,String code){



        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config();
        config.protocol = "https";
        config.regionId = "central";
        com.aliyun.dingtalkoauth2_1_0.Client client = new com.aliyun.dingtalkoauth2_1_0.Client(config);

        GetUserTokenRequest getUserTokenRequest = new GetUserTokenRequest()
                .setClientId("dingaex2gok1rllumlqs")
                .setClientSecret("Nv2pe-UoR0Z_Iw8d29laZfL3kH6ElmhZpRdPwI7SvmKJmaCI29qCrSlFEiMM88MB")
                .setCode(code)
                .setRefreshToken(code)
                .setGrantType("authorization_code");

        return client.getUserToken(getUserTokenRequest);
    }


//    //发送消息给钉钉用户
//    public Boolean sendMessage(XjrOaMessage xjrOaMessage,String userId) {
//        String token = this.getToken();
//        try {
//            DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2");
//            OapiMessageCorpconversationAsyncsendV2Request request = new OapiMessageCorpconversationAsyncsendV2Request();
//            request.setAgentId(839967779L);
//            request.setUseridList(userId);
//            request.setToAllUser(false);
//
//            OapiMessageCorpconversationAsyncsendV2Request.Msg msg = new OapiMessageCorpconversationAsyncsendV2Request.Msg();
//            msg.setMsgtype("text");
//            msg.setText(new OapiMessageCorpconversationAsyncsendV2Request.Text());
//            msg.getText().setContent(xjrOaMessage.getContent());
//            request.setMsg(msg);
//            OapiMessageCorpconversationAsyncsendV2Response rsp = client.execute(request, token);
//        } catch (ApiException e) {
//            e.printStackTrace();
//        }
//        return true;
//    }
}
