package com.tzld.piaoquan.api.component;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.tzld.piaoquan.api.model.bo.AdPutCreativeComponentCostData;
import com.tzld.piaoquan.api.model.bo.AdPutFlowRecordTencent;
import com.tzld.piaoquan.api.model.bo.AdPutTencentCreative;
import com.tzld.piaoquan.api.model.bo.AdPutTencentCreativeAnalysis;
import com.tzld.piaoquan.growth.common.component.HttpPoolClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Component
@Slf4j
public class AdApiService {

    @Autowired
    private HttpPoolClient httpPoolClient;

    @Value("${ad.api.host:https://testapi.piaoquantv.com/ad}")
    private String adApiHost;


    public Integer getPutFlowListTencentCount(String channel, Integer pageNum, Integer pageSize) {
        String url = adApiHost + "/put/flow/list/tencent?videoId=&putScene=&channel="
                + channel + "&currentPage=" + pageNum + "&pageSize=" + pageSize;
        try {
            String post = httpPoolClient.get(url);
            JSONObject res = JSONObject.parseObject(post);
            return res.getJSONObject("data").getInteger("totalSize");
        } catch (Exception e) {
            log.error("getPutFlowListTencentCount error", e);
        }
        return 0;
    }

    public List<AdPutFlowRecordTencent> getPutFlowListTencent(String channel, Integer pageNum, Integer pageSize) {
        String url = adApiHost + "/put/flow/list/tencent?videoId=&putScene=&channel="
                + channel + "&currentPage=" + pageNum + "&pageSize=" + pageSize;
        try {
            String post = httpPoolClient.get(url);
            JSONObject res = JSONObject.parseObject(post);
            JSONArray data = res.getJSONObject("data").getJSONArray("objs");
            if (Objects.nonNull(data) && !data.isEmpty()) {
                return data.toJavaList(AdPutFlowRecordTencent.class);
            }
        } catch (Exception e) {
            log.error("getPutFlowListTencent error", e);
        }
        return new ArrayList<>();
    }

    /**
     * 获取投放广告创意花费
     * @param dt 2025-12-23
     * @return
     */
    public List<AdPutCreativeComponentCostData> getCreativeComponentsCost(String dt) {
        String url = "https://api.piaoquantv.com/ad" + "/put/tencent/getCreativeComponentsCost?dt=" + dt;
        try {
            String post = httpPoolClient.get(url);
            JSONObject res = JSONObject.parseObject(post);
            JSONArray data = res.getJSONArray("data");
            if (Objects.nonNull(data) && !data.isEmpty()) {
                return data.toJavaList(AdPutCreativeComponentCostData.class);
            }
        } catch (Exception e) {
            log.error("getPutFlowListTencent error", e);
        }
        return new ArrayList<>();
    }

    /**
     * 根据rootSourceId获取创意信息
     */
    public AdPutTencentCreative getCreativeByRootSourceId(String rootSourceId) {
        try {
            String url = adApiHost + "/put/tencent/getCreativeByRootSourceId?rootSourceId=" + rootSourceId;
            String response = httpPoolClient.get(url);
            JSONObject res = JSONObject.parseObject(response);
            if (res.getInteger("code") == 0 && res.getJSONObject("data") != null) {
                return res.getJSONObject("data").toJavaObject(AdPutTencentCreative.class);
            }
        } catch (Exception e) {
            log.error("获取创意信息失败, rootSourceId={}", rootSourceId, e);
        }
        return null;
    }

    /**
     * 根据创意ID获取素材分析信息
     */
    public AdPutTencentCreativeAnalysis getCreativeAnalysis(Long creativeId) {
        try {
            String url = adApiHost + "/put/tencent/getCreativeAnalysis?creativeId=" + creativeId;
            String response = httpPoolClient.get(url);
            JSONObject res = JSONObject.parseObject(response);
            if (res.getInteger("code") == 0 && res.getJSONObject("data") != null) {
                return res.getJSONObject("data").toJavaObject(AdPutTencentCreativeAnalysis.class);
            }
        } catch (Exception e) {
            log.error("获取素材分析信息失败, creativeId={}", creativeId, e);
        }
        return null;
    }

    /**
     * 根据创意ID获取素材分析信息
     */
    public AdPutTencentCreative getCreative(Long creativeId) {
        try {
            String url = adApiHost + "/put/tencent/getCreative?creativeId=" + creativeId;
            String response = httpPoolClient.get(url);
            JSONObject res = JSONObject.parseObject(response);
            if (res.getInteger("code") == 0 && res.getJSONObject("data") != null) {
                return res.getJSONObject("data").toJavaObject(AdPutTencentCreative.class);
            }
        } catch (Exception e) {
            log.error("获取素材分析信息失败, creativeId={}", creativeId, e);
        }
        return null;
    }

    /**
     * 根据创意ID获取人群包ID
     */
    public List<Long> getPackageIdByAdId(Long adId) {
        try {
            String url = adApiHost + "/put/tencent/getPackageIdByAdId?adId=" + adId;
            String response = httpPoolClient.get(url);
            JSONObject res = JSONObject.parseObject(response);
            if (res.getInteger("code") == 0 && res.get("data") != null) {
                return res.getJSONArray("data").toJavaList(Long.class);
            }
        } catch (Exception e) {
            log.error("获取人群包信息失败, adId={}", adId, e);
        }
        return null;
    }

    /**
     * 根据RootSourceId 获取 tencentFlow videoId
     */
    public Long getVideoIdByRootSourceId(String rootSourceId) {
        try {
            String url = adApiHost + "/put/flow/getByRootSourceId/tencent?rootSourceId=" + rootSourceId;
            String response = httpPoolClient.get(url);
            JSONObject res = JSONObject.parseObject(response);
            if (res.getInteger("code") == 0 && res.get("data") != null) {
                JSONObject data = res.getJSONObject("data");
                return data.getLong("videoId");
            }
        } catch (Exception e) {
            log.error("获取tencentFlow videoId失败, rootSourceId={}", rootSourceId, e);
        }
        return null;
    }
}
