package com.njcn.device.pq.controller.file;

import cn.hutool.core.util.IdUtil;
import com.njcn.common.pojo.annotation.OperateInfo;
import com.njcn.common.pojo.enums.common.LogEnum;
import com.njcn.device.device.service.DeviceProcessService;
import com.njcn.device.device.service.IDeviceService;
import com.njcn.device.pq.pojo.param.AskFileSysParam;
import com.njcn.device.pq.pojo.po.Device;
import com.njcn.device.pq.pojo.po.DeviceProcess;
import com.njcn.message.api.ProduceFeignClient;
import com.njcn.message.message.AskFileSysMessage;
import com.njcn.web.controller.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.async.DeferredResult;

/**
 * Description:
 * Date: 2026/04/29  上午 11:41【需求编号】
 *
 * @author clam
 * @version V1.0.0
 */
@RestController
@RequestMapping("/dir")
@RequiredArgsConstructor
@Slf4j
@Api(tags = "装置文件系统")
public class DirectoryController extends BaseController {


    private final ProduceFeignClient produceFeignClient;


    private final PendingRequestManager pendingRequestManager;

    private final IDeviceService iDeviceService;
    private final DeviceProcessService deviceProcessService;

    @PostMapping("/list")
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @ApiOperation("获取装置文件目录")
    public DeferredResult<Object> listDirectory(@RequestBody AskFileSysParam askFileSysParam) {
        String methodDescribe = getMethodDescribe("listDirectory");

        long timeoutMs = 15000L; // 15 秒超时
        // 构造指令 body
        Device device = iDeviceService.getById(askFileSysParam.getDevId());
        DeviceProcess deviceProcess = deviceProcessService.getById(askFileSysParam.getDevId());
        // 发送 MQ 指令，获取 msgId
        AskFileSysMessage askFileSysMessage = new AskFileSysMessage();
        askFileSysMessage.setGuid(IdUtil.simpleUUID());
        askFileSysMessage.setNodeId(device.getNodeId());
        askFileSysMessage.setProcessNo(deviceProcess.getProcessNo());
        askFileSysMessage.setDevId(askFileSysParam.getDevId());
        askFileSysMessage.setType(0);
        askFileSysMessage.setPath(askFileSysParam.getPath());

        produceFeignClient.askFileSys(askFileSysMessage);
        // 创建挂起请求
        DeferredResult<Object> deferredResult = pendingRequestManager.createPendingRequest(askFileSysMessage.getGuid(), timeoutMs);
        // 可额外在 Redis 中记录初始状态（可选）
        // 返回 DeferredResult，Spring 将挂起此请求
        return deferredResult;
    }

    @PostMapping("/downLoadFile")
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @ApiOperation("获取装置文件文件")
    public DeferredResult<Object> downLoadFile(@RequestBody AskFileSysParam askFileSysParam) {
        String methodDescribe = getMethodDescribe("downLoadFile");

        long timeoutMs = 15000L; // 15 秒超时
        // 构造指令 body
        Device device = iDeviceService.getById(askFileSysParam.getDevId());
        DeviceProcess deviceProcess = deviceProcessService.getById(askFileSysParam.getDevId());
        // 发送 MQ 指令，获取 msgId
        AskFileSysMessage askFileSysMessage = new AskFileSysMessage();
        askFileSysMessage.setGuid(IdUtil.simpleUUID());
        askFileSysMessage.setNodeId(device.getNodeId());
        askFileSysMessage.setProcessNo(deviceProcess.getProcessNo());
        askFileSysMessage.setDevId(askFileSysParam.getDevId());
        askFileSysMessage.setType(1);
        askFileSysMessage.setPath(askFileSysParam.getPath());

        produceFeignClient.askFileSys(askFileSysMessage);
        // 创建挂起请求
        DeferredResult<Object> deferredResult = pendingRequestManager.createPendingRequest(askFileSysMessage.getGuid(), timeoutMs);
        // 可额外在 Redis 中记录初始状态（可选）
        // 返回 DeferredResult，Spring 将挂起此请求
        return deferredResult;
    }

    @PostMapping("/upLoadFile")
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @ApiOperation("上传装置文件文件")
    public DeferredResult<Object> upLoadFile(@RequestBody AskFileSysParam askFileSysParam) {
        String methodDescribe = getMethodDescribe("upLoadFile");

        long timeoutMs = 15000L; // 15 秒超时
        // 构造指令 body
        Device device = iDeviceService.getById(askFileSysParam.getDevId());
        DeviceProcess deviceProcess = deviceProcessService.getById(askFileSysParam.getDevId());
        // 发送 MQ 指令，获取 msgId
        AskFileSysMessage askFileSysMessage = new AskFileSysMessage();
        askFileSysMessage.setGuid(IdUtil.simpleUUID());
        askFileSysMessage.setNodeId(device.getNodeId());
        askFileSysMessage.setProcessNo(deviceProcess.getProcessNo());
        askFileSysMessage.setDevId(askFileSysParam.getDevId());
        askFileSysMessage.setType(2);
        askFileSysMessage.setPath(askFileSysParam.getPath());
        askFileSysMessage.setRemotePath(askFileSysParam.getRemotePath());
        produceFeignClient.askFileSys(askFileSysMessage);
        // 创建挂起请求
        DeferredResult<Object> deferredResult = pendingRequestManager.createPendingRequest(askFileSysMessage.getGuid(), timeoutMs);
        // 可额外在 Redis 中记录初始状态（可选）
        // 返回 DeferredResult，Spring 将挂起此请求
        return deferredResult;
    }

    @PostMapping("/delete")
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @ApiOperation("装置目录，文件删除")
    public DeferredResult<Object> delete(@RequestBody AskFileSysParam askFileSysParam) {
        String methodDescribe = getMethodDescribe("delete");

        long timeoutMs = 15000L; // 15 秒超时
        // 构造指令 body
        Device device = iDeviceService.getById(askFileSysParam.getDevId());
        DeviceProcess deviceProcess = deviceProcessService.getById(askFileSysParam.getDevId());
        // 发送 MQ 指令，获取 msgId
        AskFileSysMessage askFileSysMessage = new AskFileSysMessage();
        askFileSysMessage.setGuid(IdUtil.simpleUUID());
        askFileSysMessage.setNodeId(device.getNodeId());
        askFileSysMessage.setProcessNo(deviceProcess.getProcessNo());
        askFileSysMessage.setDevId(askFileSysParam.getDevId());
        askFileSysMessage.setType(3);
        askFileSysMessage.setPath(askFileSysParam.getPath());
        askFileSysMessage.setRemotePath(askFileSysParam.getRemotePath());
        produceFeignClient.askFileSys(askFileSysMessage);
        // 创建挂起请求
        DeferredResult<Object> deferredResult = pendingRequestManager.createPendingRequest(askFileSysMessage.getGuid(), timeoutMs);
        // 可额外在 Redis 中记录初始状态（可选）
        // 返回 DeferredResult，Spring 将挂起此请求
        return deferredResult;
    }

    @PostMapping("/restart")
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @ApiOperation("装置重启")
    public DeferredResult<Object> restart(@RequestBody AskFileSysParam askFileSysParam) {
        String methodDescribe = getMethodDescribe("restart");

        long timeoutMs = 15000L; // 15 秒超时
        // 构造指令 body
        Device device = iDeviceService.getById(askFileSysParam.getDevId());
        DeviceProcess deviceProcess = deviceProcessService.getById(askFileSysParam.getDevId());
        // 发送 MQ 指令，获取 msgId
        AskFileSysMessage askFileSysMessage = new AskFileSysMessage();
        askFileSysMessage.setGuid(IdUtil.simpleUUID());
        askFileSysMessage.setNodeId(device.getNodeId());
        askFileSysMessage.setProcessNo(deviceProcess.getProcessNo());
        askFileSysMessage.setDevId(askFileSysParam.getDevId());
        askFileSysMessage.setPath("reboot");
        askFileSysMessage.setType(4);
        produceFeignClient.askFileSys(askFileSysMessage);
        // 创建挂起请求
        DeferredResult<Object> deferredResult = pendingRequestManager.createPendingRequest(askFileSysMessage.getGuid(), timeoutMs);
        // 可额外在 Redis 中记录初始状态（可选）
        // 返回 DeferredResult，Spring 将挂起此请求
        return deferredResult;
    }


}
