package com.sptg.gateway.controller.videoCloud;

import com.baomidou.mybatisplus.plugins.Page;
import com.sptg.common.core.beans.PageBean;
import com.sptg.common.core.beans.ResultBean;
import com.sptg.common.core.beans.ResultEnum;
import com.sptg.common.security.annotation.Security;
import com.sptg.common.security.enums.DataFormatEnum;
import com.sptg.common.security.service.SecurityServiceInterface;
import com.sptg.permissions.api.entity.Account;
import com.sptg.video.cloud.api.constant.VideoCloudSecurityToken;
import com.sptg.video.cloud.api.dto.VCDeviceDto;
import com.sptg.video.cloud.api.entity.VCDevice;
import com.sptg.video.cloud.api.feigns.VCDeviceFeign;
import freemarker.template.utility.StringUtil;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

/**
 * @author zwx
 * @create 2020/10/18 23:29
 * 视频云设备控制器
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping(value = "/videoCloud/device")
public class VCDeviceController {

    private final SecurityServiceInterface<Account> securityServiceInterface;
    private final VCDeviceFeign vcDeviceFeign;

    /**
     * 功能描述: 视频云设备界面
     * @return ModelAndView
     */
    @GetMapping(value = "/manage/index")
    @Security(token = VideoCloudSecurityToken.VIDEO_CLOUD_DEVICE_INFO_MANAGE, format = DataFormatEnum.VIEW)
    public ModelAndView deviceManage(){
        return new ModelAndView("videoCloud/vc-device-manage");
    }

    /**
     * 功能描述: 视频云批量获取设备rtspUrl界面
     * @return ModelAndView
     */
    @GetMapping(value = "/manage/syncDeviceRtspUrlAll")
    @Security(token = VideoCloudSecurityToken.VIDEO_CLOUD_DEVICE_SYNC_RTSPURL_ALL, format = DataFormatEnum.VIEW)
    public ModelAndView syncDeviceRtspUrlBatchManage(){
        return new ModelAndView("videoCloud/vc-device-sync-rtspurl-all");
    }

    /**
     * 功能描述: 视频云设备详情界面
     * @return ModelAndView
     */
    @GetMapping(value = "/detail/{id}")
    @Security(token = VideoCloudSecurityToken.VIDEO_CLOUD_DEVICE_INFO_MANAGE, format = DataFormatEnum.VIEW)
    public ModelAndView detail(@PathVariable(value = "id") Long id){
        VCDevice vcDevice = this.vcDeviceFeign.findDeviceById(id);
        if (vcDevice == null){
            return new ModelAndView("common/404");
        }
        ModelAndView modelAndView = new ModelAndView("videoCloud/vc-device-detail");
        modelAndView.addObject("vdDevice",vcDevice);
        return modelAndView;
    }

    /**
     * 功能描述: 视频云设备分页查询设备列表df
     *
     * @param deviceDto      查询条件
     * @param pageBean 分页条件
     * @return ResultBean<Page>
     */
    @GetMapping(value = "/pagingQuery")
    @Security(token = VideoCloudSecurityToken.VIDEO_CLOUD_DEVICE_INFO_MANAGE, format = DataFormatEnum.JSON)
    public ResultBean<Page<VCDevice>> pagingQuery(VCDeviceDto deviceDto, PageBean<VCDevice, VCDeviceDto> pageBean) {
        pageBean.setDto(deviceDto);
        return this.vcDeviceFeign.pagingQuery(pageBean);
    }

    /**
     * 功能描述: 视频云设备同步
     *
     * @param deviceDto
     * @return ResultBean<Page>
     */
    @PostMapping(value = "/sync/list")
    @Security(token = VideoCloudSecurityToken.VIDEO_CLOUD_DEVICE_LIST_SYNC, format = DataFormatEnum.JSON)
    public ResultBean<String> syncDeviceList(VCDeviceDto deviceDto) {
        return this.vcDeviceFeign.syncDeviceList(deviceDto);
    }

    /**
     * 功能描述: 视频云获取设备rtsp播放地址
     *
     * @param deviceDto
     * @return ResultBean<Page>
     */
    @PostMapping(value = "/sync/rtspUrl")
    @Security(token = VideoCloudSecurityToken.VIDEO_CLOUD_DEVICE_INFO_MANAGE, format = DataFormatEnum.JSON)
    public ResultBean<String> syncDeviceRtspUrl(VCDeviceDto deviceDto) {
        return this.vcDeviceFeign.deviceGetDevUrl(deviceDto);
    }

    /**
     * 功能描述: 视频云获取设备rtsp播放地址
     *
     * @param deviceDto
     * @return ResultBean<Page>
     */
    @PostMapping(value = "/sync/rtspUrl/all")
    @Security(token = VideoCloudSecurityToken.VIDEO_CLOUD_DEVICE_INFO_MANAGE, format = DataFormatEnum.JSON)
    public ResultBean<Boolean> syncDeviceRtspUrlBatch(VCDeviceDto deviceDto) {
        return this.vcDeviceFeign.syncDeviceDevUrlList(deviceDto);
    }

    /**
     * 功能描述: 推流
     *
     * @param vcDeviceDto
     * @return ResultBean<Page>
     */
    @PostMapping(value = "/pusher")
    @Security(token = VideoCloudSecurityToken.VIDEO_CLOUD_DEVICE_INFO_MANAGE, format = DataFormatEnum.JSON)
    public ResultBean<String> devicePusher(VCDeviceDto vcDeviceDto) {
        log.error("admin:id---" + vcDeviceDto.getDeviceNum());
        log.error("admin:rtspUrl---" + vcDeviceDto.getRtspUrl());
        return this.vcDeviceFeign.devicePusher(vcDeviceDto);
    }

    /**
     * 功能描述: 推流查询
     *
     * @param deviceDto
     * @return ResultBean<Page>
     */
    @PostMapping(value = "/pusherStatus")
    @Security(token = VideoCloudSecurityToken.VIDEO_CLOUD_DEVICE_INFO_MANAGE, format = DataFormatEnum.JSON)
    public ResultBean<String> devicePusherStatus(VCDeviceDto deviceDto) {
        return this.vcDeviceFeign.devicePusherStatus(deviceDto);
    }

    /**
     * 功能描述: 推流停止
     *
     * @param deviceDto
     * @return ResultBean<Page>
     */
    @PostMapping(value = "/pusherDelete")
    @Security(token = VideoCloudSecurityToken.VIDEO_CLOUD_DEVICE_INFO_MANAGE, format = DataFormatEnum.JSON)
    public ResultBean<String> devicePusherDelete(VCDeviceDto deviceDto) {
        return this.vcDeviceFeign.devicePusherDelete(deviceDto);
    }

    /**
     * 功能描述: zlm流在线查询
     *
     * @param deviceDto
     * @return ResultBean<Page>
     */
    @PostMapping(value = "/zlmStreamOnline")
    @Security(token = VideoCloudSecurityToken.VIDEO_CLOUD_DEVICE_INFO_MANAGE, format = DataFormatEnum.JSON)
    public ResultBean<String> zmlStreamOnline(VCDeviceDto deviceDto) {
        return this.vcDeviceFeign.mediaServerStreamOnline(deviceDto);
    }

    /**
     * 功能描述: zlm推流
     *
     * @param deviceDto
     * @return ResultBean<Page>
     */
    @PostMapping(value = "/zlmStreamPush")
    @Security(token = VideoCloudSecurityToken.VIDEO_CLOUD_DEVICE_INFO_MANAGE, format = DataFormatEnum.JSON)
    public ResultBean<String> zmlStreamPush(VCDeviceDto deviceDto) {
        return this.vcDeviceFeign.msAddFFmpegSource(deviceDto);
    }

    /**
     * 功能描述: zlm关流
     *
     * @param deviceDto
     * @return ResultBean<Page>
     */
    @PostMapping(value = "/zlmStreamClose")
    @Security(token = VideoCloudSecurityToken.VIDEO_CLOUD_DEVICE_INFO_MANAGE, format = DataFormatEnum.JSON)
    public ResultBean<String> zmlStreamClose(VCDeviceDto deviceDto) {
        return this.vcDeviceFeign.msDelFFmpegSource(deviceDto);
    }

    /**
     * 功能描述: 视频云设备同步
     *
     * @param deviceDto
     * @return ResultBean<Page>
     */
    @PostMapping(value = "/play/video")
    @Security(token = VideoCloudSecurityToken.VIDEO_CLOUD_DEVICE_INFO_MANAGE, format = DataFormatEnum.JSON)
    public ResultBean<String> playVideo(VCDeviceDto deviceDto) {
        return this.vcDeviceFeign.deviceGetHttpFlvUrl(deviceDto);
    }
}
