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 com.weaver.seconddev.chapanda.beisen.dao.HrmAssessment360Dao;
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;
import java.util.List;
import java.util.Map;


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

    @Autowired
    Token2BeiSenUtil token2BeiSenUtil ;

    @Autowired
    HrmAssessment360Dao hrmAssessment360Dao;

    /***
     *
     * @return
     */
    public JSONArray queryAssessment360Data(){
        JSONArray assessmentArray = new JSONArray();

        List<Map<String, Object>> emailList =  hrmAssessment360Dao.queryEmployeeEmail();
        for(int i=0;i<emailList.size();i++){
            Map<String, Object> emailMap = emailList.get(i);
            String email = emailMap.get("email").toString();
            if(StringUtils.isNotBlank(email)){
                JSONArray dataArray = convertAssessmentData(email);
                assessmentArray.addAll(dataArray);
            }
        }
        return assessmentArray;
    }


    public JSONArray convertAssessmentData(String email){
        JSONArray assessmentArray = new JSONArray();
        if(StringUtils.isNotBlank(email)){
            String token = token2BeiSenUtil.getToken();
            log.error("token:{}",token);
            if(StringUtils.isNotBlank(token)){
                JSONObject bodyJson = new JSONObject();
                bodyJson.put("email",email);
                log.error("bodyJson:{}",bodyJson.toJSONString());
                String msg =  doHttpPost(token,bodyJson.toJSONString());
                if(StringUtils.isNotBlank(msg)){
                    JSONObject msgJson = JSONObject.parseObject(msg);
                    if(msgJson.containsKey("data")){
                        JSONObject dataJson = msgJson.getJSONObject("data");

                        String personnelStaffCode = "";
                        String personnelMail = "";
                        String personnelName = "";
                        if(dataJson.containsKey("personInfo")){
                            JSONObject personInfoJson = dataJson.getJSONObject("personInfo");
                            if(personInfoJson.containsKey("personnelName")){
                                personnelName = personInfoJson.getString("personnelName");
                            }
                            if(personInfoJson.containsKey("personnelMail")){
                                personnelMail = personInfoJson.getString("personnelMail");
                            }
                            if(personInfoJson.containsKey("personnelStaffCode")){
                                personnelStaffCode = personInfoJson.getString("personnelStaffCode");
                            }
                        }

                        log.error("personnelStaffCode:{}",personnelStaffCode);
                        log.error("personnelMail:{}",personnelMail);

                        if(dataJson.containsKey("activityList")){
                            JSONArray activityListArray = dataJson.getJSONArray("activityList");
                            for (int i = 0; i < activityListArray.size(); i++) {
                                JSONObject activityJson = activityListArray.getJSONObject(i);
                                String activityID = "";
                                if(activityJson.containsKey("activityID")){
                                    activityID = activityJson.getString("activityID");
                                }
                                String activityName = "";
                                if(activityJson.containsKey("activityName")){
                                    activityName = activityJson.getString("activityName");
                                }
                                String createTime = "";
                                if(activityJson.containsKey("createTime")){
                                    createTime = activityJson.getString("createTime");
                                }
                                JSONArray questionnaireInfoList = new JSONArray();
                                if(activityJson.containsKey("questionnaireInfoList")){
                                    questionnaireInfoList = activityJson.getJSONArray("questionnaireInfoList");
                                }

                                log.error("activityID:{}",personnelMail);
                                log.error("createTime:{}",createTime);
                                log.error("questionnaireInfoList:{}",questionnaireInfoList.size());

                                for(int n=0;n<questionnaireInfoList.size();n++){
                                    JSONObject questionnaireInfoJson = questionnaireInfoList.getJSONObject(n);
                                    String questionnaireID = questionnaireInfoJson.getString("questionnaireID");
                                    String questionnaireName = questionnaireInfoJson.getString("questionnaireName");
                                    String otherAvg = questionnaireInfoJson.getString("otherAvg");
                                    String lastUpdateTime = questionnaireInfoJson.getString("lastUpdateTime");
                                    log.error("questionnaireID:{}",questionnaireID);
                                    log.error("otherAvg:{}",otherAvg);
                                    log.error("lastUpdateTime:{}",lastUpdateTime);

                                    JSONObject assessJson = new JSONObject();
                                    assessJson.put("gsyx",personnelMail);
                                    assessJson.put("gh",personnelStaffCode);
                                    assessJson.put("hdid",activityID);
                                    assessJson.put("hdmc",activityName);
                                    assessJson.put("wjid",questionnaireID);
                                    assessJson.put("wjmc",questionnaireName);
                                    assessJson.put("zzjg",otherAvg);
                                    assessJson.put("cjsj",createTime.replace("T"," "));
                                    assessJson.put("wcsj",lastUpdateTime.replace("T"," "));
                                    assessmentArray.add(assessJson);
                                }
                            }
                        }
                    }
                }
            }
        }
        return assessmentArray;
    }

    /***
     *
     */
    public String doHttpPost(String token,String bodyJson){
        String bodyData = "";
        OkHttpClient client = new OkHttpClient().newBuilder()
                .build();
        MediaType mediaType = MediaType.parse("application/json");
        RequestBody body = RequestBody.create(mediaType, bodyJson);

        try {
            Request request = new Request.Builder()
                    .url(Constants.beishenHost + Constants.query360AssessmentUrl)
                    .method("POST", body)
                    .addHeader("Content-Type", "application/json")
                    .addHeader("Authorization", "Bearer "+token)
                    .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){
                bodyData = msg;
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return bodyData;
    }

}
