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

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.pms.controller.admin.yanfan.enums.Direct;
import cn.iocoder.yudao.module.pms.controller.admin.yanfan.utils.MessageUtils;
import cn.iocoder.yudao.module.pms.service.yanfan.play.model.PtzDirectionInput;
import cn.iocoder.yudao.module.pms.service.yanfan.play.model.PtzscaleInput;
import cn.iocoder.yudao.module.pms.service.yanfan.ptz.IPtzCmdService;
import io.swagger.v3.oas.annotations.Operation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.TimerTask;
import java.util.concurrent.CompletableFuture;

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

@Slf4j
@RestController
@RequestMapping("/sip/ptz")
public class PtzController {
    @Autowired
    private IPtzCmdService ptzCmdService;

    @Operation(summary="云台方向控制")
    //@PreAuthorize("@ss.hasPermi('iot:video:list')")
    @PostMapping("/direction/{deviceId}/{channelId}")
    public CommonResult<Boolean> direction(@PathVariable String deviceId, @PathVariable String channelId, @RequestBody PtzDirectionInput ptzDirectionInput) {
        Direct direct = null;
        int leftRight = ptzDirectionInput.getLeftRight();
        int upDown = ptzDirectionInput.getUpDown();
        if (leftRight == 1) {
            direct = Direct.RIGHT;
        } else if (leftRight == 2) {
            direct = Direct.LEFT;
        } else if (upDown == 1) {
            direct = Direct.UP;
        } else if (upDown == 2) {
            direct = Direct.DOWN;
        } else {
            direct = Direct.STOP;
        }
        Integer speed = ptzDirectionInput.getMoveSpeed();
        return success(ptzCmdService.directPtzCmd(deviceId, channelId, direct, speed));
    }

    @Operation(summary="云台缩放控制")
    //@PreAuthorize("@ss.hasPermi('iot:video:list')")
    @PostMapping("/scale/{deviceId}/{channelId}")
    public CommonResult<Boolean> scale(@PathVariable String deviceId, @PathVariable String channelId, @RequestBody PtzscaleInput ptzscaleInput) {
        Direct direct = null;
        if (ptzscaleInput.getInOut() == 1) {
            direct = Direct.ZOOM_IN;
        } else if (ptzscaleInput.getInOut() == 2) {
            direct = Direct.ZOOM_OUT;
        } else if (ptzscaleInput.getIrisInOut() == 1) {
            direct = Direct.IRIS_IN;
        } else if (ptzscaleInput.getIrisInOut() == 2) {
            direct = Direct.IRIS_OUT;
        } else if (ptzscaleInput.getFocusInOut() == 1) {
            direct = Direct.FOCUS_IN;
        } else if (ptzscaleInput.getFocusInOut() == 2) {
            direct = Direct.FOCUS_OUT;
        } else {
            direct = Direct.STOP;
        }
        Integer speed = ptzscaleInput.getScaleSpeed();
        boolean result = ptzCmdService.directPtzCmd(deviceId, channelId, direct, speed);
        if (result) {
//            AsyncManager.me().executeDelay(new TimerTask() {
//                @Override
//                public void run() {
//                    ptzCmdService.directPtzCmd(deviceId, channelId, Direct.STOP, speed);
//                }
//            }, 500);
            CompletableFuture.runAsync(()->{
               ptzCmdService.directPtzCmd(deviceId, channelId,Direct.STOP,speed);
            });
        }
       return success(result);
    }

    @Operation(summary="云台ptz控制")
    @PostMapping("/ptz/{deviceId}/{channelId}/{direct}/{speed}")
    //@PreAuthorize("@ss.hasPermi('iot:video:list')")
    public CommonResult<Boolean> ptzControl(@PathVariable String deviceId, @PathVariable String channelId, @PathVariable Direct direct, @PathVariable Integer speed) {
        return success(ptzCmdService.directPtzCmd(deviceId, channelId, direct, speed));
    }

    @Operation(summary="云台停止控制")
    @PostMapping("/ptz/{deviceId}/{channelId}/STOP")
    //@PreAuthorize("@ss.hasPermi('iot:video:list')")
    public CommonResult<Boolean> ptzControlStop(@PathVariable String deviceId, @PathVariable String channelId) {
        return success(ptzCmdService.directPtzCmd(deviceId, channelId, Direct.STOP, 0));
    }

}
