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 Offer2BeiSenUtil {
    private final static Logger log = LoggerFactory.getLogger(Offer2BeiSenUtil.class);

    /***
     *
     * @param token
     * @param startDate
     * @param startTime
     * @param endDate
     * @param endTime
     * @return
     */
    public JSONArray queryOfferByDateTime(String token,String startDate, String startTime, String endDate, String endTime){

        // https://openapi.italent.cn/TenantBaseExternal/api/v5/Offer/GetByTimeWindow
//        "{\n    \"startTime\": \"2025-06-01T00:00:00\", \n    \"stopTime\": \"2025-06-21T00:00:00\",\n    \"timeWindowQueryType\": 1\n}"
        JSONArray offerArray = new JSONArray();
        JSONObject bodyJson = new JSONObject();
        bodyJson.put("startTime",startDate+"T"+startTime);
        bodyJson.put("stopTime",endDate+"T"+endTime);
        bodyJson.put("timeWindowQueryType","1");
        log.error("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.offerUrl)
                    .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){
                if(StringUtils.isNotBlank(msg)){
                    JSONObject returnJson = JSONObject.parseObject(msg);
                    if(returnJson.containsKey("code")){
                        String dataCode = returnJson.getString("code");
                        if("200".equals(dataCode)){
                            JSONArray dataArray = returnJson.getJSONArray("data");
                            offerArray.addAll(dataArray);
                            String scrollId = returnJson.getString("scrollId");
                            if(StringUtils.isNotBlank(scrollId)){
                                queryOfferByscrollId(token, startDate, startTime, endDate, endTime,offerArray);
                            }
                        }
                    }
                }

            }else{
                log.error("msg:{}",msg);
            }

        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return offerArray;
    }

    /***
     *
     * @param token
     * @param startDate
     * @param startTime
     * @param endDate
     * @param endTime
     * @param offerArray
     */
    public void queryOfferByscrollId(String token, String startDate, String startTime, String endDate, String endTime,JSONArray offerArray){

        // https://openapi.italent.cn/TenantBaseExternal/api/v5/Offer/GetByTimeWindow
//        "{\n    \"startTime\": \"2025-06-01T00:00:00\", \n    \"stopTime\": \"2025-06-21T00:00:00\",\n    \"timeWindowQueryType\": 1\n}"

        JSONObject bodyJson = new JSONObject();
        bodyJson.put("startTime",startDate+"T"+startTime);
        bodyJson.put("stopTime",endDate+"T"+endTime);
        bodyJson.put("timeWindowQueryType","1");
        log.error("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.offerUrl)
                    .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("code:{}",code);
            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)){
                            JSONArray dataArray = returnJson.getJSONArray("data");
                            offerArray.addAll(dataArray);
                            String scrollId = returnJson.getString("scrollId");
                            if(StringUtils.isNotBlank(scrollId)){
                                queryOfferByscrollId(token, startDate, startTime, endDate, endTime,dataArray);
                            }
                        }
                    }
                }
            }else{
                log.error("msg:{}",msg);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }


}
