package com.ym.mec.teacher.controller;

import com.ym.mec.biz.dal.dto.RoomReservationUserSearch;
import com.ym.mec.biz.dal.page.LiveRoomGoodsOrderQueryInfo;
import com.ym.mec.biz.dal.vo.BaseRoomUserVo;
import com.ym.mec.biz.dal.vo.ImLiveBroadcastRoomVo;
import com.ym.mec.biz.dal.vo.LiveRoomGoodsOrderVo;
import com.ym.mec.biz.dal.vo.RoomReservationUserVo;
import com.ym.mec.biz.service.ImLiveBroadcastRoomService;
import com.ym.mec.common.controller.BaseController;
import com.ym.mec.common.entity.HttpResponseResult;
import com.ym.mec.common.entity.ImUserState;
import com.ym.mec.common.page.PageInfo;
import io.swagger.annotations.*;
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 ImLiveBroadcastRoomService imLiveBroadcastRoomService;

    @ApiImplicitParams({
            @ApiImplicitParam(name = "search", dataType = "String", value = "模糊查询关键字"),
            @ApiImplicitParam(name = "liveState", dataType = "Integer", value = "直播状态 0未开始 1开始 2结束"),
            @ApiImplicitParam(name = "startTime", dataType = "String", value = "开始时间"),
            @ApiImplicitParam(name = "endTime", dataType = "String", value = "结束时间"),
            @ApiImplicitParam(name = "popularize", dataType = "Integer", value = "是否在首页推广 0否 1是"),
            @ApiImplicitParam(name = "speakerId", dataType = "Integer", value = "老师ID"),
            @ApiImplicitParam(name = "page", dataType = "Integer", value = "页数"),
            @ApiImplicitParam(name = "rows", dataType = "Integer", value = "每页数量"),
    })
    @ApiOperation("分页查询直播间列表")
    @PostMapping("/queryPage")
    public HttpResponseResult<PageInfo<ImLiveBroadcastRoomVo>> queryPage(@RequestBody Map<String, Object> param) {
        param.put("os", "teacher");
        return succeed(imLiveBroadcastRoomService.queryPage(param));
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "search", dataType = "String", value = "模糊查询关键字"),
            @ApiImplicitParam(name = "subjectId", dataType = "Integer", value = "声部id"),
            @ApiImplicitParam(name = "organId", dataType = "Integer", value = "分部id"),
            @ApiImplicitParam(name = "page", dataType = "Integer", value = "页数"),
            @ApiImplicitParam(name = "rows", dataType = "Integer", value = "每页数量"),
    })
    @ApiOperation("直播间预约人员信息")
    @PostMapping("/query/roomUser")
    public HttpResponseResult<PageInfo<RoomReservationUserVo>> queryRoomUser(@RequestBody @Valid RoomReservationUserSearch query) {
        return succeed(imLiveBroadcastRoomService.queryRoomUser(query));
    }

    @ApiOperation("查询房间信息并校验房间是否合规")
    @GetMapping("/queryRoom")
    public HttpResponseResult<ImLiveBroadcastRoomVo> queryRoomAndCheck(@ApiParam(value = "房间uid", required = true) String roomUid,
                                                                       @ApiParam(value = "用户id", required = true) Integer userId) {
        return succeed(imLiveBroadcastRoomService.queryRoomAndCheck(roomUid, userId, 2));
    }

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

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

    @ApiOperation("开启/关闭直播的录像")
    @GetMapping("/opsLiveVideo")
    public HttpResponseResult<Object> opsLiveVideo(@ApiParam(value = "房间uid", required = true) String roomUid,
                                                   @ApiParam(value = "用户id", required = true) Integer userId,
                                                   @ApiParam(value = "type 1:开始直播-开始录像     2:关闭直播关闭录像", required = true) Integer type,
                                                   @ApiParam(value = "录制视频的尺寸-默认值是720x1280") String videoResolution) {
        if (type == 1) {
            videoResolution = Optional.ofNullable(videoResolution).orElse("720x1280");
            imLiveBroadcastRoomService.startLive(roomUid, userId, videoResolution);
        } else if (type == 2) {
            imLiveBroadcastRoomService.closeLive(roomUid, userId);
        } else {
            failed("type参数错误");
        }
        return succeed();
    }

    @ApiOperation("主讲人进入房间")
    @GetMapping("/speakerJoinRoom")
    public HttpResponseResult<ImLiveBroadcastRoomVo> speakerJoinRoom(String roomUid) {
        return succeed(imLiveBroadcastRoomService.speakerJoinRoom(roomUid,"mobile"));
    }

    @GetMapping("/shareGroup")
    public HttpResponseResult<Object> shareGroup(@ApiParam(value = "房间uid", required = true) String roomUid,
                                                 @ApiParam(value = "群编号", required = true) String groupIds) {
        imLiveBroadcastRoomService.shareGroup(roomUid, groupIds);
        return succeed();
    }

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

    @ApiOperation("查询直播间商品订单列表")
    @GetMapping("/queryLiveRoomGoodsOrderList")
    public HttpResponseResult<PageInfo<LiveRoomGoodsOrderVo>> queryLiveRoomGoodsOrderList(LiveRoomGoodsOrderQueryInfo queryInfo) {
        return succeed(imLiveBroadcastRoomService.queryLiveRoomGoodsOrderList(queryInfo));
    }
}

