package com.xueyi.nlt.nlt.service.impl;

import com.xueyi.nlt.netty.client.WebSocketClient;
import com.xueyi.nlt.nlt.domain.LlmContext;
import com.xueyi.nlt.nlt.domain.LlmParam;
import com.xueyi.nlt.nlt.domain.LlmResponse;
import com.xueyi.nlt.nlt.service.ISysLlmService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

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

@Primary
@Service
public class SparkServiceImpl implements ISysLlmService {

    @Autowired
    WebSocketClient webSocketClient;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Override
    public LlmResponse chat(LlmContext context, LlmParam param) {
        List<String> contentArr = context.getContextList().stream().map(LlmContext.Context::getContent).collect(Collectors.toList());
        synchronized (WebSocketClient.LOCK) {
            webSocketClient.sendMsg(contentArr);
            try {
                WebSocketClient.LOCK.wait();

            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            String result = redisTemplate.opsForValue().get("group:websocket:content");
            LlmResponse response = new LlmResponse();
            response.setContent(result);
            return response;
        }

    }
}
