package cn.iocoder.yudao.module.product.controller.admin.deviceinfo;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.product.controller.admin.deviceinfo.vo.*;
import cn.iocoder.yudao.module.product.service.deviceinfo.DeviceInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.annotation.security.PermitAll;
import javax.validation.Valid;

import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Api(tags = "管理后台 - 设备信息")
@RestController
@RequestMapping("/product/device-info")
@Validated
public class DeviceInfoController {

    @Resource
    private DeviceInfoService deviceInfoService;

    @PostMapping("/create")
    @ApiOperation("创建设备信息")
    @PreAuthorize("@ss.hasPermission('product:device-info:create')")
    public CommonResult<Long> createDeviceInfo(@Valid @RequestBody DeviceInfoCreateReqVO createReqVO) {
        return success(deviceInfoService.createDeviceInfo(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新设备信息")
    @PreAuthorize("@ss.hasPermission('product:device-info:update')")
    public CommonResult<Boolean> updateDeviceInfo(@Valid @RequestBody DeviceInfoUpdateReqVO updateReqVO) {
        deviceInfoService.updateDeviceInfo(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除设备信息")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('product:device-info:delete')")
    public CommonResult<Boolean> deleteDeviceInfo(@RequestParam("id") Long id) {
        deviceInfoService.deleteDeviceInfo(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得设备信息")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('product:device-info:query')")
    public CommonResult<DeviceInfoRespVO> getDeviceInfo(@RequestParam("id") Long id) {
        return success(deviceInfoService.getDeviceInfo(id));
    }

    @GetMapping("/page")
    @ApiOperation("获得设备信息分页")
    @PreAuthorize("@ss.hasPermission('product:device-info:query')")
    public CommonResult<PageResult<DeviceInfoRespVO>> getDeviceInfoPage(@Valid DeviceInfoPageReqVO pageVO) {
        return success(deviceInfoService.getDeviceInfoPage(pageVO));
    }

    @PutMapping("/changeSwitch")
    @ApiOperation("更改开关状态")
    @ApiImplicitParam(name = "switchOn", value = "是否打开开关", required = true, example = "false", dataTypeClass = Boolean.class)
    @PreAuthorize("@ss.hasPermission('product:device-info:update')")
    public CommonResult<Boolean> changeSwitch(@RequestParam("id") Long id, @RequestParam("switchOn") Boolean switchOn) {
        deviceInfoService.changeSwitch(id, switchOn);
        return success(true);
    }

    @PutMapping("/changeEnabled")
    @ApiOperation("更改是否启用")
    @ApiImplicitParam(name = "enabled", value = "是否启用", required = true, example = "false", dataTypeClass = Boolean.class)
    @PreAuthorize("@ss.hasPermission('product:device-info:update')")
    public CommonResult<Boolean> changeEnabled(@RequestParam("id") Long id, @RequestParam("enabled") Boolean enabled) {
        deviceInfoService.changeEnabled(id, enabled);
        return success(true);
    }

    @GetMapping("/getMonitorPreviewURL")
    @ApiOperation("获取监控播放地址")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "monitorCode", value = "监控点编码", required = true, example = "半球机2-bc9b5e98c90a", dataTypeClass = String.class),
            @ApiImplicitParam(name = "type", value = "url前缀类型", required = true, example = "wss", dataTypeClass = String.class)
    })
    @PreAuthorize("@ss.hasPermission('product:device-info:query')")
    public CommonResult<MonitorUrLRespVO> getMonitorPreviewURL(@RequestParam("monitorCode") String monitorCode, @RequestParam("type") String type) {
        MonitorUrLRespVO monitorPreviewURL = deviceInfoService.getMonitorPreviewURL(monitorCode, type);
        return success(monitorPreviewURL);
    }

    @GetMapping("/getMonitorPreviewURLByLandId")
    @ApiOperation("获取监控播放地址")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "landId", value = "地块/鱼塘/牧场编码", required = true, example = "AECD202305041501UE6F", dataTypeClass = String.class),
            @ApiImplicitParam(name = "type", value = "url前缀类型", required = true, example = "wss", dataTypeClass = String.class)
    })
    @PreAuthorize("@ss.hasPermission('product:device-info:query')")
    public CommonResult<List<MonitorUrLRespVO>> getMonitorPreviewURLByLandId(@RequestParam("landId") String landId, @RequestParam("type") String type) {
        return CommonResult.success(deviceInfoService.getMonitorPreviewURLByLandId(landId, type));
    }
}
