package com.xueyi.system.resource.controller.api;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.xueyi.common.web.constant.ResponseCode;
import com.xueyi.common.web.response.MyResponse;
import com.xueyi.system.digitalmans.mapper.DmDigitalmanMapper;
import com.xueyi.system.digitalmans.service.impl.DmDigitalmanServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.Map;
import java.util.TreeMap;

public class BaseApiController extends com.xueyi.common.web.controller.BaseApiController {


    @Autowired
    private DmDigitalmanServiceImpl dmDigitalmanService;

    @Autowired
    private DmDigitalmanMapper dmDigitalmanMapper;

    public MyResponse commonCheck(String devId, Double timestamp, String sign) {
        String regexZS = "[0-9A-Za-z_-]+";
        if (!(StringUtils.isNotEmpty(devId) && devId.matches(regexZS))) {
            return output(ResponseCode.ILLEGAL_PARAMETER, "devId");
        }

        if (!(StringUtils.isNotEmpty(sign) && sign.matches(regexZS))) {
            return output(ResponseCode.ILLEGAL_PARAMETER, "sign");
        }

//        dmDigitalmanService.selectById()
        /*DmDigitalmanDto dmDigitalmanDto = dmDigitalmanMapper.selectByCode(devId);
        System.err.println(dmDigitalmanDto);*/

       /* Device device = deviceService.findByDevId(devId);
        if (!ObjectUtils.isNotNull(device)) {
            return output(ResponseCode.PARAM_ID_NOT_EXIST_RECORD, devId);
        }
        System.err.println("----------sign check start----------");
        String singStr = devId + device.getBindDeviceSn() + timestamp + device.getAuthCode();
        System.err.println(singStr);
        String signStr = DigestUtils.md5DigestAsHex(singStr.getBytes());
        System.err.println(signStr);
        System.err.println("----------sign check end----------");

        if (!sign.equals(signStr)) {
            return output(ResponseCode.AUTH_NOT_PASS, null);
        }*/

        return output(ResponseCode.SUCCESS, null);
    }

    /*/**
     * @Author yangkai
     * @Description //TODO
     * @Date 2023/4/19
     * @Param [request]
     * @Param [type] 1，application/json方式请求；2，form-data方式请求
     *
     * @return com.alibaba.fastjson2.JSONObject
     **/
    public JSONObject genSign(HttpServletRequest request, Integer type){
        if (1 == type) {
            Map<String, String[]> paramMap = request.getParameterMap();

            // 將參數轉換為TreeMap並按照key排序
            TreeMap<String, String> sortedParamMap = new TreeMap<>();
            for (Map.Entry<String, String[]> entry : paramMap.entrySet()) {
                String key = entry.getKey();
                String[] valueArray = entry.getValue();
                String value = (valueArray.length > 0) ? valueArray[0] : "";
                sortedParamMap.put(key, value);
            }
            return JSONObject.from(sortedParamMap);

        } else {
            BufferedReader reader = null;
            try {
                reader = request.getReader();
                StringBuilder stringBuilder = new StringBuilder();
                String line = null;
                while ((line = reader.readLine()) != null) {
                    stringBuilder.append(line);
                }
                String requestBody = stringBuilder.toString();

                ObjectMapper objectMapper = new ObjectMapper();
                Map<String, String[]> requestMap = objectMapper.readValue(requestBody, new TypeReference<Map<String, String[]>>(){});

                TreeMap<String, String> sortedParamMap = new TreeMap<>();
                for (Map.Entry<String, String[]> entry : requestMap.entrySet()) {
                    String key = entry.getKey();
                    String[] valueArray = entry.getValue();
                    String value = (valueArray.length > 0) ? valueArray[0] : "";
                    sortedParamMap.put(key, value);
                }
                return JSONObject.from(sortedParamMap);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }

        }
    }


}