package com.multictrl.modules.business.controller;

import com.multictrl.common.annotation.ApiOrder;
import com.multictrl.common.annotation.LogOperation;
import com.multictrl.common.exception.ErrorCode;
import com.multictrl.common.utils.Result;
import com.multictrl.modules.business.dto.live.LiveUrl;
import com.multictrl.modules.business.service.LiveService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 直播管理
 *
 * @author Sdy
 * @since 1.0.0 2026/4/24
 */
@RestController
@RequestMapping("business/live")
@Tag(name = "直播管理")
@ApiOrder(4)
@RequiredArgsConstructor
public class LiveController {
    private final LiveService liveService;

    @LogOperation("开启直播")
    @PostMapping("/startLive/{dockSn}")
    @Operation(summary = "开启直播")
    @RequiresPermissions("bus:live:start")
    public Result<Object> startLive(@PathVariable String dockSn,
                                    @Parameter(description = "直播质量：0-自适应，1-流畅，2-标清，3-高清，4-超清")
                                    @RequestParam Integer videoQuality,
                                    @Parameter(description = "直播类型：dock-机场，uav-飞机")
                                    @RequestParam String videoType) {
        if (videoQuality != 0 && videoQuality != 1 && videoQuality != 2 && videoQuality != 3 && videoQuality != 4) {
            return new Result<>().error(ErrorCode.PARAMS_ERROR);
        }
        if (!videoType.equals("dock") && !videoType.equals("uav")) {
            return new Result<>().error(ErrorCode.PARAMS_ERROR);
        }

        return new Result<>().ok(liveService.startLive(dockSn, videoQuality, videoType));
    }

    @LogOperation("停止直播")
    @PostMapping("/stopLive/{dockSn}")
    @Operation(summary = "停止直播")
    @RequiresPermissions("bus:live:stop")
    public Result<Object> stopLive(@PathVariable String dockSn,
                                   @Parameter(description = "直播类型：dock-机场，uav-飞机")
                                   @RequestParam String videoType) {
        if (!videoType.equals("dock") && !videoType.equals("uav")) {
            return new Result<>().error(ErrorCode.PARAMS_ERROR);
        }

        return new Result<>().ok(liveService.stopLive(dockSn, videoType));
    }

    @LogOperation("机场直播流切换")
    @PostMapping("/liveCameraChange/{dockSn}")
    @Operation(summary = "机场直播流切换")
    @RequiresPermissions("bus:dock:live:change")
    public Result<Object> liveCameraChange(@PathVariable String dockSn,
                                           @Parameter(description = "直播流类型：0-舱内，1-舱外")
                                           @RequestParam Integer cameraPosition) {
        if (cameraPosition != 0 && cameraPosition != 1) {
            return new Result<>().error(ErrorCode.PARAMS_ERROR);
        }

        return new Result<>().ok(liveService.liveCameraChange(dockSn, cameraPosition));
    }

    @LogOperation("无人机直播镜头切换")
    @PostMapping("/liveLensChange/{dockSn}")
    @Operation(summary = "无人机直播镜头切换")
    @RequiresPermissions("bus:uav:live:change")
    public Result<Object> liveLensChange(@PathVariable String dockSn,
                                         @Parameter(description = "镜头类型：ir-红外，normal-默认，wide-广角，zoom-变焦")
                                         @RequestParam String videoType) {
        if (!videoType.equals("ir") && !videoType.equals("normal") && !videoType.equals("wide") && !videoType.equals("zoom")) {
            return new Result<>().error(ErrorCode.PARAMS_ERROR);
        }

        return new Result<>().ok(liveService.liveLensChange(dockSn, videoType));
    }

    @LogOperation("直播清晰度设置")
    @PostMapping("/liveSetQuality/{dockSn}")
    @Operation(summary = "直播清晰度设置")
    @RequiresPermissions("bus:live:set:quality")
    public Result<Object> liveSetQuality(@PathVariable String dockSn,
                                         @Parameter(description = "直播清晰度：0-自适应，1-流畅，2-标清，3-高清，4-超清")
                                         @RequestParam Integer videoQuality,
                                         @Parameter(description = "直播类型：dock-机场，uav-飞机")
                                         @RequestParam String videoType) {
        if (videoQuality != 0 && videoQuality != 1 && videoQuality != 2 && videoQuality != 3 && videoQuality != 4) {
            return new Result<>().error(ErrorCode.PARAMS_ERROR);
        }
        if (!videoType.equals("dock") && !videoType.equals("uav")) {
            return new Result<>().error(ErrorCode.PARAMS_ERROR);
        }

        return new Result<>().ok(liveService.liveSetQuality(dockSn, videoQuality, videoType));
    }

    @LogOperation("获取直播地址信息")
    @GetMapping("/getLiveUrl/{dockSn}")
    @Operation(summary = "获取直播地址信息")
    @RequiresPermissions("bus:live:url:get")
    public Result<List<LiveUrl>> getLiveUrl(@PathVariable String dockSn) {

        return new Result<List<LiveUrl>>().ok(liveService.getLiveUrl(dockSn));
    }
}
