package com.multictrl.modules.business.controller;

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.common.validator.ValidatorUtils;
import com.multictrl.modules.business.dto.DockDeviceDTO;
import com.multictrl.modules.business.dto.FirmwareDTO;
import com.multictrl.modules.business.dto.firmware.FirmwareVersion;
import com.multictrl.modules.business.service.FirmwareService;
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 org.springframework.web.multipart.MultipartFile;

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

/**
 * 固件表
 *
 * @author Sdy
 * @since 1.0.0 2026-05-11
 */
@RestController
@RequestMapping("business/firmware")
@Tag(name = "固件更新")
@ApiOrder(11)
@RequiredArgsConstructor
public class FirmwareController {
    private final FirmwareService firmwareService;

    @GetMapping("page")
    @Operation(summary = "分页")
    @Parameters({
            @Parameter(name = Constant.PAGE, description = "当前页码，从1开始"),
            @Parameter(name = Constant.LIMIT, description = "每页显示记录数")
    })
    @RequiresPermissions("bus:firmware:page")
    public Result<PageData<FirmwareDTO>> page(@Parameter(hidden = true) @RequestParam Map<String, Object> params) {
        PageData<FirmwareDTO> page = firmwareService.page(params);

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

    @GetMapping("/getFirmware/{dockSn}")
    @Operation(summary = "获取设备固件")
    @LogOperation("获取设备固件")
    @RequiresPermissions("bus:firmware:getFirmware")
    public Result<List<FirmwareDTO>> getFirmware(@PathVariable String dockSn) {
        List<FirmwareDTO> data = firmwareService.getFirmwareList(dockSn);

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

    @PostMapping
    @Operation(summary = "上传固件", description = "上传从大疆官网获取的机库或飞机的固件包，格式：zip")
    @LogOperation("上传固件")
    @RequiresPermissions("bus:firmware:upload")
    public Result<Object> uploadFirmware(@Parameter(description = "官方固件包") @RequestParam("file") MultipartFile file,
                                         @Parameter(description = "设备名称") @RequestParam("deviceName") String deviceName,
                                         @Parameter(description = "备注") @RequestParam(value = "remark", required = false) String remark) {

        firmwareService.uploadFirmware(file, deviceName, remark);

        return new Result<>();
    }

    @PutMapping
    @Operation(summary = "修改备注")
    @LogOperation("修改备注")
    @RequiresPermissions("bus:firmware:update")
    public Result<Object> update(@RequestBody FirmwareDTO dto) {
        ValidatorUtils.validateEntity(dto);
        firmwareService.update(dto);

        return new Result<>();
    }

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

        return new Result<>();
    }

    @PostMapping("/ota/{dockSn}")
    @LogOperation("固件升级")
    @Operation(summary = "固件升级", description = "固件升级，设备必须开机情况下才能升级，如果给飞机升级，需要调试模式打开飞机，然后关闭调试模式，再进行升级")
    @RequiresPermissions("bus:firmware:ota")
    public Result<Object> ota(@PathVariable String dockSn,
                              @Parameter(description = "固件id") @RequestParam Long firmwareId) {

        return new Result<>().ok(firmwareService.ota(dockSn, firmwareId));
    }

    @GetMapping("/registerDevices")
    @Operation(summary = "设备列表")
    @LogOperation("设备列表")
    @RequiresPermissions("bus:firmware:registerDevices")
    public Result<List<DockDeviceDTO>> registerDevices() {
        List<DockDeviceDTO> list = firmwareService.registerDevices();

        return new Result<List<DockDeviceDTO>>().ok(list);
    }

    @GetMapping("/getCurrentFirmwareVersion/{dockSn}")
    @Operation(summary = "设备当前版本", description = "获取机库和飞机的固件版本，如果为空，需重启机场")
    @LogOperation("设备当前版本")
    @RequiresPermissions("bus:firmware:currentFirmware")
    public Result<FirmwareVersion> getCurrentFirmwareVersion(@PathVariable String dockSn) {
        FirmwareVersion data = firmwareService.getCurrentFirmwareVersion(dockSn);

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

}
