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.flight.FlightExecute;
import com.multictrl.modules.business.service.DJIBaseService;
import com.multictrl.modules.business.service.RouteFlightService;
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/27
 */
@RestController
@RequestMapping("business/command")
@Tag(name = "航线飞行", description = """
        航线管理是无人机自主作业的重要功能，
        可以实现行业领域的批量化、智能化作业。
        上云 API 提供了相关的接口，实现了航线任务在云端的共享查看、下发执行、取消以及进度上报等功能。
        用户需要遵照航线文件格式规范（WPML）编写航线文件，定义航线任务。一个航线任务中可以定义多条航线。""")
@ApiOrder(5)
@RequiredArgsConstructor
public class RouteFlightController {
    private final DJIBaseService djiBaseService;
    private final RouteFlightService routeFlightService;

    @LogOperation("一键返航")
    @PostMapping("/returnHome/{dockSn}")
    @Operation(summary = "一键返航")
    @RequiresPermissions("bus:route:flight:returnHome")
    public Result<Object> returnHome(@PathVariable String dockSn) {

        return new Result<>().ok(djiBaseService.executeAndReturnResult(dockSn, "return_home"));
    }

    @LogOperation("取消返航")
    @PostMapping("/returnHomeCancel/{dockSn}")
    @Operation(summary = "取消返航", description = "返航后，飞行器会退出航线模式，此时取消返航，飞行器会悬停")
    @RequiresPermissions("bus:route:flight:returnHomeCancel")
    public Result<Object> returnHomeCancel(@PathVariable String dockSn) {

        return new Result<>().ok(djiBaseService.executeAndReturnResult(dockSn, "return_home_cancel"));
    }

    @LogOperation("航线恢复")
    @PostMapping("/flightTaskRecovery/{dockSn}")
    @Operation(summary = "航线恢复")
    @RequiresPermissions("bus:route:flight:flightTaskRecovery")
    public Result<Object> flightTaskRecovery(@PathVariable String dockSn) {

        return new Result<>().ok(djiBaseService.executeAndReturnResult(dockSn, "flighttask_recovery"));
    }

    @LogOperation("航线暂停")
    @PostMapping("/flightTaskPause/{dockSn}")
    @Operation(summary = "航线暂停")
    @RequiresPermissions("bus:route:flight:flightTaskPause")
    public Result<Object> flightTaskPause(@PathVariable String dockSn) {

        return new Result<>().ok(djiBaseService.executeAndReturnResult(dockSn, "flighttask_pause"));
    }

    @LogOperation("取消任务")
    @PostMapping("/flightTaskUndo/{dockSn}")
    @Operation(summary = "取消任务")
    @RequiresPermissions("bus:route:flight:flightTaskUndo")
    public Result<Object> flightTaskUndo(@PathVariable String dockSn,
                                         @RequestBody List<String> taskIds) {
        if (taskIds.isEmpty()) {
            return new Result<>().error(ErrorCode.PARAMS_ERROR);
        }

        return new Result<>().ok(routeFlightService.flightTaskUndo(dockSn, taskIds));
    }

    @LogOperation("执行航线")
    @PostMapping("/flightExecute/{dockSn}")
    @Operation(summary = "执行航线")
    @RequiresPermissions("bus:route:flight:flightExecute")
    public Result<Object> flightExecute(@PathVariable String dockSn,
                                        @RequestBody FlightExecute flightExecute) {

        return new Result<>().ok(routeFlightService.flightExecute(dockSn, flightExecute, true));
    }

    @LogOperation("空中下发航线")
    @PostMapping("/inFlightWaylineDeliver/{dockSn}")
    @Operation(summary = "空中下发航线")
    @RequiresPermissions("bus:route:flight:inFlightWaylineDeliver")
    public Result<Object> inFlightWaylineDeliver(@PathVariable String dockSn,
                                                 @Parameter(name = "routeId", description = "航线标识")
                                                 @RequestParam Long routeId) {

        return new Result<>().ok(routeFlightService.inFlightWaylineDeliver(dockSn, routeId));
    }

    @LogOperation("暂停空中航线")
    @PostMapping("/inFlightWaylineStop/{dockSn}")
    @Operation(summary = "暂停空中航线")
    @RequiresPermissions("bus:route:flight:inFlightWaylineStop")
    public Result<Object> inFlightWaylineStop(@PathVariable String dockSn) {

        return new Result<>().ok(routeFlightService.inFlightWaylineStop(dockSn));
    }

    @LogOperation("恢复空中航线")
    @PostMapping("/inFlightWaylineRecover/{dockSn}")
    @Operation(summary = "恢复空中航线")
    @RequiresPermissions("bus:route:flight:inFlightWaylineRecover")
    public Result<Object> inFlightWaylineRecover(@PathVariable String dockSn) {

        return new Result<>().ok(routeFlightService.inFlightWaylineRecover(dockSn));
    }

    @LogOperation("取消空中航线")
    @PostMapping("/inFlightWaylineCancel/{dockSn}")
    @Operation(summary = "取消空中航线")
    @RequiresPermissions("bus:route:flight:inFlightWaylineCancel")
    public Result<Object> inFlightWaylineCancel(@PathVariable String dockSn) {

        return new Result<>().ok(djiBaseService.executeAndReturnResult(dockSn, "in_flight_wayline_cancel"));
    }
}
