package com.yunyan.business.service.impl;

import com.yunyan.business.service.IBaiduNlpService;
import com.yunyan.business.utils.baiduNlp.BaiduEcnetUtils;
import com.yunyan.business.utils.baiduNlp.pojo.BaiduEcnetDto;
import com.yunyan.business.utils.baiduNlp.pojo.BaiduEcnetVo;
import com.yunyan.business.utils.baiduNlp.pojo.list.BaiduEcnetAndIndexDto;
import com.yunyan.business.utils.baiduNlp.pojo.list.BaiduEcnetAndIndexItemVo;
import com.yunyan.business.utils.baiduNlp.pojo.list.BaiduEcnetAndIndexListDto;
import com.yunyan.business.utils.baiduNlp.pojo.list.BaiduEcnetAndIndexListVo;
import com.yunyan.common.core.domain.AjaxResultVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


@Slf4j
@Service
public class BaiduNlpServiceImpl implements IBaiduNlpService {


    @Override
    public AjaxResultVo checkWord(BaiduEcnetDto dto) {

        long startTime = System.currentTimeMillis();
        if(dto.getText().length() > 500) {
            return AjaxResultVo.error("检测的文本不能超过550汉字");
        }

        //判断 token
        String token = "24.cb8048a203e185c513d1e1141218939b.2592000.1700893836.282335-38824649";

        BaiduEcnetVo vo = BaiduEcnetUtils.ecnet(dto.getText(), token);

        long endTime = System.currentTimeMillis();
        long usedTime = (endTime-startTime)/1000;
        log.debug("检测文本耗时 [{}s] - [{}ms]", usedTime, endTime - startTime);

        return AjaxResultVo.success(vo);
    }

    @Override
    public AjaxResultVo checkWordList(BaiduEcnetAndIndexListDto dto) {
        if(dto.getList().isEmpty()) {
            return AjaxResultVo.error("检测的文本索引list不能为空");
        }

        for (BaiduEcnetAndIndexDto item : dto.getList()) {
            if(item.getText().length() > 500) {
                return AjaxResultVo.error("检测的文本不能超过550汉字");
            }
        }

        //判断 token
        String token = "24.0933ba0af34f39bc820179e233d46000.2592000.1736318717.282335-38824649";

        //检测结果
        BaiduEcnetAndIndexListVo result = new BaiduEcnetAndIndexListVo();
        List<BaiduEcnetAndIndexItemVo> resultList = new ArrayList<>();
        result.setList(resultList);

        for (BaiduEcnetAndIndexDto item : dto.getList()) {
            if(item.getText().length() > 500) {
                log.error(String.format("检测的文本[%s]不能超过550汉字", item.getIndex()));
                return AjaxResultVo.error(String.format("检测的文本[%s]不能超过550汉字", item.getIndex()));
            }

            BaiduEcnetVo vo = BaiduEcnetUtils.ecnet(item.getText(), token);
            BaiduEcnetAndIndexItemVo resultItem = new BaiduEcnetAndIndexItemVo();
            resultItem.setIndex(item.getIndex());
            resultItem.setEcnetVo(vo);
            resultList.add(resultItem);
        }

        return AjaxResultVo.success(result);
    }
}
