package cn.sikey.hmd.controller.app.qr;

import cn.sikey.framework.common.exception.ServiceException;
import cn.sikey.framework.common.pojo.CommonResult;
import cn.sikey.hmd.api.hmd.vo.qr.RemainingExpireTimeRespVO;
import cn.sikey.hmd.dal.dataobject.AdminDeviceDO;
import cn.sikey.hmd.enums.RateLimit;
import cn.sikey.hmd.service.activation.AdminDeviceService;
import cn.sikey.hmd.service.activation.DeviceActivationService;
import cn.sikey.hmd.service.qr.QRCodeService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author: qin
 * @Date: 2025/3/31
 * @Description: 二维码
 */
@RestController
@RequestMapping("/qr")
@Slf4j
public class QRCodeController {

    @Resource
    private QRCodeService qrCodeService;

    @Resource
    private DeviceActivationService deviceActivationService;

    /*@Resource
    private TuLingService tulingService;*/

    @Resource
    private AdminDeviceService adminDeviceService;

    @Resource
    private String paymentPath;

    /**
     * 生成二维码
     *
     * @param deviceId 设备唯一标识
     * @param apiType  类型
     * @return
     */
    @GetMapping("/generateQRCode")
    public CommonResult<String> generateQRCode(String deviceId, String apiType) {
        if (StringUtils.isBlank(deviceId) || StringUtils.isBlank(apiType)) {
            throw new ServiceException(HttpStatus.SC_INTERNAL_SERVER_ERROR, "请检查参数");
        }

        // 校验设备是否存在
        AdminDeviceDO deviceDO = adminDeviceService.selectByDeviceId(deviceId);
        if (deviceDO == null) {
            log.info("[生成二维码]不存在该设备，设备标识：{}", deviceId);
            return CommonResult.success();
        }

        CommonResult<String> commonResult = qrCodeService.generateQRCode(deviceId, apiType, paymentPath);

        if (commonResult.getCode() != HttpStatus.SC_OK) {
            return commonResult;
        }

        return CommonResult.success(commonResult.getData());
    }

    /**
     * 查询到期时间
     *
     * @param deviceId 设备唯一标识
     * @param apiType  类型
     * @return
     */
    @RateLimit(key = "queryRemainingTimesExpireTime", type = RateLimit.LimitType.GLOBAL)
    @GetMapping("/queryRemainingTimesExpireTime")
    public CommonResult<RemainingExpireTimeRespVO> queryRemainingTimesExpireTime(String deviceId, String apiType) {
        if (StringUtils.isBlank(deviceId) || StringUtils.isBlank(apiType)) {
            throw new ServiceException(HttpStatus.SC_INTERNAL_SERVER_ERROR, "请检查参数");
        }

        // 校验设备是否存在
        AdminDeviceDO deviceDO = adminDeviceService.selectByDeviceId(deviceId);
        if (deviceDO == null) {
            log.info("[查询到期时间]不存在该设备，设备标识：{}", deviceId);
            return CommonResult.success();
        }

        return deviceActivationService.queryExpireTime(deviceId, apiType);
    }

    /**
     * 查询到期时间
     *
     * @param deviceId 设备唯一标识
     * @return
     */
    /*@GetMapping("/queryTLRemainingTimesExpireTime")
    public CommonResult<RemainingExpireTimeRespVO> queryTLRemainingTimesExpireTime(String deviceId) {
        if (StringUtils.isBlank(deviceId)) {
            throw new ServiceException(HttpStatus.SC_INTERNAL_SERVER_ERROR, "请检查参数");
        }

        // 调用图灵API
        ApiResponse<QueryUpgradeResponse> upgradeDevice = tulingService.upgradeDevice(deviceId, 2, "5af60716dc0847018da04bdcc248abb4", "c0N0459vI9x0N09Z");
        log.info("[二维码]调用图灵API，更新返回数据：{}", upgradeDevice.getData());
        ApiResponse<QueryUpgradeResponse> queryUpgradeResponseApiResponse = tulingService.getDeviceMenu(deviceId, "5af60716dc0847018da04bdcc248abb4", "c0N0459vI9x0N09Z");
        log.info("[二维码]调用图灵API，设备编码：{}，查询返回数据：{}", deviceId, queryUpgradeResponseApiResponse.getData());

        return CommonResult.success();
    }*/

    /**
     * 更新剩余次数
     *
     * @param updateRemainingTimesReqVO 剩余次数
     * @return
     */
    /*@PostMapping("/updateRemainingTimes")
    public CommonResult<Void> updateRemainingTimes(@RequestBody UpdateRemainingTimesReqVO updateRemainingTimesReqVO) {
        return qrCodeService.updateRemainingTimes(updateRemainingTimesReqVO);
    }*/

}
