
package com.tmerclub.cloud.live.controller.platform;

import cn.hutool.core.util.PhoneUtil;
import cn.hutool.core.util.StrUtil;
import com.tmerclub.cloud.common.database.dto.PageDTO;
import com.tmerclub.cloud.common.database.vo.PageVO;
import com.tmerclub.cloud.common.exception.LuckException;
import com.tmerclub.cloud.common.response.ServerResponseEntity;
import com.tmerclub.cloud.common.util.BeanUtil;
import com.tmerclub.cloud.live.dto.LiveRoomDTO;
import com.tmerclub.cloud.live.model.LiveRoom;
import com.tmerclub.cloud.live.service.LiveRoomProdService;
import com.tmerclub.cloud.live.service.LiveRoomService;
import com.tmerclub.cloud.live.vo.LiveRoomProdVO;
import com.tmerclub.cloud.live.vo.LiveRoomVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * 直播间
 * @author TRACK
 */
@RestController("platformLiveRoomController")
@RequestMapping("/p/live_room")
@Tag(name = "平台直播间")
public class LiveRoomController {

    @Autowired
    private LiveRoomService liveRoomService;
    @Autowired
    private LiveRoomProdService liveRoomProdService;

    @GetMapping("/page")
    @Operation(summary = "获取直播间列表", description = "分页获取直播间列表")
    public ServerResponseEntity<PageVO<LiveRoomVO>> page(@Valid PageDTO pageDTO, LiveRoomDTO liveRoom) {
        PageVO<LiveRoomVO> liveRoomPage = liveRoomService.page(pageDTO, liveRoom);
        return ServerResponseEntity.success(liveRoomPage);
    }

    @GetMapping
    @Operation(summary = "获取直播间", description = "根据roomId获取直播间")
    public ServerResponseEntity<LiveRoomVO> getByRoomId(@RequestParam Long roomId) {
        LiveRoomVO liveRoomVO = liveRoomService.getByRoomId(roomId);
        if (StrUtil.isNotBlank(liveRoomVO.getUserMobile())) {
            liveRoomVO.setUserMobile(PhoneUtil.hideBetween(liveRoomVO.getUserMobile()).toString());
        }
        return ServerResponseEntity.success(liveRoomVO);
    }

    @GetMapping("/list_live_room_prod")
    @Operation(summary = "获取直播间的直播商品列表")
    public ServerResponseEntity<List<LiveRoomProdVO>> listLiveRoomProd(@RequestParam Long roomId) {
        return ServerResponseEntity.success(liveRoomProdService.listLiveRoomProdInfo(roomId));
    }

    @PutMapping("/to_top")
    @Operation(summary = "置顶直播间", description = "置顶直播间")
    public ServerResponseEntity<Void> update(@RequestParam Long roomId) {
        LiveRoomVO liveRoomVO = liveRoomService.getByRoomId(roomId);
        if (Objects.isNull(liveRoomVO)) {
            throw new LuckException("直播间不存在，请刷新后重试");
        }
        liveRoomVO.setRoomTop(Objects.equals(liveRoomVO.getRoomTop(), 0) ? 1 : 0);
        LiveRoom liveRoom = BeanUtil.map(liveRoomVO, LiveRoom.class);
        liveRoomService.updateById(liveRoom);
        return ServerResponseEntity.success();
    }

    @DeleteMapping
    @Operation(summary = "删除直播间", description = "根据直播间id删除直播间")
    public ServerResponseEntity<Void> delete(@RequestParam Long roomId) {
        liveRoomService.deleteById(roomId);
        return ServerResponseEntity.success();
    }

    @PostMapping("/offline")
    @Operation(summary = "下线直播间")
    public ServerResponseEntity<Void> offline(@RequestBody LiveRoomDTO liveRoomDTO) {
        liveRoomService.offline(liveRoomDTO);
        return ServerResponseEntity.success();
    }
}
