package cn.com.ctop.toutiao.modules.tool.service.impl;

import cn.com.ctop.common.module.entity.CtopOauthToken;
import cn.com.ctop.common.module.utils.HttpUtils;
import cn.com.ctop.toutiao.modules.tool.constants.AudiencePackageUrlConstant;
import cn.com.ctop.toutiao.modules.tool.service.IByteDanceAudiencePackageService;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

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

@Slf4j
@Service
public class ByteDanceAudiencePackageServiceImpl implements IByteDanceAudiencePackageService {

    //TODO 定向包暂时不同步头条后台
    @Override
    public Map<String, Object> getAudiencePackage(CtopOauthToken token, Long advertiserId, String landingType, String deliveryRange) {
        Map<String, Object> filtering = new HashMap<>();
        if(!landingType.isEmpty()){
            filtering.put("landing_type",landingType);
        }
        if(!deliveryRange.isEmpty()){
            filtering.put("deliveryRange",deliveryRange);
        }
        JSONObject param = new JSONObject();
        param.put("advertiser_id", advertiserId);
        param.put("page", 1);
        param.put("page_size", 50);
        param.put("filtering", filtering);
        String url= AudiencePackageUrlConstant.GET_AUDIENCE_PACKAGE;
        JSONObject resultObject = HttpUtils.bytedanceGetRequest(token.getAccessToken(), url, param);
        int code = resultObject.getInteger("code");
        if (code != 0) {
            log.error("获取定向包接口异常==》accountId:{},message:{}", token.getAccountId(), resultObject.getString("message"));
        }
        JSONArray data = resultObject.getJSONObject("data").getJSONArray("list");
        //TODO 定向包是否入库
        if (null != data && !data.isEmpty()) {
            data.forEach(it->{

            });
        }
        return null;
    }

    @Override
    public Map<String, Object> createAudiencePackage(CtopOauthToken token, Long advertiserId,JSONObject requestBody) {

        Map<String, String> headers = new HashMap<>();
        headers.put("Content-Type", "application/json");
        headers.put("Access-Token", token.getAccessToken());

        /**
         *  拼接创建定向包参数
         */
        JSONObject params = new JSONObject();
        //必填参数
        params.put("advertiser_id",advertiserId);
        params.put("name",requestBody.getString("name"));
        params.put("description",requestBody.getString("description"));
        params.put("landing_type",requestBody.getString("landingType"));
        //选填参数
        if(requestBody.getJSONArray("retargetingTags")!=null){
            params.put("retargeting_tags",requestBody.getJSONArray("retargetingTags"));
        }
        if(requestBody.getJSONArray("retargetingTagsExclude")!=null){
            params.put("retargeting_tags_exclude",requestBody.getJSONArray("retargetingTagsExclude"));
        }
        //受众性别
        params.put("gender",requestBody.getString("gender"));
        if(requestBody.getJSONArray("age")!=null){
            params.put("age",requestBody.getJSONArray("age"));
        }
        //受众最低ios版本
        params.put("ios_osv",requestBody.getString("iosOsv"));
        if(params.getJSONArray("carrier")!=null){
            params.put("carrier",requestBody.getJSONArray("carrier"));
        }
        if(params.getJSONArray("ac")!=null){
            params.put("ac",requestBody.getJSONArray("ac"));
        }
        if(params.getJSONArray("deviceBrand")!=null){
            params.put("device_brand",requestBody.getJSONArray("deviceBrand"));
        }
        if(params.getJSONArray("articleCategory")!=null){
            params.put("article_category",requestBody.getJSONArray("articleCategory"));
        }
        if(params.getJSONArray("activateType")!=null){
            params.put("activate_type",requestBody.getJSONArray("activateType"));
        }
        if(params.getJSONArray("platform")!=null){
            params.put("platform",requestBody.getJSONArray("platform"));
        }
        params.put("auto_extend_enabled",requestBody.getInteger("autoExtendEnabled"));
        if(params.getJSONArray("autoExtendTargets")!=null){
            params.put("auto_extend_targets",requestBody.getJSONArray("autoExtendTargets"));
        }
        if(params.getJSONArray("launchPrice")!=null){
            params.put("launch_price",requestBody.getJSONArray("launchPrice"));
        }
        params.put("interest_action_mode",requestBody.getString("interestActionMode"));
        if(params.getJSONArray("actionScene")!=null){
            params.put("action_scene",requestBody.getJSONArray("actionScene"));
        }
        params.put("action_days",requestBody.getString("actionDays"));
        if(params.getJSONArray("actionCategories")!=null){
            params.put("action_categories",requestBody.getJSONArray("actionCategories"));
        }
        if(params.getJSONArray("actionWords")!=null){
            params.put("action_words",requestBody.getJSONArray("actionWords"));
        }
        if(params.getJSONArray("interestCategories")!=null){
            params.put("interest_categories",requestBody.getJSONArray("interestCategories"));
        }
        if(params.getJSONArray("interestWords")!=null){
            params.put("interest_words",requestBody.getJSONArray("interestWords"));
        }
        if(params.getJSONArray("city")!=null){
            params.put("city",requestBody.getJSONArray("city"));
        }
        if(params.getJSONArray("businessIds")!=null){
            params.put("business_id" +
                    "s",requestBody.getJSONArray("businessIds"));
        }
        params.put("district",requestBody.getString("district"));
        params.put("location_type",requestBody.getString("locationType"));
        if(params.getJSONArray("interestWords")!=null){
            params.put("interest_words",requestBody.getJSONArray("interestWords"));
        }
        JSONObject jsonObject = JSONObject.parseObject(HttpUtils.httpPostRequest(AudiencePackageUrlConstant.CREATE_AUDIENCE_PACKAGE, params, headers));
        Map<String,Object> resultMap=new HashMap<>();
        if(jsonObject.getInteger("code")!=0){
            log.error("创建定向包失败==》accountId:{},message:{}", token.getAccountId(), jsonObject.getString("message"));
            resultMap.put("code", -1);
            resultMap.put("message", jsonObject.getString("message"));
            return resultMap;
        }
        resultMap.put("code", 0);
        return resultMap;
    }

    @Override
    public Map<String, Object> updateAudiencePackage(CtopOauthToken token, Long advertiserId,JSONObject requestBody) {

        Map<String, String> headers = new HashMap<>();
        headers.put("Content-Type", "application/json");
        headers.put("Access-Token", token.getAccessToken());

        JSONObject params = new JSONObject();
        //必填参数
        params.put("advertiser_id",advertiserId);
        params.put("name",requestBody.getString("name"));
        params.put("description",requestBody.getString("description"));
        //修改的定向包Id
        params.put("audience_package_id",requestBody.getString("audiencePackageId"));
        //选填参数
        if(requestBody.getJSONArray("retargetingTags")!=null){
            params.put("retargeting_tags",requestBody.getJSONArray("retargetingTags"));
        }
        if(requestBody.getJSONArray("retargetingTagsExclude")!=null){
            params.put("retargeting_tags_exclude",requestBody.getJSONArray("retargetingTagsExclude"));
        }
        //受众性别
        params.put("gender",requestBody.getString("gender"));
        if(requestBody.getJSONArray("age")!=null){
            params.put("age",requestBody.getJSONArray("age"));
        }
        //受众最低ios版本
        params.put("ios_osv",requestBody.getString("iosOsv"));
        if(params.getJSONArray("carrier")!=null){
            params.put("carrier",requestBody.getJSONArray("carrier"));
        }
        if(params.getJSONArray("ac")!=null){
            params.put("ac",requestBody.getJSONArray("ac"));
        }
        if(params.getJSONArray("deviceBrand")!=null){
            params.put("device_brand",requestBody.getJSONArray("deviceBrand"));
        }
        if(params.getJSONArray("articleCategory")!=null){
            params.put("article_category",requestBody.getJSONArray("articleCategory"));
        }
        if(params.getJSONArray("activateType")!=null){
            params.put("activate_type",requestBody.getJSONArray("activateType"));
        }
        if(params.getJSONArray("platform")!=null){
            params.put("platform",requestBody.getJSONArray("platform"));
        }
        params.put("auto_extend_enabled",requestBody.getInteger("autoExtendEnabled"));
        if(params.getJSONArray("autoExtendTargets")!=null){
            params.put("auto_extend_targets",requestBody.getJSONArray("autoExtendTargets"));
        }
        if(params.getJSONArray("launchPrice")!=null){
            params.put("launch_price",requestBody.getJSONArray("launchPrice"));
        }
        params.put("interest_action_mode",requestBody.getString("interestActionMode"));
        if(params.getJSONArray("actionScene")!=null){
            params.put("action_scene",requestBody.getJSONArray("actionScene"));
        }
        params.put("action_days",requestBody.getString("actionDays"));
        if(params.getJSONArray("actionCategories")!=null){
            params.put("action_categories",requestBody.getJSONArray("actionCategories"));
        }
        if(params.getJSONArray("actionWords")!=null){
            params.put("action_words",requestBody.getJSONArray("actionWords"));
        }
        if(params.getJSONArray("interestCategories")!=null){
            params.put("interest_categories",requestBody.getJSONArray("interestCategories"));
        }
        if(params.getJSONArray("interestWords")!=null){
            params.put("interest_words",requestBody.getJSONArray("interestWords"));
        }
        if(params.getJSONArray("city")!=null){
            params.put("city",requestBody.getJSONArray("city"));
        }
        if(params.getJSONArray("businessIds")!=null){
            params.put("business_id" +
                    "s",requestBody.getJSONArray("businessIds"));
        }
        params.put("district",requestBody.getString("district"));
        params.put("location_type",requestBody.getString("locationType"));
        if(params.getJSONArray("interestWords")!=null){
            params.put("interest_words",requestBody.getJSONArray("interestWords"));
        }

        JSONObject jsonObject = JSONObject.parseObject(HttpUtils.httpPostRequest(AudiencePackageUrlConstant.UPDATE_AUDIENCE_PACKAGE, params, headers));
        Map<String,Object> resultMap=new HashMap<>();
        if(jsonObject.getInteger("code")!=0){
            log.error("更新定向包失败==》accountId:{},message:{}", token.getAccountId(), jsonObject.getString("message"));
            resultMap.put("code", -1);
            resultMap.put("message", jsonObject.getString("message"));
            return resultMap;
        }
        resultMap.put("code", 0);
        return resultMap;
    }

    @Override
    public Map<String, Object> deleteAudiencePackage(CtopOauthToken token, Long advertiserId, String landingType, String deliveryRange) {
        return null;
    }

    @Override
    public Map<String, Object> bindAdAudiencePackage(CtopOauthToken token, Long advertiserId, String landingType, String deliveryRange) {
        return null;
    }

    @Override
    public Map<String, Object> unbindAdAudiencePackage(CtopOauthToken token, Long advertiserId, String landingType, String deliveryRange) {
        return null;
    }
}
