package com.ym.mec.web.controller;

import com.ym.mec.biz.dal.dto.ImLiveBroadcastRoomDto;
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 com.ym.mec.common.page.WrapperUtil;
import io.swagger.annotations.*;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;

/**
 * 直播房间管理表(ImLiveBroadcastRoom)表控制层
 *
 * @author hgw
 * @since 2022-02-17 20:52:04
 */
@Api(tags = "直播房间管理表")
@RestController
@RequestMapping("/imLiveBroadcastRoom")
public class ImLiveBroadcastRoomController 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 = "page", dataType = "Integer", value = "页数"),
            @ApiImplicitParam(name = "rows", dataType = "Integer", value = "每页数量"),
    })
    @ApiOperation("后台-分页查询直播间列表")
    @PostMapping("/queryPage")
    public HttpResponseResult<PageInfo<ImLiveBroadcastRoomVo>> queryPage(@RequestBody Map<String, Object> param) {
        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));
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "search", dataType = "String", value = "模糊查询关键字"),
            @ApiImplicitParam(name = "subjectId", dataType = "Integer", value = "声部id"),
            @ApiImplicitParam(name = "organId", dataType = "Integer", value = "分部id"),
    })
    @ApiOperation(value = "导出训练营详情")
    @PostMapping(value = "/exportReservationRoomUser")
    public void exportReservationRoomUser(@RequestBody RoomReservationUserSearch query, HttpServletResponse response) throws IOException {
        imLiveBroadcastRoomService.exportReservationRoomUser(query, response);
    }

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

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

    @ApiOperation("创建直播间")
    @PostMapping("/add")
    @PreAuthorize("@pcs.hasPermissions('imLiveBroadcastRoom/add')")
    public HttpResponseResult<Object> add(@Valid @RequestBody ImLiveBroadcastRoomDto dto) {
        imLiveBroadcastRoomService.add(dto);
        //看是否需要马上创建房间
        CompletableFuture.runAsync(imLiveBroadcastRoomService::createLiveRoom);
        return succeed();
    }

    @ApiOperation("修改直播间信息-已开播无法修改")
    @PostMapping("/update")
    public HttpResponseResult<Object> update(@Valid @RequestBody ImLiveBroadcastRoomDto dto) {
        imLiveBroadcastRoomService.update(dto);
        return succeed();
    }

    /**
     * 修改是否禁言
     *
     * @param id          房间表id
     * @param whetherChat 是否允许聊天互动 0允许 1不允许
     */
    @ApiOperation("修改是否禁言")
    @GetMapping(value = "/whetherChat/{id}")
    public HttpResponseResult<Object> whetherChat(@ApiParam(value = "房间表id", required = true) @PathVariable("id") Integer id,
                                                  @ApiParam(value = "是否允许聊天互动 0允许 1不允许", required = true) Integer whetherChat) {
        imLiveBroadcastRoomService.whetherChat(id, whetherChat);
        return succeed();
    }

    /**
     * 关闭直播间
     *
     * @param id 房间表id
     */
    @ApiOperation("关闭直播间")
    @GetMapping(value = "/roomDestroy/{id}")
    public HttpResponseResult<Object> roomDestroy(@ApiParam(value = "房间表id", required = true) @PathVariable("id") Integer id) {
        imLiveBroadcastRoomService.roomDestroy(id);
        return succeed();
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", dataType = "Integer", value = "房间id"),
    })
    @ApiOperation("删除直播间信息-已开播无法删除")
    @PostMapping("/delete")
    @PreAuthorize("@pcs.hasPermissions('imLiveBroadcastRoom/delete')")
    public HttpResponseResult<Object> delete(@RequestBody Map<String, Object> param) {
        Integer id = WrapperUtil.toInt(param, "id", "请传入房间id");
        imLiveBroadcastRoomService.delete(id);
        return succeed();
    }

    @ApiOperation("推广直播间-每个机构只能有一个直播间在首页推广")
    @GetMapping("/opsPopularize")
    @PreAuthorize("@pcs.hasPermissions('imLiveBroadcastRoom/opsPopularize')")
    public HttpResponseResult<Object> opsPopularize(@ApiParam(value = "房间id", required = true) Integer id,
                                                    @ApiParam(value = "是否在首页推广 0否 1是", required = true) Integer popularize) {
        imLiveBroadcastRoomService.opsPopularize(id, popularize);
        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();
    }

    @PostMapping("/quitRoom")
    public HttpResponseResult<Object> quitRoom(@RequestBody List<ImUserState> userState) {
        imLiveBroadcastRoomService.opsRoom(userState);
        return succeed();
    }

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

    @ApiOperation("学生-进入房间")
    @GetMapping("/joinRoom")
    public HttpResponseResult<Object> joinRoom(String roomUid, Integer userId) {
        imLiveBroadcastRoomService.joinRoom(roomUid, userId);
        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) {
        if (type == 1) {
            imLiveBroadcastRoomService.startLive(roomUid, userId, null);
        } else if (type == 2) {
            imLiveBroadcastRoomService.closeLive(roomUid, userId);
        } else {
            failed("type参数错误");
        }
        return succeed();
    }

    @GetMapping("/test")
    public Object test(String roomUid, String userFlag) {
        return imLiveBroadcastRoomService.test(roomUid, userFlag);
    }

    @GetMapping("/destroyExpiredLiveRoom")
    public Object destroyExpiredLiveRoom() {
        imLiveBroadcastRoomService.destroyExpiredLiveRoom();
        return succeed();
    }

    @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("根据传入的用户id查询用户信息")
    @PostMapping("/queryBaseUserInfo")
    public HttpResponseResult<List<BaseRoomUserVo>> queryBaseUserInfo(@RequestBody List<Integer> userIdList) {
        return succeed(imLiveBroadcastRoomService.queryBaseUserInfo(userIdList));
    }

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

