package com.yonge.cooleshow.student.controller;

import com.yonge.cooleshow.auth.api.client.SysUserFeignService;
import com.yonge.cooleshow.biz.dal.entity.ImUserStateSync;
import com.yonge.cooleshow.biz.dal.service.ImGroupService;
import com.yonge.cooleshow.biz.dal.service.LiveRoomService;
import com.yonge.cooleshow.biz.dal.wrapper.liveroom.LiveRoomWrapper;
import com.yonge.cooleshow.common.controller.BaseController;
import com.yonge.cooleshow.common.entity.HttpResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 直播房间管理表(ImLiveBroadcastRoom)表控制层
 *
 * @author hgw
 * @since 2022-02-17 20:52:04
 */
@Api(tags = "直播房间管理表")
@RestController
@RequestMapping("${app-config.url.student:}/imLiveBroadcastRoom")
public class ImLiveBroadcastRoomController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private LiveRoomService liveRoomService;

    @Autowired
    private ImGroupService imGroupService;

    @Autowired
    private SysUserFeignService sysUserFeignService;

    @ApiOperation("学生端-查询房间信息")
    @GetMapping("/queryRoomInfo")
    public HttpResponseResult<LiveRoomWrapper.LiveRoomVo> queryRoomInfo(@ApiParam(value = "房间uid", required = true) String roomUid) {
        return succeed(liveRoomService.queryRoomInfo(roomUid));
    }


    @ApiOperation("查询房间信息并校验房间是否合规")
    @GetMapping("/queryRoom")
    public HttpResponseResult<LiveRoomWrapper.LiveRoomVo> queryRoomAndCheck(@ApiParam(value = "房间uid", required = true) String roomUid,
                                                                       @ApiParam(value = "用户id", required = true) String userId) {
        Long userIdLong = null;
        if (StringUtils.isNotBlank(userId)) {
            userIdLong = Long.parseLong(imGroupService.analysisImUserId(userId));
        }
        return succeed(liveRoomService.queryRoomAndCheck(roomUid, userIdLong, 1));
    }

    @PostMapping("/quitRoom")
    public HttpResponseResult<Object> quitRoom(@RequestBody List<ImUserStateSync> userState) {
        for (ImUserStateSync stateSync : userState) {
            stateSync.setUserid(imGroupService.analysisImUserId(stateSync.getUserid()));
        }
        liveRoomService.opsRoom(userState);
        return succeed();
    }

    @ApiOperation("学生-进入房间")
    @GetMapping("/joinRoom")
    public HttpResponseResult<Object> joinRoom(String roomUid, String userId,Boolean microphoneFlag) {
        if (microphoneFlag == null) {
            microphoneFlag = true;
        }
        Long userIdLong = null;
        if (StringUtils.isNotBlank(userId)) {
            userIdLong = Long.parseLong(imGroupService.analysisImUserId(userId));
        } else {
            userIdLong =sysUserFeignService.queryUserInfo().getId();
        }
        return succeed(liveRoomService.joinRoom(roomUid, userIdLong,microphoneFlag));
    }

    @ApiOperation("设置连麦状态")
    @PutMapping("/userWhetherMic")
    public HttpResponseResult<Object> userWhetherMic(@ApiParam(value = "房间uid", required = true) String roomUid,
                                                     @ApiParam(value = "用户id", required = true) String userId,
                                                     @ApiParam(value = "连麦状态 0:未申请1:申请连麦中2:连麦中", required = true) Integer whetherMicStatus) {
        Long userIdLong = null;
        if (StringUtils.isNotBlank(userId)) {
            userIdLong = Long.parseLong(imGroupService.analysisImUserId(userId));
        }
        liveRoomService.userWhetherMic(roomUid,userIdLong,whetherMicStatus);
        return succeed();
    }

}

