package com.njcn.gather.system.resource.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.njcn.common.pojo.annotation.OperateInfo;
import com.njcn.common.pojo.constant.OperateType;
import com.njcn.common.pojo.enums.common.LogEnum;
import com.njcn.common.pojo.enums.response.CommonResponseEnum;
import com.njcn.common.pojo.response.HttpResult;
import com.njcn.common.utils.LogUtil;
import com.njcn.gather.system.resource.pojo.param.ResourceManageParam;
import com.njcn.gather.system.resource.pojo.vo.PlayVO;
import com.njcn.gather.system.resource.pojo.vo.ResourceManageVO;
import com.njcn.gather.system.resource.service.IResourceManageService;
import com.njcn.web.controller.BaseController;
import com.njcn.web.utils.HttpResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;

/**
 * 资源管理
 */
@Slf4j
@Api(tags = "资源管理")
@RestController
@RequestMapping("/resourceManage")
@RequiredArgsConstructor
public class ResourceManageController extends BaseController {

    private final IResourceManageService resourceManageService;

    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
    @PostMapping("/list")
    @ApiOperation("分页查询资源列表")
    @ApiImplicitParam(name = "queryParam", value = "查询参数", required = true)
    public HttpResult<Page<ResourceManageVO>> list(@RequestBody @Validated ResourceManageParam.QueryParam queryParam) {
        String methodDescribe = getMethodDescribe("list");
        LogUtil.njcnDebug(log, "{}，查询参数为：{}", methodDescribe, queryParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, resourceManageService.list(queryParam), methodDescribe);
    }

    @OperateInfo(info = LogEnum.SYSTEM_COMMON, operateType = OperateType.ADD)
    @PostMapping("/add")
    @ApiOperation("新增视频资源")
    @ApiImplicitParam(name = "resourceManageParam", value = "资源参数", required = true)
    public HttpResult<Boolean> add(ResourceManageParam resourceManageParam) {
        String methodDescribe = getMethodDescribe("add");
        LogUtil.njcnDebug(log, "{}，新增资源参数为：{}", methodDescribe, resourceManageParam);
        boolean result = resourceManageService.add(resourceManageParam);
        if (result) {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, true, methodDescribe);
        }
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, false, methodDescribe);
    }

    @OperateInfo(info = LogEnum.SYSTEM_COMMON, operateType = OperateType.UPDATE)
    @PostMapping("/update")
    @ApiOperation("编辑资源信息")
    @ApiImplicitParam(name = "updateParam", value = "资源参数", required = true)
    public HttpResult<Boolean> update(@RequestBody @Validated ResourceManageParam.UpdateParam updateParam) {
        String methodDescribe = getMethodDescribe("update");
        LogUtil.njcnDebug(log, "{}，编辑资源参数为：{}", methodDescribe, updateParam);
        boolean result = resourceManageService.update(updateParam);
        if (result) {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, true, methodDescribe);
        }
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, false, methodDescribe);
    }

    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
    @GetMapping("/play")
    @ApiOperation("获取视频播放地址")
    @ApiImplicitParam(name = "id", value = "资源id", required = true)
    public HttpResult<PlayVO> play(@RequestParam("id") String id) {
        String methodDescribe = getMethodDescribe("play");
        LogUtil.njcnDebug(log, "{}，资源id为：{}", methodDescribe, id);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, resourceManageService.play(id), methodDescribe);
    }

    @GetMapping("/stream")
    @ApiOperation("播放视频流")
    public void stream(@RequestParam("id") String id,
                       @RequestParam("token") String token,
                       @RequestHeader(value = "Range", required = false) String rangeHeader,
                       HttpServletResponse response) {
        resourceManageService.stream(id, token, rangeHeader, response);
    }
}
