package com.fs.baidu.api;

import com.fs.baidu.param.ConversionType;
import com.fs.common.utils.PubFun;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;


/**
 * API 回传数据Demo
 */
public class ConvertData {

    private static final Integer RETRY_TIMES = 3;
    private static final String BAIDU_OCPC_URL = "https://ocpc.baidu.com/ocpcapi/api/uploadConvertData";

    /**
     * 数据回传接口
     * @param token 用户回传数据api接口token
     * @param conversionTypeList 回传转化数据数组
     * @return 返回true代表成功 false代表失败
     */
    public static Boolean sendConvertData(String token, List<ConversionType> conversionTypeList) {

        JsonObject data = new JsonObject();
        // 设置API接口回传Token
        data.addProperty("token", token);
        // 设置API接口回传conversionTypes
        data.add("conversionTypes",
                new Gson().toJsonTree(conversionTypeList, new TypeToken<List<ConversionType>>() {}.getType()).getAsJsonArray());
        // 发送的完整请求数据
        // do some log
        System.out.println("req data: " + data);
        // 向百度发送数据
        return sendWithRetry(data.toString());

    }

    private static boolean sendWithRetry(String msg) {
        // 发送请求
        CloseableHttpClient client = HttpClients.createDefault();
        HttpPost post = new HttpPost(BAIDU_OCPC_URL);
        post.setHeader("Content-type", "application/json; charset=UTF-8");
        StringEntity entity = new StringEntity(msg, StandardCharsets.UTF_8);
        entity.setContentEncoding("UTF-8");
        post.setEntity(entity);
        // 添加失败重试
        int retry = RETRY_TIMES;
        for (int i = 0; i < retry; i++) {
            try {
                HttpResponse response = client.execute(post);
                // 检验状态码，如果成功接收数据
                int code = response.getStatusLine().getStatusCode();
                if (code == HttpStatus.SC_OK) {
                    String res = EntityUtils.toString(response.getEntity());
                    JsonObject returnData = JsonParser.parseString(res).getAsJsonObject();
                    // 打印返回结果
                    // do some log
                    System.out.println("retry times :" + i + ", res data: " + res);

                    int status = returnData.getAsJsonObject("header").get("status").getAsInt();
                    // status为4，代表服务端异常，可添加重试
                    if ( status != 4) {
                        return status == 0; // status == 0 代表回传数据成功，其余情况回传数据失败
                    }
                }
            } catch (IOException e) {
                // do some log
            }
        }
        return false;
    }

    public static ConversionType getConvertData(String url, Integer newType) {
        // 编辑一条转化数据
        ConversionType cv = new ConversionType();
        // 设置落地页url
        cv.setLogidUrl(url);
        // 设置转化类型
        cv.setNewType(newType);
        return cv;
    }
    public static void uploadConvertData(String token, String url, Integer newType) {
        List<ConversionType> conversionTypes = new ArrayList<>();
        // 编辑一条转化数据
        ConversionType cv = new ConversionType();
        // 设置落地页url
        cv.setLogidUrl(url);
        // 设置转化类型
        cv.setNewType(newType);
        conversionTypes.add(cv);
        sendConvertData(token, conversionTypes);
    }

//    public static void sendConvertData(String token, List<ConversionType> conversionTypes) {
//        PubFun.batchProcessing(100, conversionTypes, e -> sendConvertData(token, e));
//    }

    public static void main(String[] args) {

        List<ConversionType> conversionTypes = new ArrayList<>();
        // 编辑一条转化数据
        ConversionType cv = new ConversionType();
        // 设置落地页url
        cv.setLogidUrl("https://bd.runtzh.com/#/?no=b43d2ad3-4a27-425e-bcf3-0d8167b6669d&bd_vid=uANBIyIxUhNLgvw-I-tzrHm1nHckg1mLc1T4g1DLn1mYP16dnHmYnjT");
        // 设置转化类型
//        cv.setNewType(35);
        cv.setNewType(67);
//        cv.setNewType(79);
        conversionTypes.add(cv);
        System.out.println(ConvertData.sendConvertData("token", conversionTypes));
    }
}