package com.xueyi.nlt.nlt.template;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONException;
import com.alibaba.fastjson2.JSONObject;
import com.xueyi.common.core.context.SecurityContextHolder;
import com.xueyi.nlt.netty.client.WebSocketClient;
import com.xueyi.nlt.nlt.context.TerminalSecurityContextHolder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;


@Service("free-chat")
public class FreeChatTemplate implements BaseTemplate{

    private static final Logger log = LoggerFactory.getLogger(FreeChatTemplate.class);
    @Autowired
    WebSocketClient webSocketClient;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @Override
    public JSONObject handle(String devId, String content) {
        Long operatorId = TerminalSecurityContextHolder.getOperatorId();
        String redisKey = "group:nlp:" + SecurityContextHolder.getLocalMap().get("enterprise_id") + ":" + operatorId;
        // 根据content内容调用模版并返回结果
        synchronized (WebSocketClient.LOCK) {
            // 通过redis获取数字人上下文信息
            Long size = redisTemplate.opsForList().size(redisKey);
            if (size > 8) {
                redisTemplate.opsForList().leftPop(redisKey,2);
            }
            redisTemplate.opsForList().rightPush(redisKey,content);
            size = redisTemplate.opsForList().size(redisKey);
            List<String> context = redisTemplate.opsForList().range(redisKey,size-7,size);

            String result = null;
            webSocketClient.sendMsg(context);
            try {
                WebSocketClient.LOCK.wait();
                result = (String)redisTemplate.opsForValue().get("group:websocket:content");
            } catch (InterruptedException e) {
                log.warn(e.getMessage());
            }

            String[] blockedWord = {"科大讯飞", "认知模型", "抱歉"};
            for(String a :blockedWord){
                if(result.contains(a)){
                    result = "";
                    break;
                }
            }

            String resultFinal = "";
            //精简结果并赋予身份
            if(!StringUtils.isEmpty(result)){
                String promptBefore ="你需要完成的目标任务是：对我说的话进行总结。我的需求是：1、回答简短得体自然。50字以内。按照以上要求，我的输入是：";
                String promptAfter = "";
                webSocketClient.sendMsg(promptBefore + result + promptAfter);
                try {
                    WebSocketClient.LOCK.wait();
                    resultFinal = (String)redisTemplate.opsForValue().get("group:websocket:content");
                } catch (InterruptedException e) {
                    log.warn(e.getMessage());
                }
            }
            redisTemplate.opsForList().rightPush(redisKey,resultFinal);
            JSONObject resultJson = new JSONObject();
            resultJson.put("msg",resultFinal);
            return resultJson;
        }
    }

    @Override
    public JSONObject handle(String dev, String content, Long tenantId) {
        return null;
    }
}