package jnpf.attendance.controller;

import jnpf.util.CustomTenantUtil;
import jnpf.util.NoDataSourceBind;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * 英泰斯达考勤机控制器
 *
 * @author yanwenfu
 * @create 2024-03-28
 */
@RestController
@Slf4j
@RequestMapping(value = "/ffi")
public class FfiMachineController {

    @Autowired
    private CustomTenantUtil customTenantUtil;

    /**
     * 设备录入自定义编号判断
     * @return java.util.Map<java.lang.String,java.lang.Object>
     */
    @PostMapping(consumes = "application/octet-stream")
    @NoDataSourceBind
    public Map<String, Object> addFace(HttpServletRequest request) {

        String requestCode = request.getHeader("request_code");
        String devId = request.getHeader("dev_id");
        String contentLength = request.getHeader("Content-Length");
        log.error("requestCode: {}, devId: {}, contentLength: {}", requestCode, devId, contentLength);
        int contentLength2 = request.getContentLength();
        log.error("contentLength2: {}", contentLength2);
        // 获取请求体的ServletInputStream
        String body;
        try {
            // 读取二进制流（这里需要自己实现）
            ServletInputStream inputStream = request.getInputStream();
            byte[] buffer = new byte[contentLength2];
            int bytesRead = 0;
            while (bytesRead < contentLength2) {
                int bytes = inputStream.read(buffer, bytesRead, contentLength2 - bytesRead);
                if (bytes == -1) {
                    break;
                }
                bytesRead += bytes;
            }
            log.error("buffer bytes: {}", Arrays.toString(buffer));
            body = getJsonBlock(buffer);
        } catch (IOException e) {
            log.error(e.getMessage());
            return null;
        }
        log.error("body: {}", body);
        Map<String, Object> map = new HashMap<>();
        map.put("Result", 0);
        map.put("Msg", "进入方法...");
        return map;
    }

    private String getJsonBlock(byte[] buffer) {

        String str = "";
        if (buffer.length < 4) {
            return str;
        }
        int lenText = ByteBuffer.wrap(buffer).order(ByteOrder.LITTLE_ENDIAN).getInt();
        if (lenText > buffer.length - 4 || lenText == 0) {
            return str;
        }
        str = new String(buffer, 4, lenText, StandardCharsets.UTF_8);
        if (buffer[4 + lenText - 1] == 0) {
            str = new String(buffer, 4, lenText - 1, StandardCharsets.UTF_8);
        }
        return str;
    }
}
