package com.wteam.controller.order;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.extra.qrcode.QrCodeUtil;
import cn.hutool.extra.qrcode.QrConfig;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import com.egzosn.pay.common.bean.PayOutMessage;
import com.wteam.framework.common.enums.ResultCode;
import com.wteam.framework.common.enums.ResultUtil;
import com.wteam.framework.common.exception.ServiceException;
import com.wteam.framework.common.vo.ResultMessage;
import com.wteam.framework.modules.hardware.ApiCallerUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Base64;
import java.util.Random;

import static java.lang.String.valueOf;

/**
 * @author doncic
 * @date 2023/9/26 20:56
 */
@Slf4j
@Tag(name = "二维码相关接口管理")
@RestController
@RequestMapping("/v1/qrcode")
public class QrCodeController {


    //TODO ：免费场次直接获取二维码进入即可 中间是随机数据 第二位到时候可能加上用户信息
    @Operation(summary = "获得免费场次的二维码")
    @GetMapping("/getCode/{userId}")
    public ResultMessage<String> getCode(@PathVariable String userId) {
        // 获取当前日期
        LocalDate currentDate = LocalDate.now();
        // 格式化日期为字符串
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
        String formattedDate = currentDate.format(formatter);
        String numbers1 = RandomUtil.randomNumbers(5);
        String numbers2 = RandomUtil.randomNumbers(5);
        String data = "pxo_" + formattedDate + "_" + numbers1 + "_" + numbers2 + "_00";
        log.info("此时生成的二维码为：{} ", data);

        // 接口地址和参数
        String apiUrl = "https://xiaofeng.pankzone.com/api/openapi/reg_access_qcode.x";
        //这个mac为出门
        String mac1 = "863569068849199";
        /**
         * 进
         * 出
         * 863569068848969
         * 863569068849199
         */
        //这个mac为进门
        String mac2 = "863569068848969";
        String s = callApiWithMac(apiUrl, mac1, mac2, data);
        log.info("此时mac1注册生成的二维码为：{}", s);
        return ResultUtil.data(s);
    }

    public String callApiWithMac(String apiUrl, String mac1, String mac2, String data) {
        String tk = ApiCallerUtil.callApi(mac1);
        log.info("此时获取到的token为：{} ", tk);

        // 拼接两个 MAC 地址，以逗号分隔
        String macParam = mac1 + "_" + mac2;
        log.info("此时拼接的macParam为：{}", macParam);


        //调用hutool接口注册二维码信息
        HttpResponse response = HttpRequest.post(apiUrl)
                .form("mac", macParam)
                .form("tk", tk)
                .form("qcodetxt", data)
                .form("enablecount", 100)
                .execute();

        String body = response.body();
        JSONObject json = new JSONObject(body);
        int code = json.getInt("code");
        log.info("此时返回的code为：{}", code);
        String qcodetxt = json.getStr("qcodetxt");
        log.info("此时返回的qcodetxt为：{}", qcodetxt);
        String desc = json.getStr("desc");
        log.info("此时返回的desc为：{}", desc);

        if (code != 0 && code != 4) {
            throw new ServiceException("硬件回调url设置接口异常！");
        }
        log.info("此时接口响应数据为：{} (不要记录敏感信息在日志中)", body);
        return data;
    }

}

