package org.jeecg.modules.ctop.service.impl;

import cn.com.ctop.common.module.service.IBindAccountLoginService;
import cn.com.ctop.common.module.utils.HttpUtils;
import cn.com.ctop.crawler.modules.kuaishoucce.entity.KuaishouEffectAdInfo;
import cn.com.ctop.crawler.modules.kuaishoucce.entity.KuaishouHotPhotoInfo;
import cn.com.ctop.crawler.modules.kuaishoucce.entity.KuaishouInspiredAd;
import cn.com.ctop.crawler.modules.kuaishoucce.service.IKuaishouEffectAdInfoService;
import cn.com.ctop.crawler.modules.kuaishoucce.service.IKuaishouHotPhotoInfoService;
import cn.com.ctop.crawler.modules.kuaishoucce.service.IKuaishouInspiredAdService;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.ctop.service.ICrawlerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Service
@Slf4j
public class CrawlerServiceImpl implements ICrawlerService {
    @Autowired
    private IBindAccountLoginService bindAccountLoginService;
    @Autowired
    private IKuaishouInspiredAdService inspiredAdService;
    @Autowired
    private IKuaishouHotPhotoInfoService hotPhotoInfoService;
    @Autowired
    private IKuaishouEffectAdInfoService effectAdInfoService;


    @Override
    public Map<String, Object> loadInspiration() {
        //(1):效果广告解析
        loadeffectAdByPage(100, 1);
        //(2):广告创意视频
        loadInspiredAdByPage(100, 1);
        //(3):快手热门作品
        loadHotPhotoByPage(100, 1);
        return null;
    }

    private void loadHotPhotoByPage(int pageSize, int pageNum) {
        String url = "https://cc.e.kuaishou.com/rest/creativeCentor/hotPhoto/list";
        JSONObject param = new JSONObject();
        param.put("pageNum", pageNum);
        param.put("pageSize", pageSize);
        param.put("sortType", 1);
        String result = HttpUtils.httpPostRequest(url, param, new HashMap<>());
        JSONObject resultObject = JSONObject.parseObject(result);
        if (null == resultObject.getLong("result") || resultObject.getLong("result") != 1) {
            log.info("热门作品数据获取失败");
            return;
        }
        JSONArray hotPhotoInfos = resultObject.getJSONObject("data").getJSONArray("hotPhotoInfos");
        if (null == hotPhotoInfos || hotPhotoInfos.isEmpty()) {
            log.info("热门作品数据获取完成");
            return;
        }
        List<KuaishouHotPhotoInfo> photoInfos = new ArrayList<>();
        for (int i = 0; i < hotPhotoInfos.size(); i++) {
            JSONObject photoInfoJSONObject = hotPhotoInfos.getJSONObject(i);
            KuaishouHotPhotoInfo hotPhotoInfo = new KuaishouHotPhotoInfo(photoInfoJSONObject);
            photoInfos.add(hotPhotoInfo);
        }
        hotPhotoInfoService.replaceBatch(photoInfos);
        loadHotPhotoByPage(pageSize, pageNum + 1);
    }

    private void loadInspiredAdByPage(int pageSize, int pageNumber) {
        String url = "https://cc.e.kuaishou.com/rest/creativeCentor/inspired/list";
        JSONObject param = new JSONObject();
        param.put("pageNum", pageNumber);
        param.put("pageSize", pageSize);
        param.put("firstIndustryId", 0);
        param.put("formatId", 0);
        param.put("inspiredSortTypeId", 0);
        param.put("platformSourceId", 0);
        param.put("queryCond", "");
        String result = HttpUtils.httpPostRequest(url, param, new HashMap<>());
        JSONObject resultObject = JSONObject.parseObject(result);
        if (null == resultObject.getLong("result") || resultObject.getLong("result") != 1) {
            log.info("广告创意数据获取失败");
            return;
        }
        JSONArray inspiredAds = resultObject.getJSONObject("data").getJSONArray("inspiredAds");
        if (null == inspiredAds || inspiredAds.size() <= 0) {
            log.info("广告创意数据获取完成");
            return;
        }
        List<KuaishouInspiredAd> ads = new ArrayList<>();
        for (int i = 0; i < inspiredAds.size(); i++) {
            JSONObject ad = inspiredAds.getJSONObject(i);
            KuaishouInspiredAd inspiredAd = new KuaishouInspiredAd(ad);
            ads.add(inspiredAd);
        }
        inspiredAdService.replaceBatch(ads);
        loadInspiredAdByPage(pageSize, pageNumber + 1);
    }

    private void loadeffectAdByPage(int pageSize, int pageNum) {
        String url = "https://cc.e.kuaishou.com/rest/creativeCentor/adCase/effectAd/list";
        JSONObject param = new JSONObject();
        param.put("caseType", 1);
        param.put("pageNum", pageNum);
        param.put("pageSize", pageSize);
        String result = HttpUtils.httpPostRequest(url, param, new HashMap<>());
        JSONObject resultObject = JSONObject.parseObject(result);
        if (null == resultObject.getLong("result") || resultObject.getLong("result") != 1) {
            log.info("效果广告数据获取失败");
            return;
        }
        JSONArray effectAds = resultObject.getJSONObject("data").getJSONArray("effectAds");
        if (null == effectAds || effectAds.isEmpty()) {
            log.info("效果广告数据获取完成");
            return;
        }
        for (int i = 0; i < effectAds.size(); i++) {
            JSONObject ad = effectAds.getJSONObject(i);
            Long contentId = ad.getLong("contentId");
            if (null != contentId) {
                JSONObject content = getEffectAdDetail(contentId).getJSONObject("data").getJSONObject("effectAdDetail");
                KuaishouEffectAdInfo adInfo = new KuaishouEffectAdInfo(content);
                effectAdInfoService.saveOrUpdate(adInfo);
            }
        }
        loadeffectAdByPage(pageSize, pageNum + 1);
    }

    private JSONObject getEffectAdDetail(Long contentId) {
        String url = "https://cc.e.kuaishou.com/rest/creativeCentor/adCase/effectAd/detail";
        JSONObject param = new JSONObject();
        param.put("caseType", 1);
        param.put("contentId", contentId);
        String result = HttpUtils.httpPostRequest(url, param, new HashMap<>());
        return JSONObject.parseObject(result);
    }
}
