package cn.com.ctop.kuaishou.modules.v3.service.impl;

import cn.com.ctop.common.module.entity.CtopOauthToken;
import cn.com.ctop.common.module.utils.HttpUtils;
import cn.com.ctop.common.module.utils.KuaishouInterfaceConstant;
import cn.com.ctop.common.module.utils.PropertiesUtils;
import cn.com.ctop.kuaishou.modules.batch.service.IKuaiShouGroupService;
import cn.com.ctop.kuaishou.modules.batch.service.IKuaishouInterfaceService;
import cn.com.ctop.kuaishou.modules.v3.service.V3KuaishouAccountDetailsService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@Service
public class V3KuaishouAccountDetailsServiceImpl implements V3KuaishouAccountDetailsService {
    @Value("${kuaishou.realuser-ip}")
    private String realUserIP;

    @Autowired
    private IKuaishouInterfaceService kuaishouInterfaceService;
    @Autowired
    private IKuaiShouGroupService getKuaiShouGroupService;


    @Override
    public JSONObject createCascadeAll(CtopOauthToken token, JSONObject requestJson) {
        Long accountId = requestJson.getLong("advertiser_id");
        String accessToken = token.getAccessToken();
        JSONObject resultJson = null;
        try {
            String url = PropertiesUtils.getConfig("kuaishou_api_url") + KuaishouInterfaceConstant.V3_CREATE_CASCADE_ALL;
            Map<String, String> headers = new HashMap<>();
            headers.put("Access-Token", accessToken);
            headers.put("K-RealUser-IP", realUserIP);
            headers.put("Content-Type", " application/json");
            String result = HttpUtils.kuaiShouhttpPostRequest(url, requestJson.toJSONString(), headers);
            resultJson = JSONObject.parseObject(result);
            Integer code = resultJson.getInteger("code");
            if (code == 0) {
                JSONObject dataJson = resultJson.getJSONObject("data");
                Long campaignId = dataJson.getLong("campaign_id");
                Long unitId = dataJson.getLong("unit_id");
                JSONArray creativeIds = dataJson.getJSONArray("creative_ids");
                // 创建成功 拉取广告组信息
                Thread.sleep(300);
                Thread thread = new Thread() {
                    @Override
                    public void run() {
                        kuaishouInterfaceService.getCampaign(accessToken, accountId, campaignId);
                        getKuaiShouGroupService.getGroupByUnitId(accessToken, accountId, unitId);
                        kuaishouInterfaceService.getCreativeByCreativeIds(accessToken, accountId, creativeIds);
                    }
                };
            } else {
                log.error("创建V3广告失败,advertiser_id:{},返回信息：{}", accountId, resultJson);
            }
        } catch (Exception e) {
            log.error("创建V3广告失败,advertiser_id:{}", accountId);
            e.printStackTrace();
        }
        return resultJson;
    }

    @Override
    public JSONObject createUnitAndCreative(CtopOauthToken token, JSONObject requestJson) {
        Long accountId = requestJson.getLong("advertiser_id");
        String accessToken = token.getAccessToken();
        JSONObject resultJson = null;
        try {
            String url = PropertiesUtils.getConfig("kuaishou_api_url") + KuaishouInterfaceConstant.V3_CREATE_UNIT_AND_CREATIVE;
            Map<String, String> headers = new HashMap<>();
            headers.put("Access-Token", accessToken);
            headers.put("K-RealUser-IP", realUserIP);
            headers.put("Content-Type", " application/json");
            String result = HttpUtils.kuaiShouhttpPostRequest(url, requestJson.toJSONString(), headers);
             resultJson = JSONObject.parseObject(result);
            Integer code = resultJson.getInteger("code");
            if (code == 0) {
                JSONObject dataJson = resultJson.getJSONObject("data");
                Long campaignId = dataJson.getLong("campaign_id");
                Long unitId = dataJson.getLong("unit_id");
                JSONArray creativeIds = dataJson.getJSONArray("creative_ids");
                // 创建成功 拉取广告组信息
                Thread.sleep(300);
                Thread thread = new Thread() {
                    @Override
                    public void run() {
                        kuaishouInterfaceService.getCampaign(accessToken, accountId, campaignId);
                        getKuaiShouGroupService.getGroupByUnitId(accessToken, accountId, unitId);
                        kuaishouInterfaceService.getCreativeByCreativeIds(accessToken, accountId, creativeIds);
                    }
                };
            } else {
                log.error("创建V3广告失败,advertiser_id:{},返回信息：{}", accountId, resultJson);
            }
        } catch (Exception e) {
            log.error("创建V3广告失败,advertiser_id:{}", accountId);
            e.printStackTrace();
        }
        return resultJson;
    }
}
