package com.weaver.seconddev.chapanda.beisen.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.weaver.seconddev.chapanda.beisen.constant.Constants;
import okhttp3.*;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;


@Component
public class BeisenQueryUseridUtil {
    private final static Logger log = LoggerFactory.getLogger(BeisenQueryUseridUtil.class);

    @Autowired
    Token2BeiSenUtil token2BeiSenUtil ;

    public String queryBeisenUserIdByEmail(String email){
        String userid = "";
        if(StringUtils.isNotEmpty(email)) {
            String token = token2BeiSenUtil.getToken();
            if (StringUtils.isNotBlank(token)) {
                userid = queryUserIdByEmail(token, email);
            }
        }
        return userid;
    }


    public String queryBeisenUserIdByJobNum(String jobNum){
        String userid = "";
        if(StringUtils.isNotEmpty(jobNum)) {
            String token = token2BeiSenUtil.getToken();
            if (StringUtils.isNotBlank(token)) {
                userid = queryUserIdByJobNum(token, jobNum);
            }
        }
        return userid;
    }

    /***
     *
     * @param token
     * @param email
     * @return
     */
    private String queryUserIdByEmail(String token,String email){
        String userid = "";
        OkHttpClient client = new OkHttpClient().newBuilder()
                .build();
        MediaType mediaType = MediaType.parse("application/json");
        JSONObject dataJson = new JSONObject();
        dataJson.put("email",email);
        RequestBody body = RequestBody.create(mediaType, dataJson.toJSONString());
        try {
            Request request = new Request.Builder()
                    .url(Constants.beishenHost+Constants.queryUserIdByEmailUrl)
                    .method("POST", body)
                    .addHeader("Authorization", "Bearer "+token)
                    .addHeader("Content-Type", "application/json")
                    .build();
            Response response = client.newCall(request).execute();

            int code = response.code();
            String msg = response.body().string();
            log.error("code:{}",code);
            log.error("msg:{}",msg);
            if(code == 200) {
                if(StringUtils.isNotBlank(msg)){
                    JSONObject bodyJson = JSONObject.parseObject(msg);
                    if(bodyJson.containsKey("data")){
                        userid = bodyJson.getString("data");
                    }
                }
            }
        } catch (IOException e) {
            log.error("e:{}",e);
        }
        log.error("userid:{}",userid);
        return userid;
    }

    /***
     *
     * @param token
     * @param jobNum
     * @return
     */
    private String queryUserIdByJobNum(String token,String jobNum){
        String userid = "";
        OkHttpClient client = new OkHttpClient().newBuilder()
                .build();
        MediaType mediaType = MediaType.parse("application/json");
        RequestBody body = RequestBody.create(mediaType, "{\n    \"jobNumbers\":[\""+jobNum+"\"]\n}");

        try {
            Request request = new Request.Builder()
                    .url(Constants.beishenHost+Constants.queryUserIdByCodeUrl)
                    .method("POST", body)
                    .addHeader("Authorization", "Bearer "+token)
                    .addHeader("Content-Type", "application/json")
                    .build();
            Response response = client.newCall(request).execute();

            int code = response.code();
            log.error("code:{}",code);
            String msg = response.body().string();
            if(code == 200) {
                if(StringUtils.isNotBlank(msg)){
                    JSONObject bodyJson = JSONObject.parseObject(msg);
                    if(bodyJson.containsKey("data")){
                        JSONArray dataArray = bodyJson.getJSONArray("data");
                        if(dataArray.size() > 0){
                            JSONObject itemJson = dataArray.getJSONObject(0);
                            if(itemJson.containsKey("userId")){
                                userid = itemJson.getString("userId");
                            }
                        }
                    }
                }
            }
        } catch (IOException e) {
            log.error("e:{}",e);
        }
        log.error("userid:{}",userid);
        return userid;
    }


}
