package com.ruoyi.sim.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.sim.service.impl.CommSendService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/sim/debug")
@Api("硬件通信DebugController")
public class HardwareCommDebugController extends BaseController {
    @Autowired
    private CommSendService commSendService;
    @Autowired
    private ApplicationContext applicationContext;

    @GetMapping(value = "/spring-boot-close")
    @ApiOperation("关闭本SpringBoot应用")
    public void debugReadSimType() {
        ((ConfigurableApplicationContext) applicationContext).close();
    }

    @GetMapping(value = "/debugReadSimType/{simNum}")
    @ApiOperation("debug读取模拟器类型序列号")
    public AjaxResult debugReadSimType(@PathVariable("simNum") final String simNum) {
        return success(commSendService.debugReadSimType(simNum));
    }

    @GetMapping(value = "/debugReadOneFaultResistance/{simNum}/{bindHardwareMsg}")
    @ApiOperation("debug读取一个故障位置数据")
    public AjaxResult debugReadOneFaultResistance(@PathVariable("simNum") final String simNum,
                                                  @PathVariable("bindHardwareMsg") final String bindHardwareMsg) {
        return success(commSendService.debugReadOneFaultResistance(simNum, bindHardwareMsg));
    }

    @GetMapping(value = "/debugReadAllFaultResistance/{simNum}")
    @ApiOperation("debug读取全部故障位置数据")
    public AjaxResult debugReadAllFaultResistance(@PathVariable("simNum") final String simNum) {
        return success(commSendService.debugReadAllFaultResistance(simNum));
    }

    @GetMapping(value = "/debugReadAllFaultResistanceBySimNum/{simNum}")
    @ApiOperation("debug通过simNum读取一台模拟器所有故障答题值，保存debug_fault表中，类似交卷")
    public AjaxResult debugReadAllFaultResistanceBySimNum(@PathVariable("simNum") final String simNum) {
        return commSendService.debugReadAllFaultResistanceBySimNum(simNum);
    }

    @GetMapping(value = "/debugClearOneFault/{simNum}/{bindHardwareMsg}")
    @ApiOperation("debug清除一个故障")
    public AjaxResult debugClearOneFault(@PathVariable("simNum") final String simNum,
                                         @PathVariable("bindHardwareMsg") final String bindHardwareMsg) {
        return success(commSendService.debugClearOneFault(simNum, bindHardwareMsg));
    }

    @GetMapping(value = "/debugClearAllFaultBySimNum/{simNum}")
    @ApiOperation("debug通过simNum清除一台模拟器所有故障")
    public AjaxResult debugClearAllFaultBySimNum(@PathVariable("simNum") final String simNum) {
        return commSendService.debugClearAllFaultBySimNum(simNum);
    }

    @GetMapping(value = "/debugClearAllOnlineSimAllFault/")
    @ApiOperation("debug清除所有在线模拟器所有故障")
    public AjaxResult debugClearAllOnlineSimAllFault() {
        return commSendService.debugClearAllOnlineSimAllFault();
    }

    @GetMapping(value = "/debugWriteOneFault/{simNum}/{bindHardwareMsg}")
    @ApiOperation("debug下发一个故障")
    public AjaxResult debugWriteOneFault(@PathVariable("simNum") final String simNum,
                                         @PathVariable("bindHardwareMsg") final String bindHardwareMsg) {
        return success(commSendService.debugWriteOneFault(simNum, bindHardwareMsg));
    }

    @GetMapping(value = "/debugWriteAllFault/{simNum}")
    @ApiOperation("debug下发所有故障")
    public AjaxResult debugWriteAllFault(@PathVariable("simNum") final String simNum) {
        return success(commSendService.debugWriteAllFault(simNum));
    }

    @GetMapping(value = "/debugWriteSelectedFaultBySimNum/{simNum}/{faultIds}")
    @ApiOperation("debug下发所选故障，保存debug_fault表中，类似开始考试")
    public AjaxResult debugWriteSelectedFaultBySimNum(@PathVariable("simNum") final String simNum,
                                                      @PathVariable("faultIds") final String[] faultIds,
                                                      @RequestParam final Boolean checkReplace) {
        return commSendService.debugWriteSelectedFaultBySimNum(simNum, faultIds, checkReplace);
    }
}
