package cn.iocoder.yudao.module.pms.controller.admin.yanfan.media;

import cn.iocoder.yudao.module.pms.controller.admin.yanfan.media.vo.YfMediaServerPageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.yanfan.media.vo.YfMediaServerRespVO;
import cn.iocoder.yudao.module.pms.controller.admin.yanfan.media.vo.YfMediaServerSaveReqVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.yanfan.media.YfMediaServerDO;
import cn.iocoder.yudao.module.pms.service.yanfan.media.YfMediaServerService;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.teautil.Common;
import lombok.extern.java.Log;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;


@Tag(name = "管理后台 - yf流媒体服务器配置")
@RestController
@RequestMapping("/rq/yf-media-server")
@Validated
public class YfMediaServerController {

    @Resource
    private YfMediaServerService yfMediaServerService;

    @PostMapping("/create")
    @Operation(summary = "创建yf流媒体服务器配置")
    @PreAuthorize("@ss.hasPermission('rq:yf-media-server:create')")
    public CommonResult<Integer> createYfMediaServer(@Valid @RequestBody YfMediaServerSaveReqVO createReqVO) {
        return success(yfMediaServerService.createYfMediaServer(createReqVO));
    }

    @Operation(summary = "获取rtp推流端口列表")
    //@PreAuthorize("@ss.hasPermi('iot:video:list')")
    @GetMapping("/listRtpServer")
    public CommonResult<JSONObject> listRtpServer() {
        return success(yfMediaServerService.listRtpServer());
    }


    @PutMapping("/update")
    @Operation(summary = "更新yf流媒体服务器配置")
    @PreAuthorize("@ss.hasPermission('rq:yf-media-server:update')")
    public CommonResult<Boolean> updateYfMediaServer(@Valid @RequestBody YfMediaServerSaveReqVO updateReqVO) {
        yfMediaServerService.updateMediaServer(updateReqVO);
        return success(true);
    }

    /**
     * 获取流媒体服务器配置详细信息,只获取第一条
     */
    @Operation(summary = "获取流媒体服务器配置详细信息")
    //@PreAuthorize("@ss.hasPermi('iot:video:query')")
    @GetMapping()
    public CommonResult<YfMediaServerDO> getInfo() {
        List<YfMediaServerDO> list = yfMediaServerService.selectMediaServer();
        if (list == null || list.size() == 0) {
            YfMediaServerDO mediaServer = new YfMediaServerDO();
            // 设置默认值
            mediaServer.setEnabled(1);
            mediaServer.setDomain("");
            mediaServer.setIp("");
            mediaServer.setPortHttp(8082L);
            mediaServer.setPortHttps(8443L);
            mediaServer.setPortRtmp(1935L);
            mediaServer.setPortRtsp(554L);
            mediaServer.setProtocol("HTTP");
            mediaServer.setSecret("035c73f7-bb6b-4889-a715-d9eb2d192xxx");
            mediaServer.setRtpPortRange("30000,30500");
            list = new ArrayList<>();
            list.add(mediaServer);
        }
        return success(list.get(0));
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除yf流媒体服务器配置")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('rq:yf-media-server:delete')")
    public CommonResult<Boolean> deleteYfMediaServer(@RequestParam("id") Long id) {
        yfMediaServerService.deleteYfMediaServer(id);
        return success(true);
    }

    /**
     * 删除流媒体服务器配置
     */
    @Operation(summary = "删除流媒体服务器配置")
    //@PreAuthorize("@ss.hasPermi('iot:video:remove')")
    @DeleteMapping("/{ids}")
    public CommonResult<Integer> remove(@PathVariable Long[] ids) {
        return success(yfMediaServerService.deleteMediaServerByIds(ids));
    }

    @GetMapping("/get")
    @Operation(summary = "获得yf流媒体服务器配置")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('rq:yf-media-server:query')")
    public CommonResult<YfMediaServerRespVO> getYfMediaServer(@RequestParam("id") Long id) {
        YfMediaServerDO yfMediaServer = yfMediaServerService.getYfMediaServer(id);
        return success(BeanUtils.toBean(yfMediaServer, YfMediaServerRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得yf流媒体服务器配置分页")
    @PreAuthorize("@ss.hasPermission('rq:yf-media-server:query')")
    public CommonResult<PageResult<YfMediaServerRespVO>> getYfMediaServerPage(@Valid YfMediaServerPageReqVO pageReqVO) {
        PageResult<YfMediaServerDO> pageResult = yfMediaServerService.getYfMediaServerPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, YfMediaServerRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出yf流媒体服务器配置 Excel")
    @PreAuthorize("@ss.hasPermission('rq:yf-media-server:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportYfMediaServerExcel(@Valid YfMediaServerPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<YfMediaServerDO> list = yfMediaServerService.getYfMediaServerPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "yf流媒体服务器配置.xls", "数据", YfMediaServerRespVO.class,
                        BeanUtils.toBean(list, YfMediaServerRespVO.class));
    }


    @Operation(summary = "检验流媒体服务")
    //@PreAuthorize("@ss.hasPermi('iot:video:list')")
    @GetMapping(value = "/check")
    public CommonResult<YfMediaServerDO> checkMediaServer(@RequestParam String ip, @RequestParam Long port, @RequestParam String secret) {
        YfMediaServerDO mediaServer = yfMediaServerService.checkMediaServer(ip, port, secret);
        return success(mediaServer);
    }
}
