package com.ruoyi.cai.chat;

import com.ruoyi.cai.config.CaiProperties;
import com.ruoyi.cai.domain.Anchor;
import com.ruoyi.cai.domain.User;
import com.ruoyi.cai.domain.UserCall;
import com.ruoyi.cai.dto.app.dto.GuardNum;
import com.ruoyi.cai.dto.app.query.CallReq;
import com.ruoyi.cai.dto.app.vo.chat.CallResp;
import com.ruoyi.cai.dto.app.vo.chat.GetRoomResp;
import com.ruoyi.cai.enums.SystemConfigEnum;
import com.ruoyi.cai.manager.SystemConfigManager;
import com.ruoyi.cai.service.AnchorService;
import com.ruoyi.cai.service.GuardTotalService;
import com.ruoyi.cai.service.UserCallService;
import com.ruoyi.cai.service.UserService;
import com.ruoyi.cai.ws.bean.Room;
import com.ruoyi.cai.ws.manager.WebSocketManager;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.helper.LoginHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

@Component
public class ChatManager {
    @Autowired
    private UserService userService;
    @Autowired
    private AnchorService anchorService;
    @Autowired
    private WebSocketManager webSocketManager;
    @Autowired
    private UserCallService userCallService;
    @Autowired
    private SystemConfigManager systemConfigManager;
    @Autowired
    private CaiProperties properties;
    @Autowired
    private GuardTotalService guardTotalService;

    public CallResp call(CallReq callReq){
        Long userId = LoginHelper.getUserId();
        User fromUser = userService.getById(userId);
        User toUser = userService.getById(callReq.getToUid());
        if(toUser.getIsAnchor() != 1){
            throw new ServiceException("对方未通过女神认证，不能接听视频");
        }
        if(fromUser.getGender() == 1 && toUser.getGender() == 1){
            throw new ServiceException("主播和主播之间，不可以拨打视频哦~");
        }
        Anchor anchor = anchorService.getByUserId(toUser.getId());
        if(anchor == null){
            throw new ServiceException("主播技能不存在");
        }
        Long roomId = null;
        Room room = webSocketManager.checkOnlineRoom(fromUser.getId(), toUser.getId());
        if(room == null){
            UserCall call = userCallService.createCall(fromUser, toUser, anchor);
            roomId = webSocketManager.createRoom(call.getId());
        }
        String weSocketUrl = String.format(properties.getWebSocketUrl(),"token",roomId);
        Long guardPrice = systemConfigManager.getSystemConfigOfLong(SystemConfigEnum.GUARD_PRICE);
        // 获取鉴黄规则 TODO
        CallResp callResp = new CallResp();
        callResp.setRoomId(roomId);
        callResp.setFromUid(fromUser.getId());
        callResp.setFromNickname(fromUser.getNickname());
        callResp.setFromAvatar(fromUser.getAvatar());
        callResp.setToUid(toUser.getId());
        callResp.setToNickname(toUser.getNickname());
        callResp.setToAvatar(toUser.getAvatar());
        callResp.setSocketUrl(weSocketUrl);
        callResp.setPrice(anchor.getPrice());
        callResp.setScore(anchor.getGiveScore());
        callResp.setServiceCount(anchor.getServiceCount());
        callResp.setServiceTime(anchor.getServiceTime());
        GuardNum guardNum = guardTotalService.getGuardNum(fromUser.getId(), toUser.getId());
        callResp.setGuardDiffNum(guardNum.getDiffNum());
        callResp.setGuardPrice(guardPrice);
        return callResp;
    }

    public GetRoomResp getRoom(String roomId){
        Long userId = LoginHelper.getUserId();
        UserCall userCall = userCallService.getById(roomId);
        if(userCall == null){
            throw new ServiceException("房间不存在");
        }
        if(!userCall.getFromUid().equals(userId) && !userCall.getToUid().equals(userId)){
            throw new ServiceException("无权限操作房间");
        }
        Room room = webSocketManager.checkOnlineRoom(userCall.getFromUid(), userCall.getToUid());
        if (room == null) {
            throw new ServiceException("'对方已取消通话'");
        }
        List<User> userList = userService.listByIds(Arrays.asList(userCall.getFromUid(), userCall.getToUid()));
        Map<Long, User> userMap = userList.stream().collect(Collectors.toMap(User::getId, Function.identity()));
        User fromUser = userMap.get(userCall.getFromUid());
        User toUser = userMap.get(userCall.getToUid());
        Anchor anchor = anchorService.getByUserId(toUser.getId());
        String weSocketUrl = String.format(properties.getWebSocketUrl(),"token",roomId);
        GetRoomResp getRoomResp = new GetRoomResp();
        getRoomResp.setRoomId(userCall.getId()+"");
        getRoomResp.setFromUid(userCall.getFromUid());
        getRoomResp.setFromNickname(fromUser.getNickname());
        getRoomResp.setFromAvatar(fromUser.getAvatar());
        getRoomResp.setToUid(userCall.getToUid());
        getRoomResp.setToNickname(toUser.getNickname());
        getRoomResp.setToAvatar(toUser.getAvatar());
        getRoomResp.setSocketUrl(weSocketUrl);
        getRoomResp.setPrice(anchor.getPrice());
        getRoomResp.setScore(anchor.getGiveScore());
        getRoomResp.setServiceCount(anchor.getServiceCount());
        getRoomResp.setServiceTime(anchor.getServiceTime());
        return getRoomResp;
    }

}
