package com.yonge.cooleshow.admin.controller.open;

import com.alibaba.fastjson.JSON;
import com.yonge.cooleshow.api.feign.dto.RoomMember;
import com.yonge.cooleshow.biz.dal.dto.LiveRoomStatus;
import com.yonge.cooleshow.biz.dal.enums.ClientEnum;
import com.yonge.cooleshow.biz.dal.service.ImGroupService;
import com.yonge.cooleshow.biz.dal.service.ImLiveBroadcastRoomMemberService;
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.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("${app-config.url.admin:}/open/live")
@Slf4j
public class OpenLiveController extends BaseController {

    @Autowired
    private LiveRoomService liveRoomService;

    @Autowired
    private ImLiveBroadcastRoomMemberService imLiveBroadcastRoomMemberService;

    @Autowired
    private ImGroupService imGroupService;

    @ApiOperation("查询房间人数")
    @GetMapping("/queryRoomInfo")
    public HttpResponseResult<ImLiveBroadcastRoomMemberWrapper.RoomMemberNumDto> queryRoomInfo(@ApiParam(value = "房间uid", required = true) String roomUid) {
        return succeed(liveRoomService.getRoomMemberNumDto(roomUid));
    }


    @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<List<RoomMember>> queryRoomMember(@RequestBody Map<String, Object> param) {
        param.put("page",1);
        param.put("rows",-1);
        List<ImLiveBroadcastRoomMemberWrapper.ImLiveBroadcastRoomMember> rows = imLiveBroadcastRoomMemberService.queryRoomMember(param).getRows();
        if (CollectionUtils.isNotEmpty(rows)) {
            List<RoomMember> roomMembers = JSON.parseArray(JSON.toJSONString(rows), RoomMember.class);
            for (RoomMember roomMember : roomMembers) {
                roomMember.setImUserId(imGroupService.getImUserId(roomMember.getStudentId(), ClientEnum.STUDENT));
            }
            return succeed(roomMembers);
        }
        return succeed();
    }


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


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