package com.tzld.piaoquan.api;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.tzld.piaoquan.api.component.AigcApiService;
import com.tzld.piaoquan.api.component.ManagerApiService;
import com.tzld.piaoquan.api.dao.mapper.GhDetailExtMapper;
import com.tzld.piaoquan.api.model.po.GhDetailExt;
import com.tzld.piaoquan.api.model.po.GhDetailExtExample;
import com.tzld.piaoquan.growth.common.dao.mapper.CgiReplyBucketDataMapper;
import com.tzld.piaoquan.growth.common.dao.mapper.ext.CgiReplyBucketDataMapperExt;
import com.tzld.piaoquan.growth.common.model.po.CgiReplyBucketData;
import com.tzld.piaoquan.growth.common.model.po.CgiReplyBucketDataExample;
import com.tzld.piaoquan.growth.common.utils.MessageUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

@SpringBootTest(classes = GrowthServerApplication.class)
@Slf4j
public class GhDetailTest {

    @Autowired
    GhDetailExtMapper ghDetailExtMapper;
    @Autowired
    ManagerApiService managerApiService;
    @Autowired
    CgiReplyBucketDataMapper cgiReplyBucketDataMapper;
    @Autowired
    CgiReplyBucketDataMapperExt cgiReplyBucketDataMapperExt;
    @Autowired
    AigcApiService aigcApiService;

    @Test
    public void testGhDetailExtId() {
        GhDetailExtExample example = new GhDetailExtExample();
        example.createCriteria().andIsDeleteEqualTo(0);
        List<GhDetailExt> ghDetailExtList = ghDetailExtMapper.selectByExample(example);
        for (GhDetailExt ghDetailExt : ghDetailExtList) {
            setCustomTitle(ghDetailExt);
            setCustomCover(ghDetailExt);
            ghDetailExtMapper.updateByPrimaryKeySelective(ghDetailExt);
        }
    }

    private void setCustomTitle(GhDetailExt ghDetailExt) {
        if (StringUtils.isNotEmpty(ghDetailExt.getTitle())) {
            JSONArray multiTitleList = managerApiService.videoMultiTitleListV2(ghDetailExt.getVideoId());
            boolean titleExist = false;
            if (!CollectionUtils.isEmpty(multiTitleList)) {
                for (int i = 0; i < multiTitleList.size(); i++) {
                    JSONObject item = multiTitleList.getJSONObject(i);
                    if (item.getString("title").equals(ghDetailExt.getTitle())) {
                        ghDetailExt.setTitleId(item.getLong("id"));
                        titleExist = true;
                        break;
                    }
                }
            }
            if (!titleExist) {
                JSONObject multiTitle = managerApiService.videoMultiTitleSave(ghDetailExt.getVideoId(), ghDetailExt.getTitle());
                ghDetailExt.setTitleId(multiTitle.getLong("id"));
            }
        }
    }

    private void setCustomCover(GhDetailExt ghDetailExt) {
        if (StringUtils.isNotEmpty(ghDetailExt.getCover())) {
            JSONArray multiCoverList = managerApiService.videoMultiCoverListV2(ghDetailExt.getVideoId());
            boolean customCoverExist = false;
            if (!CollectionUtils.isEmpty(multiCoverList)) {
                for (int i = 0; i < multiCoverList.size(); i++) {
                    JSONObject item = multiCoverList.getJSONObject(i);
                    if (item.getString("coverUrl").equals(ghDetailExt.getCover())) {
                        ghDetailExt.setCoverId(item.getLong("id"));
                        customCoverExist = true;
                        break;
                    }
                }
            }
            if (!customCoverExist) {
                JSONObject multiCover = managerApiService.videoMultiCoverSave(ghDetailExt.getVideoId(), ghDetailExt.getCover());
                ghDetailExt.setCoverId(multiCover.getLong("id"));
            }
        }
    }

    @Test
    public void refreshCgiReplyBucketData() {
        CgiReplyBucketDataExample example = new CgiReplyBucketDataExample();
        example.createCriteria().andIsDeleteEqualTo(0).andTitleIsNull();
        List<CgiReplyBucketData> list = cgiReplyBucketDataMapper.selectByExample(example);
        if (!CollectionUtils.isEmpty(list)) {
            for (CgiReplyBucketData item : list) {
                CgiReplyBucketData oldItem = cgiReplyBucketDataMapperExt.getOldCgiReplyData(item.getMiniVideoId());
                if (oldItem != null) {
                    item.setTitle(oldItem.getTitle());
                    item.setCoverUrl(oldItem.getCoverUrl());
                    cgiReplyBucketDataMapper.updateByPrimaryKeySelective(item);
                }
            }
        }
    }

    @Test
    public void refreshGzhAutoReplyMsgData() {
        CgiReplyBucketDataExample example = new CgiReplyBucketDataExample();
        example.createCriteria().andIsDeleteEqualTo(0);
        List<CgiReplyBucketData> list = cgiReplyBucketDataMapper.selectByExample(example);
        List<String> ghIds = list.stream().map(CgiReplyBucketData::getGhId).distinct().collect(Collectors.toList());
        for (String ghId : ghIds) {
            aigcApiService.refreshGzhAutoReplyMsgData(ghId);
        }
    }

    @Test
    public void setRootSourceId() {
        CgiReplyBucketDataExample example = new CgiReplyBucketDataExample();
        example.createCriteria().andRootSourceIdIsNull();
        List<CgiReplyBucketData> list = cgiReplyBucketDataMapper.selectByExample(example);
        for (CgiReplyBucketData item : list) {
            item.setRootSourceId(MessageUtil.getRootSourceId(item.getMiniPagePath()));
            cgiReplyBucketDataMapper.updateByPrimaryKeySelective(item);
        }
    }

    @Test
    public void searchVideoByTitle() {
        String title = "历史";
        JSONObject res = managerApiService.searchVideoByTitle(title, 1, 10);
        System.out.println(res.toJSONString());
    }

}
