package com.yonge.cooleshow.teacher.controller;

import com.alibaba.fastjson.JSONObject;
import com.yonge.cooleshow.biz.dal.entity.ImUserStateSync;
import com.yonge.cooleshow.biz.dal.entity.LiveRoom;
import com.yonge.cooleshow.biz.dal.entity.RoomInfoCache;
import com.yonge.cooleshow.biz.dal.service.LiveRoomService;
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.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

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

/**
 * 直播房间与课程的关系表表(LiveRoom)表控制层
 *
 * @author hgw
 * @since 2022-03-18 15:41:16
 */
@Api(tags = "直播房间与课程的关系表表")
@RestController
@RequestMapping("/liveRoom")
public class TeacherLiveRoomController extends BaseController {
    private final static Logger log = LoggerFactory.getLogger(TeacherLiveRoomController.class);
    /**
     * 服务对象
     */
    @Resource
    private LiveRoomService liveRoomService;

    @ApiImplicitParams({
            @ApiImplicitParam(name = "roomType", dataType = "String", value = "房间类型  LIVE-直播课  TEMP-临时直播间"),
            @ApiImplicitParam(name = "liveState", dataType = "String", value = "直播状态 0未开始 1已开始 2已结束"),
            @ApiImplicitParam(name = "page", dataType = "Integer", value = "页数"),
            @ApiImplicitParam(name = "rows", dataType = "Integer", value = "每页数量"),
    })
    @ApiOperation("分页查询当前用户关联的房间信息")
    @PostMapping("/queryPageRoom")
    public HttpResponseResult<PageInfo<LiveRoom>> queryPageRoom(@RequestBody Map<String, Object> param) {
        return succeed(liveRoomService.queryPageRoom(param));
    }

    /**
     * 根据房间uid查询房间信息
     *
     * @param roomUid 房间uid
     */
    @GetMapping("/speakerCheckRoomInfo")
    public HttpResponseResult<RoomInfoCache> speakerCheckRoomInfo(@RequestParam("roomUid") String roomUid) {
        return succeed(liveRoomService.speakerCheckRoomInfo(roomUid));
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "roomTitle", dataType = "String", value = "房间标题"),
            @ApiImplicitParam(name = "liveRemark", dataType = "String", value = "直播内容/最多200个字"),
            @ApiImplicitParam(name = "liveTime", dataType = "Integer", value = "直播时长"),
            @ApiImplicitParam(name = "coverPic", dataType = "String", value = "封面图"),
    })
    @ApiOperation("创建临时房间-直播间")
    @PostMapping("/createTempLiveRoom")
    public HttpResponseResult<String> createTempLiveRoom(@RequestBody Map<String, Object> param) {
        return succeed(liveRoomService.createTempLiveRoom(param));
    }

    @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(value = "/speakerJoinRoom")
    public HttpResponseResult<RoomInfoCache> speakerJoinRoom(String roomUid) {
        return succeed(liveRoomService.speakerJoinRoom(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("手动关闭直播间")
    @GetMapping("/destroyLiveRoom")
    public HttpResponseResult<Object> destroyLiveRoom(@ApiParam(value = "房间uid", required = true) String roomUid) {
        liveRoomService.destroyLiveRoom(roomUid);
        return succeed();
    }

    /**
     * 同步融云用户状态变更
     *
     * @param userState
     */
    @PostMapping(value = "/syncUserStatus")
    public void statusImUser(@RequestBody List<ImUserStateSync> userState) {
        log.info("opsRoom >>>>> : {}", JSONObject.toJSONString(userState));
        liveRoomService.opsRoom(userState);
    }

    @ApiOperation("方便测试观察房间数据的方法")
    @GetMapping("/test")
    public Object destroyExpiredLiveRoom(@RequestParam("roomUid") String roomUid) {
        return liveRoomService.test(roomUid);
    }

}

