package com.yonge.cooleshow.teacher.controller;

import com.microsvc.toolkit.middleware.live.message.TencentWrapper;
import com.yonge.cooleshow.biz.dal.dto.LiveRoomStatus;
import com.yonge.cooleshow.biz.dal.service.ImGroupService;
import com.yonge.cooleshow.biz.dal.service.ImLiveBroadcastRoomMemberService;
import com.yonge.cooleshow.biz.dal.service.LiveBroadcastRoomMemberService;
import com.yonge.cooleshow.biz.dal.service.LiveRoomService;
import com.yonge.cooleshow.biz.dal.wrapper.liveroom.ImLiveBroadcastRoomMemberWrapper;
import com.yonge.cooleshow.biz.dal.wrapper.liveroom.LiveRoomWrapper;
import com.yonge.cooleshow.common.controller.BaseController;
import com.yonge.cooleshow.common.entity.HttpResponseResult;
import com.yonge.toolset.base.page.PageInfo;
import io.swagger.annotations.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;
import java.util.Optional;

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

    @Autowired
    private ImLiveBroadcastRoomMemberService imLiveBroadcastRoomMemberService;

    @Autowired
    private ImGroupService imGroupService;

    @ApiOperation("查询房间信息并校验房间是否合规")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roomUid", dataType = "String", value = "房间uid", required = true),
            @ApiImplicitParam(name = "userId", dataType = "Integer", value = "用户id", required = true),
            @ApiImplicitParam(name = "liveAssistant", dataType = "Boolean", value = "直播助手"),
    })
    @GetMapping("/queryRoom")
    public HttpResponseResult<LiveRoomWrapper.LiveRoomVo> queryRoomAndCheck(@ApiParam(value = "房间uid", required = true) String roomUid,
                                                                 @ApiParam(value = "用户id", required = true) Long userId,
                                                                 @RequestParam(defaultValue = "false") Boolean liveAssistant) {

        // 请求来源业源
        int osType = 2; // 老师端
        if (Optional.ofNullable(liveAssistant).orElse(false)) {
            osType = 3; // 直播助手
        }

        return succeed(liveRoomService.queryRoomAndCheck(roomUid, userId, osType));
    }

    @ApiOperation("关闭直播间")
    @GetMapping(value = "/roomDestroy")
    public HttpResponseResult<Object> roomDestroy(@ApiParam(value = "房间Uid", required = true) String roomUid) {
        liveRoomService.destroyLiveRoom(roomUid);
        return succeed();
    }

    @ApiOperation("同步点赞数量")
    @GetMapping("/syncLike")
    public HttpResponseResult<Object> syncLike(@ApiParam(value = "房间uid", required = true) String roomUid,
                                               @ApiParam(value = "点赞数", required = true) Integer likeNum) {
        liveRoomService.syncLike(roomUid, likeNum);
        return succeed();
    }

    @ApiOperation("主讲人进入房间")
    @GetMapping("/speakerJoinRoom")
    public HttpResponseResult<LiveRoomWrapper.LiveRoomVo> speakerJoinRoom(String roomUid) {
        return succeed(liveRoomService.speakerCheckRoomInfo(roomUid));
    }

    @ApiOperation("获取房间人员")
    @GetMapping("/queryRoomUserInfo")
    public HttpResponseResult<List<LiveRoomWrapper.BaseRoomUserVo>> queryRoomUserInfo(@ApiParam(value = "房间uid", required = true) String roomUid) {
        return succeed(liveRoomService.queryRoomLimitOnlineUserInfo(roomUid));
    }

    @ApiOperation("设置是否允许连麦")
    @GetMapping("/whetherMic")
    public HttpResponseResult<Object> whetherMic(@ApiParam(value = "房间uid", required = true) String roomUid,
                                                 @ApiParam(value = "是否连麦 0：是 1否", required = true) Integer whetherMic) {
        liveRoomService.whetherMic(roomUid,whetherMic);
        return succeed();
    }


    @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();
    }

    @ApiOperation("分页查询直播间人员列表")
    @ApiImplicitParams({
      @ApiImplicitParam(name = "search", dataType = "String", value = "模糊搜索 学员编号姓名"),
      @ApiImplicitParam(name = "roomUid", dataType = "String", value = "房间uid"),
      @ApiImplicitParam(name = "onlineStatus", dataType = "String ", value = "0:离线 1：在线  不传是全部"),
      @ApiImplicitParam(name = "whetherMicStatus", dataType = "String", value = "连麦状态 0:未申请1:申请连麦中2:连麦中"),
      @ApiImplicitParam(name = "page", dataType = "Integer", value = "页数"),
      @ApiImplicitParam(name = "rows", dataType = "Integer", value = "每页数量"),
    })
    @PostMapping("/queryRoomMember")
    public HttpResponseResult<PageInfo<ImLiveBroadcastRoomMemberWrapper.ImLiveBroadcastRoomMember>> queryRoomMember(@RequestBody Map<String, Object> param) {
        return succeed(imLiveBroadcastRoomMemberService.queryRoomMember(param));
    }


    @ApiOperation("更新直播间状态")
    @PostMapping("/updateRoomStatus")
    public HttpResponseResult<Boolean> updateRoomStatus(@RequestBody @Valid LiveRoomStatus status ) {
        return succeed(liveRoomService.updateRoomStatus(status));
    }

    @ApiOperation("直播间推流状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roomUid", dataType = "String", value = "房间Uid", required = true),
    })
    @GetMapping(value = "/liveStreamStatus")
    public HttpResponseResult<TencentWrapper.LiveStreamState> roomLiveStream(String roomUid) {

        // 直播间推流状态查询
        return succeed(liveRoomService.roomLiveStreamStatus(roomUid));
    }
}

