package com.ruoyi.iot.controller;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.iot.domain.WeitiandiDeviceInfo;
import com.ruoyi.iot.entity.param.DeviceParam;
import com.ruoyi.iot.service.DeviceControlerService;
import com.ruoyi.iot.service.ITChargeRecordService;
import com.ruoyi.iot.service.ITDeviceRecordService;
import com.ruoyi.iot.service.IWeitiandiDeviceInfoService;
import com.ruoyi.iot.transdata.RpcResult;
import com.ruoyi.iot.transdata.entity.MainBoard;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;

import static com.ruoyi.common.utils.SecurityUtils.getLoginUser;

@Slf4j
@RestController
@RequestMapping("/device")
public class DeviceController {

    @Autowired
    private ITDeviceRecordService itDeviceRecordService;

    @Autowired
    private ITChargeRecordService itChargeRecordService;

    @Autowired
    private DeviceControlerService deviceControlerService;

    @Autowired
    private IWeitiandiDeviceInfoService iWeitiandiDeviceInfoService;


    @GetMapping("/testrpc")
    @Anonymous
    public RpcResult testRpc(){
        return null;
    }



    @PostMapping("/getDeviceInfo")
    @ApiOperation("获取设备详情")
    public AjaxResult getDeviceInfo(@RequestParam("qrcode") String qrcode){
        Long userId = getLoginUser().getUserId();
        WeitiandiDeviceInfo deviceInfoByQrcode = iWeitiandiDeviceInfoService.getDeviceInfoByQrcode(qrcode, userId);
        return AjaxResult.success(deviceInfoByQrcode);
    }

    @PostMapping("/sendPortDetailCmd")
    @ApiOperation("发送获取端口详情CMD")
    public AjaxResult sendPortDetailCmd (@RequestBody DeviceParam deviceParam) {
        deviceControlerService.sendPortDetailCmd(deviceParam);
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
        return AjaxResult.success(sdf.format(date));
    }

    @PostMapping("/getPortDetail")
    @ApiOperation("根据设备编号查询设备详情")
    public AjaxResult getPortDetail (@RequestBody DeviceParam deviceParam) {
        JSONObject portDetail = itDeviceRecordService.getPortDetail(deviceParam);
        return AjaxResult.success(portDetail);
    }

    @PostMapping("/startCharge")
    public AjaxResult startCharge(@RequestBody DeviceParam deviceParam) {
        // 对电流值进行处理
        LoginUser loginUser = getLoginUser();
        Long userId = loginUser.getUserId();
        itChargeRecordService.startCharge(deviceParam.getDeviceId(),deviceParam.getCcid(),userId);
        return AjaxResult.success();
    }

    @PostMapping("/stopCharge")
    public AjaxResult stopCharge(@RequestBody DeviceParam deviceParam) {
        // 对电流值进行处理
        deviceControlerService.stopCharge(deviceParam.getDeviceId(),deviceParam.getCcid());
        return AjaxResult.success();
    }

    /**
     * 1.在开始充电后，需要做一个定时任务检测是否结束充电
     *
     * 2.在普通情况下 检查枪是否变动  暂定 感觉是枪口连上了 才有
     * @return
     */
    @PostMapping("/statusChange")
    public AjaxResult checkStatusChange(@RequestBody DeviceParam deviceParam){
        JSONObject object = itDeviceRecordService.checkStatus(deviceParam);
        return AjaxResult.success(object);
    }

    @PostMapping("/setCurrent")
    public AjaxResult setCurrent(@RequestBody DeviceParam deviceParam,@RequestParam("current") Integer current) {
        MainBoard mainBoard = new MainBoard();
        mainBoard.setMaxCurrent(current);
        String deviceId = deviceParam.getDeviceId();
        String ccid = deviceParam.getCcid();
        deviceControlerService.setMainBoard(deviceId,ccid,mainBoard);
        return AjaxResult.success(current);
    }

    @PostMapping("/sendMainboardCmd")
    public AjaxResult sendMainboardCmd(@RequestBody DeviceParam deviceParam) {

        deviceControlerService.getMainBoard(deviceParam.getDeviceId(),deviceParam.getCcid());
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
        return AjaxResult.success(sdf.format(date));
    }

    @PostMapping("/getMainboardConfig")
    public AjaxResult getMainboardConfig(@RequestBody DeviceParam deviceParam) {
        JSONObject mainboardConfig = itDeviceRecordService.getMainboardConfig(deviceParam);
        return AjaxResult.success(mainboardConfig);
    }

    @PostMapping("/resetMainboard")
    public AjaxResult resetMainboard(@RequestBody DeviceParam deviceParam) {
        // 恢复默认配置
        deviceControlerService.reset(deviceParam.getDeviceId(),deviceParam.getCcid());
        return AjaxResult.success();
    }

    @PostMapping("/restartMainboard")
    public AjaxResult restartMainboard(@RequestBody DeviceParam deviceParam) {
        // 重启充电桩
        deviceControlerService.restart(deviceParam.getDeviceId(),deviceParam.getCcid());
        return AjaxResult.success();
    }

}
