package com.yonge.cooleshow.classroom.controller;


import com.alibaba.fastjson.JSONObject;
import com.yonge.cooleshow.biz.dal.dto.*;
import com.yonge.cooleshow.biz.dal.service.ImNetworkRoomService;
import com.yonge.cooleshow.biz.dal.service.SysUserService;
import com.yonge.cooleshow.common.controller.BaseController;
import com.yonge.cooleshow.common.entity.HttpResponseResult;
import com.yonge.toolset.base.exception.BizException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Optional;

/**
 * 网络教室房间(room)表控制层
 *
 * @author zx
 * @since 2022-03-30 16:36:36
 */
@Api(tags = "网络教室房间")
@RestController
@RequestMapping("/room")
public class ImNetworkRoomController extends BaseController {

    private final static Logger log = LoggerFactory.getLogger(ImNetworkRoomController.class);
    /**
     * 服务对象
     */
    @Resource
    private ImNetworkRoomService imNetworkRoomService;
    @Resource
    private SysUserService sysUserService;

    @ApiOperation("加入网络教室")
    @PostMapping(value = "/join")
    public HttpResponseResult<ImNetworkRoomResult> joinRoom(@RequestBody ImNetworkBaseDto imNetworkBaseDto) throws Exception {
        return imNetworkRoomService.joinRoom(Optional.ofNullable(imNetworkBaseDto)
                .map(ImNetworkBaseDto::getRoomId)
                .orElseThrow(()->new BizException("房间号不可为空")));
    }

//    @ApiOperation("加入网络教室状态回调")
//    @PostMapping(value = "joinRoomSuccess", consumes = MediaType.MULTIPART_FORM_DATA_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
//    public HttpResponseResult joinRoomSuccess(Long courseScheduleId) throws Exception {
//        imNetworkRoomService.joinRoomSuccess(courseScheduleId.toString(),UserRoleEnum.TEACHER);
//        return succeed();
//    }

    @ApiOperation("加入网络教室状态回调")
    @PostMapping(value = "joinRoomFailure")
    public HttpResponseResult joinRoomFailure(@RequestBody ImNetworkBaseDto imNetworkBaseDto){
        imNetworkRoomService.joinRoomFailure(Optional.ofNullable(imNetworkBaseDto)
                .map(ImNetworkBaseDto::getRoomId)
                .orElseThrow(()->new BizException("房间号不可为空")).toString());
        return succeed();
    }

    @PostMapping(value = "/statusSync")
    @ApiOperation("加入网络教室状态回调")
    public void statusSync(@RequestBody String body) throws Exception {
        log.info("statusSync body: {}",body);
        ImChannelStateNotify notify = JSONObject.parseObject(body, ImChannelStateNotify.class);
        switch (notify.getEvent()) {
            case 11:
                //成员加入
                imNetworkRoomService.joinRoomSuccess(notify.getRoomId(),Long.parseLong(notify.getUserId()));
                break;
            case 12:
                //成员退出
                imNetworkRoomService.quitRoomSuccess(notify.getRoomId(),Long.parseLong(notify.getUserId()));
                break;
        }
    }

    @ApiOperation("退出网络教室")
    @PostMapping(value = "/leave")
    public HttpResponseResult leaveRoom(@RequestBody ImNetworkBaseDto imNetworkBaseDto) throws Exception {
        imNetworkRoomService.quitRoomSuccess(Optional.ofNullable(imNetworkBaseDto)
                .map(ImNetworkBaseDto::getRoomId)
                .orElseThrow(()->new BizException("房间号不可为空")).toString(),sysUserService.getUserId());
        return succeed();
    }

    @ApiOperation("控制学员节拍器")
    @PostMapping(value = "/sendImPlayMidiMessage")
    public HttpResponseResult sendImPlayMidiMessage(@RequestBody ImNetworkCustomMessage customMessage) throws Exception {
        imNetworkRoomService.sendImPlayMidiMessage(customMessage);
        return succeed();
    }

    @ApiOperation("移动端用来渲染页面")
    @PostMapping(value = "/display")
    public HttpResponseResult display(@RequestBody ImNetworkDisplayDataDto displayData) throws Exception {
        imNetworkRoomService.display(displayData);
        return succeed();
    }

    @ApiOperation(value = "批量控制学员设备开关")
    @PostMapping(value = "/device/batchControl")
    public Object batchControlDevice(@RequestBody ImNetworkDeviceControlDto deviceControl)throws Exception {
        imNetworkRoomService.batchControlDevice(deviceControl);
        return succeed();
    }

    @ApiOperation(value = "控制学员设备开关")
    @PostMapping(value = "/device/control")
    public HttpResponseResult controlDevice(@RequestBody ImNetworkDeviceControlDto deviceControl)
            throws Exception {
        imNetworkRoomService.controlDevice(deviceControl);
        return succeed();
    }

    @ApiOperation(value = "学员同意打开，麦克风、摄像头")
    @PostMapping(value = "/device/approve")
    public HttpResponseResult approveControlDevice(@RequestBody ImNetworkDeviceControlDto deviceControl) throws Exception {
        deviceControl.setEnable(true);
        imNetworkRoomService.approveControlDevice(deviceControl);
        return succeed();
    }

    @ApiOperation(value = "学员拒绝打开，麦克风、摄像头")
    @PostMapping(value = "/device/reject")
    public HttpResponseResult rejectControlDevice(@RequestBody ImNetworkDeviceControlDto deviceControl)
            throws Exception {
        deviceControl.setEnable(false);
        imNetworkRoomService.rejectControlDevice(deviceControl);
        return succeed();
    }

    @ApiOperation(value = "学员设备状态同步")
    @PostMapping(value = "/device/sync")
    public Object deviceStatusSync(@RequestBody ImNetworkDeviceControlDto deviceControl)
            throws Exception {
        imNetworkRoomService.deviceStatusSync(deviceControl);
        return succeed();
    }

    @ApiOperation(value = "老师在网络教室选择完伴奏后、通知学员下载伴奏")
    @PostMapping(value = "pushDownloadMusicSheetMsg")
    public HttpResponseResult pushDownloadMusicSheetMsg(@RequestBody ImNetworkMusicSheetDto musicSheetDto) throws Exception {
        imNetworkRoomService.pushDownloadMusicSheetMsg(musicSheetDto);
        return succeed();
    }

    @ApiOperation(value = "学员伴奏下载状态回调")
    @PostMapping(value = "musicSheetDownNotify")
    public HttpResponseResult adjustMusicScore(@RequestBody ImNetworkMusicSheetDto musicSheetDto) throws Exception {
        imNetworkRoomService.musicSheetDownNotify(musicSheetDto);
        return succeed();
    }

    @ApiOperation(value = "查询不在教室的学员")
    @PostMapping(value = "/queryNoJoinStu")
    public HttpResponseResult queryNoJoinStu(@RequestBody ImNetworkBaseDto imNetworkBaseDto){
        return succeed(imNetworkRoomService.queryNoJoinStu(Optional.ofNullable(imNetworkBaseDto)
                .map(ImNetworkBaseDto::getRoomId)
                .orElseThrow(()->new BizException("房间号不可为空"))));
    }
}

