package com.multictrl.modules.business.controller;

import cn.hutool.json.JSONObject;
import com.multictrl.common.annotation.ApiOrder;
import com.multictrl.common.annotation.LogOperation;
import com.multictrl.common.constant.Constant;
import com.multictrl.common.page.PageData;
import com.multictrl.common.utils.Result;
import com.multictrl.common.validator.AssertUtils;
import com.multictrl.modules.business.dto.FlightTaskDTO;
import com.multictrl.modules.business.dto.MediaFileDTO;
import com.multictrl.modules.business.entity.SrsRecordEntity;
import com.multictrl.modules.business.service.FlightTaskService;
import io.swagger.v3.oas.annotations.Operation;
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 io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;

import java.util.List;
import java.util.Map;

/**
 * 飞行架次
 *
 * @author Sdy
 * @since 1.0.0 2026-05-02
 */
@RestController
@RequestMapping("business/task")
@Tag(name = "飞行架次")
@ApiOrder(7)
@RequiredArgsConstructor
public class FlightTaskController {
    private final FlightTaskService flightTaskService;

    @GetMapping("page")
    @Operation(summary = "分页")
    @Parameters({
            @Parameter(name = Constant.PAGE, description = "当前页码，从1开始"),
            @Parameter(name = Constant.LIMIT, description = "每页显示记录数"),
            @Parameter(name = "key", description = "机库/航线名称"),
            @Parameter(name = "taskType", description = "任务类型 1:航线飞行 2:手动飞行 3:定时飞行"),
            @Parameter(name = "routeType", description = "航线类型 航点waypoint"),
            @Parameter(name = "taskStatus", description = "任务状态 0:进行中 -1:失败 1:完成 2:阻飞")
    })
    @RequiresPermissions("bus:task:page")
    public Result<PageData<FlightTaskDTO>> page(@Parameter(hidden = true) @RequestParam Map<String, Object> params) {
        PageData<FlightTaskDTO> page = flightTaskService.pageList(params);

        return new Result<PageData<FlightTaskDTO>>().ok(page);
    }

    @GetMapping("{id}")
    @Operation(summary = "信息")
    @RequiresPermissions("bus:task:info")
    public Result<FlightTaskDTO> get(@PathVariable("id") Long id) {
        FlightTaskDTO data = flightTaskService.get(id);

        return new Result<FlightTaskDTO>().ok(data);
    }

    @DeleteMapping
    @Operation(summary = "删除")
    @LogOperation("删除")
    @RequiresPermissions("bus:task:delete")
    public Result<Object> delete(@RequestBody Long[] ids) {
        //效验数据
        AssertUtils.isArrayEmpty(ids, "id");
        flightTaskService.delete(ids);

        return new Result<>();
    }

    @GetMapping("/getFlightTrack/{taskId}")
    @Operation(summary = "飞行轨迹")
    @RequiresPermissions("bus:task:flightTrack")
    public Result<List<JSONObject>> getFlightTrack(@PathVariable("taskId") String taskId) {
        List<JSONObject> data = flightTaskService.getFlightTrack(taskId);

        return new Result<List<JSONObject>>().ok(data);
    }

    @GetMapping("/getFlightLog/{taskId}")
    @Operation(summary = "飞行日志")
    @RequiresPermissions("bus:task:flightLog")
    public Result<List<JSONObject>> getFlightLog(@PathVariable("taskId") String taskId) {
        List<JSONObject> data = flightTaskService.getFlightLog(taskId);

        return new Result<List<JSONObject>>().ok(data);
    }

    @GetMapping("/getTaskMedia/{taskId}")
    @Operation(summary = "媒体文件")
    @RequiresPermissions("bus:task:taskMedia")
    public Result<List<MediaFileDTO>> getTaskMedia(@PathVariable("taskId") String taskId) {
        List<MediaFileDTO> data = flightTaskService.getTaskMedia(taskId);

        return new Result<List<MediaFileDTO>>().ok(data);
    }

    @GetMapping("/getFlightVideo/{taskId}")
    @Operation(summary = "飞行视频")
    @RequiresPermissions("bus:task:flightVideo")
    public Result<List<SrsRecordEntity>> getFlightVideo(@PathVariable("taskId") String taskId) {
        List<SrsRecordEntity> data = flightTaskService.getFlightVideo(taskId);

        return new Result<List<SrsRecordEntity>>().ok(data);
    }
}
