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

import cn.com.ctop.common.module.entity.UserAllocation;
import cn.com.ctop.common.module.service.IUserAllocationService;
import cn.com.ctop.common.module.utils.Check;
import cn.com.ctop.common.module.utils.HttpUtils;
import cn.com.ctop.common.module.utils.ResultMapUtils;
import cn.com.ctop.common.module.utils.StatusCode;
import cn.com.ctop.kuaishou.modules.ai.entity.AiKuaishouAdvertiserStrategy;
import cn.com.ctop.kuaishou.modules.ai.mapper.AiKuaishouAdvertiserStrategyMapper;
import cn.com.ctop.kuaishou.modules.ai.service.IAiKuaishouAdvertiserStrategyService;
import cn.com.ctop.kuaishou.modules.batch.service.IKuaiShouVideoGetService;
import cn.com.ctop.kuaishou.modules.batch.service.IKuaishouVideoRelateCreativesService;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * 快手账户策略设置信息表
 *
 * @author jeecg-boot
 * @version V1.0
 * @date 2021-01-25
 */
@Service
public class AiKuaishouAdvertiserStrategyServiceImpl extends ServiceImpl<AiKuaishouAdvertiserStrategyMapper, AiKuaishouAdvertiserStrategy> implements IAiKuaishouAdvertiserStrategyService {
    public static final String TEST_CREATIVE_URL = "http://139.186.27.96:31012/ai_historical_missing_material";
    public static final String TEST_PROGRAM_CREATIVE_URL = "http://139.186.27.96:31012/ai_programme_new_material";
    @Autowired
    private IUserAllocationService userAllocationService;
    @Autowired
    private IKuaiShouVideoGetService videoGetService;
    @Autowired
    private IKuaishouVideoRelateCreativesService relateCreativesService;
    @Autowired
    private AiKuaishouAdvertiserStrategyMapper kuaishouAdvertiserStrategyMapper;

    @Override
    public AiKuaishouAdvertiserStrategy getByAccountId(Long accountId) {
        QueryWrapper<AiKuaishouAdvertiserStrategy> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("account_id", accountId);
        queryWrapper.orderByDesc("id");
        queryWrapper.last("limit 1");
        return this.getOne(queryWrapper);
    }

    @Override
    public Map<String, Object> testCreate(Long accountId) {
        Map<String, Object> result = new HashMap<>();
        if (null == accountId || accountId == 0) {
            ResultMapUtils.setResultMap(result, StatusCode.COMMON_PARAM_ERROR);
            return result;
        }
        AiKuaishouAdvertiserStrategy aiKuaishouAdvertiserStrategy = this.getByAccountId(accountId);
        if (null == aiKuaishouAdvertiserStrategy || null == aiKuaishouAdvertiserStrategy.getStatus() || aiKuaishouAdvertiserStrategy.getStatus() != 1) {
            ResultMapUtils.setResultMap(result, StatusCode.COMMON_PARAM_ERROR);
            return result;
        }
        UserAllocation allocation = userAllocationService.getByAccountId(accountId);
        if (null == allocation) {
            ResultMapUtils.setResultMap(result, StatusCode.COMMON_PARAM_ERROR);
            return result;
        }
        JSONObject params = new JSONObject();
        params.put("account_id", accountId);
        params.put("test_video_cnt", 1);
        String request = null;
        //程序化
        if (aiKuaishouAdvertiserStrategy.getOpenProgramCreate() == 1) {
            request = HttpUtils.httpPostRequest(TEST_PROGRAM_CREATIVE_URL, params, new HashMap<>());
        } else {
            request = HttpUtils.httpPostRequest(TEST_CREATIVE_URL, params, new HashMap<>());
        }
        JSONObject requestObject = JSONObject.parseObject(request);
        String message = requestObject.getString("message");
        Integer code = requestObject.getInteger("code");
        result.put("code", code);
        result.put("message", message);
        return result;
    }

    @Override
    public Map<String, Object> aiHistoricalMissingMaterial(Long accountId) {
        Map<String, Object> result = new HashMap<>();
        if (null == accountId || accountId == 0) {
            ResultMapUtils.setResultMap(result, StatusCode.COMMON_PARAM_ERROR);
            return result;
        }
        AiKuaishouAdvertiserStrategy strategy = this.getByAccountId(accountId);
        if (null == strategy || strategy.getStatus() != 1) {
            ResultMapUtils.setResultMap(result, StatusCode.KUAISHOU_AI_STRATEGY_HAS_NOT_EXIST);
            return result;
        }
        this.historicalMissingMaterialCreateCreative(strategy);
        ResultMapUtils.setResultMap(result, StatusCode.COMMON_SUCCESS);
        return result;
    }

    @Override
    public List<AiKuaishouAdvertiserStrategy> getAllEffectStrategy() {
        QueryWrapper<AiKuaishouAdvertiserStrategy> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", 1);
        queryWrapper.orderByDesc("id");
        return this.list(queryWrapper);
    }

    @Override
    public PageInfo<AiKuaishouAdvertiserStrategy> queryList(AiKuaishouAdvertiserStrategy aiKuaishouAdvertiserStrategy) {
        return new PageInfo<>(kuaishouAdvertiserStrategyMapper.queryList(aiKuaishouAdvertiserStrategy));
    }

    private Map<String, Object> historicalMissingMaterialCreateCreative(AiKuaishouAdvertiserStrategy strategy) {
        Map<String, Object> result = new HashMap<>();
        //判定是否单一应用
        if (strategy.getSingleAppid() == 1) {
            //TODO 参数拼接
            Map<String, Object> params = new HashMap<>();
            //1:直接查询历史素材
            List<JSONObject> materialList = relateCreativesService.getVideoZeroListByMap(params);
            if (Check.isNull(materialList)) {
                ResultMapUtils.setResultMap(result, StatusCode.KUAISHOU_AI_STRATEGY_HAS_NO_HISTORY_MATERIAL);
                return result;
            }
            materialList = videoGetService.getVideoInfoAndImageList(materialList);
            //补充遗漏素材
            JSONObject hisMatParams = formatHisMatParams(strategy, materialList, "补充遗漏素材");
        } else {
            //

        }
        return result;
    }

    private JSONObject formatHisMatParams(AiKuaishouAdvertiserStrategy strategy, List<JSONObject> materialList, String fixString) {
        JSONObject result = new JSONObject();
        result.put("video", materialList);
        result.put("operation_type", 1);
        result.put("account_id", strategy.getAccountId());
        result.put("ai_strategy_remark", fixString);

        return result;
    }

}
