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.stereotype.Component;
import java.io.IOException;


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

    /***
     *
     * @param token
     * @param page
     * @param capacity
     * @param modifiedAfter
     * @param modifiedBefore
     */
    public String getAssessmentsByFilters(String token, int page, int capacity, String modifiedAfter, String modifiedBefore) {
        String bodyData = "";

        JSONObject bodyJson = new JSONObject();
        bodyJson.put("page", page);
        bodyJson.put("capacity", capacity);
        bodyJson.put("modifiedAfter", modifiedAfter);
        bodyJson.put("modifiedBefore", modifiedBefore);
        log.error("getAssessmentsByFilters:bodyJson:{}", bodyJson.toJSONString());

        OkHttpClient client = new OkHttpClient().newBuilder()
                .build();
        MediaType mediaType = MediaType.parse("application/json");
        RequestBody body = RequestBody.create(mediaType, bodyJson.toJSONString());

        try {
            Request request = new Request.Builder()
                    .url(Constants.beishenHost+Constants.assessmentFilterUrl)
                    .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);
            if (code == 200) {
                bodyData = response.body().string();
            }

            log.error("bodyData:{}", bodyData);
        } catch (IOException e) {
            log.error("e:{}", e.getMessage());
            throw new RuntimeException(e);
        }
        return bodyData;
    }

    public JSONArray queryAssessmentsByPage(String token, int page, int capacity, String modifiedAfter, String modifiedBefore){
        JSONArray assessmentArray = new JSONArray();


        JSONObject bodyJson = new JSONObject();
        bodyJson.put("page", page);
        bodyJson.put("capacity", capacity);
        bodyJson.put("modifiedAfter", modifiedAfter);
        bodyJson.put("modifiedBefore", modifiedBefore);
        log.error("queryAssessmentsByPage:bodyJson:{}", bodyJson.toJSONString());

        OkHttpClient client = new OkHttpClient().newBuilder()
                .build();
        MediaType mediaType = MediaType.parse("application/json");
        RequestBody body = RequestBody.create(mediaType, bodyJson.toJSONString());


        try {
            Request request = new Request.Builder()
                    .url(Constants.beishenHost+Constants.assessmentFilterUrl)
                    .method("POST", body)
                    .addHeader("Authorization", "Bearer "+token)
                    .addHeader("Content-Type", "text/plain")
                    .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 returnJson = JSONObject.parseObject(msg);
                    if(returnJson.containsKey("code")){
                        String dataCode = returnJson.getString("code");
                        if("200".equals(dataCode)){
                            JSONObject dataObject = returnJson.getJSONObject("data");
                            if(dataObject.containsKey("items")){
                                JSONArray itemsArray = dataObject.getJSONArray("items");
                                assessmentArray.addAll(itemsArray);
                            }
                        }
                    }
                }
            }
        } catch (IOException e) {
            log.error("e:{}",e.getMessage());
            throw new RuntimeException(e);
        }
        log.error("assessmentArray:{}",assessmentArray.size());
        return assessmentArray;
    }


}
