package com.red.circle.live.app.command.user.qry;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.red.circle.common.business.dto.cmd.app.AppRoomIdCmd;
import com.red.circle.external.inner.endpoint.message.AgoraClient;
import com.red.circle.framework.core.asserts.ResponseAssert;
import com.red.circle.live.app.command.mic.qry.LiveMicListQryExe;
import com.red.circle.live.app.convertor.LiveMicAppConvertor;
import com.red.circle.live.app.dto.clientobject.LiveMicUserCO;
import com.red.circle.live.app.dto.clientobject.LiveMicrophoneCO;
import com.red.circle.live.domain.gateway.LiveMicrophoneGateway;
import com.red.circle.live.domain.live.LiveMicUser;
import com.red.circle.other.inner.endpoint.user.user.UserProfileClient;
import com.red.circle.other.inner.model.cmd.user.UserAccountQryCmd;
import com.red.circle.other.inner.model.dto.user.UserProfileDTO;
import com.red.circle.tool.core.collection.CollectionUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 房间正在说话用户.
 * 查询的声网
 *
 * @author pengliang on 2023/12/11
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class LiveSpeakUserQryExe {

    private final LiveMicAppConvertor liveMicAppConvertor;
    private final LiveMicrophoneGateway liveMicrophoneGateway;
    private final UserProfileClient userProfileClient;
    private final AgoraClient agoraClient;
    private final LiveOnlineUserQryExe liveOnlineUserQryExe;

    public List<LiveMicUserCO> execute(AppRoomIdCmd cmd) {
        // 查询当前房间用户
        List<LiveMicUserCO> execute = liveOnlineUserQryExe.execute(cmd);
        List<Long> userIds = CollectionUtils.isEmpty(execute) ? CollectionUtils.newArrayList()
                : execute.stream()
                .map(LiveMicUserCO::getId).toList();
        List<Long> broadcasters = agoraClient.channelUser(cmd.getRoomId().toString()).getBody();
        log.info("获取到的所有用户Id:{}", broadcasters);
        if (CollectionUtils.isEmpty(broadcasters)) {
            return CollectionUtils.newArrayList();
        }
        List<UserProfileDTO> users = ResponseAssert.requiredSuccess(
                userProfileClient.listByAccount(
                        new UserAccountQryCmd()
                                .setSysOrigin(cmd.requireReqSysOriginEnum().getSysOrigin())
                                .setAccounts(broadcasters.stream().map(String::valueOf).collect(Collectors.toSet()))
                )
        );
        if (CollectionUtils.isEmpty(users)) {
            return CollectionUtils.newArrayList();
        }
        Map<Long, LiveMicUser> longLiveMicUserMap = liveMicrophoneGateway.mapLiveMicUser(
                cmd.getRoomId(),
                new HashSet<>(users.stream().map(UserProfileDTO::getId).collect(Collectors.toSet()))
        );
        List<LiveMicUserCO> list = users.stream()
                .map(user -> liveMicAppConvertor.toLiveMicUserCO(
                        longLiveMicUserMap.get(user.getId())))
                .filter(Objects::nonNull)
                .toList();
        list.forEach(user -> user.setIsGhost(!userIds.contains(user.getId())));
        return list;
    }

    public List<Long> channelHost(AppRoomIdCmd cmd) {
        return agoraClient.channelHost(String.valueOf(cmd.getRoomId())).getBody();
    }
}
