package cn.com.ctop.kuaishou.modules.ai.controller;

import cn.com.ctop.common.module.utils.Check;
import cn.com.ctop.kuaishou.modules.ai.entity.AiKuaishouAdvertiserStrategy;
import cn.com.ctop.kuaishou.modules.ai.service.IAiKuaishouAdvertiserStrategyService;
import cn.com.ctop.kuaishou.modules.ai.service.IAiKuaishouCreateCreativeService;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@Slf4j
@Api(tags = "快手-自动投放")
@RestController
@RequestMapping("/ai/create")
public class AiKuaishouCreateCreativeController {
    @Autowired
    private IAiKuaishouCreateCreativeService createCreativeService;
    @Autowired
    private IAiKuaishouAdvertiserStrategyService strategyService;

    static ExecutorService kuaishouCustomCreativeSupplementExecutorService = Executors.newFixedThreadPool(3);
    static ExecutorService kuaishouProgramCreativeAutoService = Executors.newFixedThreadPool(2);
    static ExecutorService kuaishouProgramTopCreativeAutoService = Executors.newFixedThreadPool(3);


    /**
     * 自动上新
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/customCreativeLimit")
    public JSONObject customCreativeLimit(String id) {
        JSONObject returnJson = new JSONObject();
        try {
            if (Check.isNull(id)) {
                throw new Exception("主键id不能为空");
            }
            AiKuaishouAdvertiserStrategy strategy = strategyService.getById(id);
            if (Check.isNull(strategy)) {
                throw new Exception("根据id获取详细信息为空");
            }
            Boolean unitOverrun = createCreativeService.getUnitOverrun(strategy.getAccountId());
            if (unitOverrun) {
                log.error("组创建超限,accountId:{}", strategy.getAccountId());
                throw new Exception("今日组创建已超限");
            }
            createCreativeService.customCreativeLimit(strategy);
            returnJson.put("code", 0);
            returnJson.put("message", "异步创建中");
        } catch (Exception e) {
            returnJson.put("code", -1);
            returnJson.put("message", e.getMessage());
        }
        return returnJson;

    }

    /**
     * 自定义创意补充
     *
     * @param id
     * @param hour
     * @return
     */
    @GetMapping(value = "/kuaishouCustomCreativeSupplement")
    public JSONObject kuaishouCustomCreativeSupplement(String id, Integer hour) {
        JSONObject returnJson = new JSONObject();
        try {
            if (Check.isNull(id)) {
                throw new Exception("主键id不能为空");
            }
            AiKuaishouAdvertiserStrategy strategy = strategyService.getById(id);
            if (Check.isNull(strategy)) {
                throw new Exception("根据id获取详细信息为空");
            }
            Boolean unitOverrun = createCreativeService.getUnitOverrun(strategy.getAccountId());
            if (unitOverrun) {
                log.error("组创建超限,accountId:{}", strategy.getAccountId());
                throw new Exception("今日组创建已超限");
            }
            if (Check.isNull(hour)) {
                throw new Exception("小时数据为空");
            }
            if (strategy.getHistoryTop() == 0) {
                kuaishouCustomCreativeSupplementExecutorService.submit(() -> {
                    try {
                        createCreativeService.customCreativeSupplement(strategy, hour);
                    } catch (ParseException e) {
                        e.printStackTrace();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                });
            } else {
                log.info("高质量暂停搭建,accountId:{}", strategy.getAccountId());
            }
            returnJson.put("code", 0);
            returnJson.put("message", "异步创建中");
        } catch (Exception e) {
            returnJson.put("code", -1);
            returnJson.put("message", e.getMessage());
        }
        return returnJson;

    }

    /**
     * 程序化上新素材
     *
     * @param id
     * @param videoCnt
     * @return
     */
    @GetMapping(value = "/kuaishouProgramCreativeAuto")
    public JSONObject kuaishouCustomCreativeSupplement(String id, Long videoCnt) {
        JSONObject returnJson = new JSONObject();
        try {
            if (Check.isNull(id)) {
                throw new Exception("主键id不能为空");
            }
            AiKuaishouAdvertiserStrategy strategy = strategyService.getById(id);
            if (Check.isNull(strategy)) {
                throw new Exception("根据id获取详细信息为空");
            }
            Boolean unitOverrun = createCreativeService.getUnitOverrun(strategy.getAccountId());
            if (unitOverrun) {
                log.error("组创建超限,accountId:{}", strategy.getAccountId());
                throw new Exception("今日组创建已超限");
            }
            if (Check.isNull(videoCnt)) {
                throw new Exception("视频数量不能为空");
            }
            kuaishouProgramCreativeAutoService.submit(() -> {
                createCreativeService.ProgramCreative(strategy, 1, videoCnt);

            });
            returnJson.put("code", 0);
            returnJson.put("message", "异步创建中");
        } catch (Exception e) {
            returnJson.put("code", -1);
            returnJson.put("message", e.getMessage());
        }
        return returnJson;

    }

    /**
     * 程序化历史高质量
     *
     * @param id
     * @param videoCnt
     * @return
     */
    @GetMapping(value = "/autoCreateProgramHistoryTopCreative")
    public JSONObject autoCreateProgramHistoryTopCreative(String id, Long videoCnt) {
        JSONObject returnJson = new JSONObject();
        try {
            if (Check.isNull(id)) {
                throw new Exception("主键id不能为空");
            }
            AiKuaishouAdvertiserStrategy strategy = strategyService.getById(id);
            if (Check.isNull(strategy)) {
                throw new Exception("根据id获取详细信息为空");
            }
            Boolean unitOverrun = createCreativeService.getUnitOverrun(strategy.getAccountId());
            if (unitOverrun) {
                log.error("组创建超限,accountId:{}", strategy.getAccountId());
                throw new Exception("今日组创建已超限");
            }
            if (Check.isNull(videoCnt)) {
                throw new Exception("视频数量不能为空");
            }
            if (strategy.getHistoryTop() == 0) {
                kuaishouProgramTopCreativeAutoService.submit(() -> {
                    createCreativeService.ProgramCreative(strategy, 2, videoCnt);
                });
            } else {
                log.info("历史高质量暂停搭建,accountId:{}", strategy.getAccountId());
            }
            returnJson.put("code", 0);
            returnJson.put("message", "异步创建中");
        } catch (Exception e) {
            returnJson.put("code", -1);
            returnJson.put("message", e.getMessage());
        }
        return returnJson;

    }


}
