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

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

    @Autowired
    Token2BeiSenUtil token2BeiSenUtil ;

    /***
     *
     * @param startDate
     * @param endDate
     * @return
     */
    public String queryPositionByDate(String startDate, String endDate,int capacity){

        String token = token2BeiSenUtil.getToken();
        log.error("token:{}",token);
        String bodyData = "";
        if(StringUtils.isNotBlank(token)){
            JSONObject bodyJson = new JSONObject();
            bodyJson.put("startTime",startDate);
            bodyJson.put("stopTime",endDate);
            bodyJson.put("timeWindowQueryType","1");
            bodyJson.put("capacity",capacity);
            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.positionUrl)
                        .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);
                if(code == 200){
                    bodyData = msg;
                }
            } catch (IOException e) {
                log.error("e:{}",e);
            }
        }

        return bodyData;
    }

    /***
     *
     * @param startDate
     * @param endDate
     */
    public String queryPositionByscrollId(String startDate, String endDate,String scrollId,int capacity){

        String token = token2BeiSenUtil.getToken();
        log.error("token:{}",token);
        String bodyData = "";
        if(StringUtils.isNotBlank(token)){
            JSONObject bodyJson = new JSONObject();
            bodyJson.put("startTime",startDate);
            bodyJson.put("stopTime",endDate);
            bodyJson.put("timeWindowQueryType","1");
            bodyJson.put("scrollId",scrollId);
            bodyJson.put("capacity",capacity);
            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.positionUrl)
                        .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);
                if(code == 200){
                    bodyData = msg;
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return bodyData;
    }
}
