package com.tzld.piaoquan.api.service.strategy;

import com.tzld.piaoquan.api.common.enums.ReplyStrategyServiceEnum;
import com.tzld.piaoquan.api.model.bo.BucketDataParam;
import com.tzld.piaoquan.api.model.bo.ReplyBucketData;
import com.tzld.piaoquan.growth.common.model.po.CgiReplyBucketData;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;

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

public interface ReplyStrategyService {

    ReplyBucketData getResult(BucketDataParam bucketDataParam);

    Boolean support(ReplyStrategyServiceEnum key);

    static List<CgiReplyBucketData> checkCgiReplyBucketDataDiff(List<CgiReplyBucketData> result,
                                                                List<CgiReplyBucketData> existCgiReplyBucketDataList) {
        if (CollectionUtils.isEmpty(result)) {
            return null;
        }
        if (result.size() != existCgiReplyBucketDataList.size()) {
            return result;
        }
        Map<Long, CgiReplyBucketData> existCgiReplyBucketDataMap = existCgiReplyBucketDataList.stream()
                .collect(Collectors.toMap(CgiReplyBucketData::getMiniVideoId, x -> x, (a, b) -> b));
        boolean diff = false;
        for (CgiReplyBucketData cgiReplyBucketData : result) {
            if (!existCgiReplyBucketDataMap.containsKey(cgiReplyBucketData.getMiniVideoId())) {
                diff = true;
                break;
            }
            if (!StringUtils.equals(cgiReplyBucketData.getTitle(), existCgiReplyBucketDataMap.get(cgiReplyBucketData.getMiniVideoId()).getTitle())) {
                diff = true;
                break;
            }
            String resultCoverUrl = cgiReplyBucketData.getCoverUrl();
            String existCoverUrl = existCgiReplyBucketDataMap.get(cgiReplyBucketData.getMiniVideoId()).getCoverUrl();
            // cover处理，获取url
            if (StringUtils.isNotEmpty(resultCoverUrl)) {
                resultCoverUrl = resultCoverUrl.split("\\?")[0];
            }
            if (StringUtils.isNotEmpty(existCoverUrl)) {
                existCoverUrl = existCoverUrl.split("\\?")[0];
            }
            if (!StringUtils.equals(resultCoverUrl, existCoverUrl)) {
                diff = true;
                break;
            }
        }
        return diff ? result : null;
    }

}
