package com.wteam.framework.modules.hardware;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONObject;
import com.wteam.framework.common.exception.ServiceException;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class QrCodeRegistrationUtil {


    /**
     * @param data
     * @return
     */
    public static Boolean registerTimerQrCode(String data) {

        // 获取当前时间戳
        long currentTime = System.currentTimeMillis() / 1000;

        // 获取60秒之后的时间戳
        long sixtySecondsLater = currentTime + 60;


        String apiUrl = "https://xiaofeng.pankzone.com/api/openapi/reg_access_qcode.x";
        String mac1 = "863569068849199";
        String mac2 = "863569068848969";
        String tk = ApiCallerUtil.callApi(mac1);
        log.info("此时获取到的token为：{} ", tk);

        String macParam = mac1 + "_" + mac2;
        log.info("此时拼接的macParam为：{}", macParam);

        HttpResponse response = HttpRequest.post(apiUrl)
                .form("mac", macParam)
                .form("tk", tk)
                .form("qcodetxt", data)
                .form("starttime", currentTime)
                .form("enabletime", sixtySecondsLater)
                .form("enablecount", 2)
                .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 true;
    }


    /**
     * @param data
     * @return
     */
    public static Boolean registerMonthQrCode(String data) {
        String apiUrl = "https://xiaofeng.pankzone.com/api/openapi/reg_access_qcode.x";
        String mac1 = "863569068849199";
        String mac2 = "863569068848969";
        String tk = ApiCallerUtil.callApi(mac1);
        log.info("此时获取到的token为：{} ", tk);

        String macParam = mac1 + "_" + mac2;
        log.info("此时拼接的macParam为：{}", macParam);

        HttpResponse response = HttpRequest.post(apiUrl)
                .form("mac", macParam)
                .form("tk", tk)
                .form("qcodetxt", data)
                .form("enablecount", 50)
                .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 true;
    }

    public static Boolean registerTicketQrCode(String data) {
        String apiUrl = "https://xiaofeng.pankzone.com/api/openapi/reg_access_qcode.x";
        String mac1 = "863569068849199";
        String mac2 = "863569068848969";
        String tk = ApiCallerUtil.callApi(mac1);
        log.info("此时获取到的token为：{} ", tk);


        String macParam = mac1 + "_" + mac2;
        log.info("此时拼接的macParam为：{}", macParam);

        HttpResponse response = HttpRequest.post(apiUrl).form("mac", macParam).form("tk", tk).form("qcodetxt", data).form("enablecount", 2).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 true;
    }


    public static String registerOneQrCode(String data) {
        String apiUrl = "https://xiaofeng.pankzone.com/api/openapi/reg_access_qcode.x";
        String mac1 = "863569068849199";
        String mac2 = "863569068848969";
        String tk = ApiCallerUtil.callApi(mac1);
        log.info("此时获取到的token为：{} ", tk);

        String macParam = mac1 + "_" + mac2;
        log.info("此时拼接的macParam为：{}", macParam);

        HttpResponse response = HttpRequest.post(apiUrl).form("mac", macParam).form("tk", tk).form("qcodetxt", data).form("enablecount", 1).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;
    }
}
