package com.xhpc.pp.utils.security;

import cn.hutool.core.date.DateUtil;
import com.xhpc.pp.utils.HexUtils;
import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.concurrent.TimeUnit;

import static cn.hutool.core.date.DatePattern.NORM_DATETIME_FORMAT;
import static com.xhpc.common.core.utils.StringUtils.capitalize;
import static com.xhpc.common.data.redis.StaticBeanUtil.REDIS;
import static com.xhpc.pp.utils.security.CP56Time2a.cp56toDate;

@Slf4j
public class CacheDataUtils {

    public static Object reflectTranslate(Object srcobj, Class tarclz, Class srcclz, Field[] targetfields) throws IllegalAccessException, InvocationTargetException, InstantiationException {

        Constructor[] constructors = tarclz.getConstructors();
        Object tarobj = null;
        for (Constructor c : constructors) {
            if (c.getParameters().length == 0) {
                tarobj = c.newInstance();
                for (Field tarfield : targetfields) {
                    String tarFieldName = tarfield.getName();
                    String srcval = null;
                    try {
                        srcval = (String) srcclz.getMethod("get".concat(capitalize(tarFieldName))).invoke(srcobj);
                        Object tarval;
                        if (tarfield.getType().getSimpleName().equals("Integer") && !tarfield.getName().equals("stopReason")) {
                            if (srcval.length() == 2) {
                                tarval = Integer.valueOf(srcval, 16);
                                if (tarfield.getName().endsWith("Temperature")) {
                                    tarval = (Integer) tarval - 50;
                                    tarval = (Integer) tarval > 0 ? tarval : 0;
                                }
                            } else {
                                try {
                                    tarval = HexUtils.reverseHexInt(srcval);
                                } catch (java.lang.NumberFormatException e) { // todo 测试代码，不应该在生产环境运行到这里
                                    log.error("fatal!! 4 bytes (bin) number: {} - {}", tarfield.getName(), srcval);
                                    tarval = 0;
                                }
                            }
                        } else if (tarFieldName.contains("ime")) {
                            tarval = DateUtil.format(cp56toDate(srcval), NORM_DATETIME_FORMAT);
                        } else if (tarFieldName.startsWith("vin")) {
                            tarval = HexUtils.toAscii(srcval);
                        } else {
                            tarval = srcval;
                        }
                        tarclz.getMethod("set".concat(capitalize(tarFieldName)), tarfield.getType()).invoke(tarobj, tarval);
                    } catch (NoSuchMethodException ignored) {
                        log.warn(" src[{}], tar[{}] NoSuchMethodException {}", tarclz.getName(),
                                srcclz.getName(), "set".concat(capitalize(tarFieldName)));
                    }
                }
            }
        }
        return tarobj;
    }

    public static void hori2(String pileNo, String gunId) {

        String hori = "gun:".concat(pileNo).concat(gunId).concat(".hori");
        REDIS.setCacheObject(hori, "protection film", 300L, TimeUnit.MILLISECONDS);
    }

    public static boolean hori(String gunkey) {

        String hori = gunkey.concat(".hori");
        String HORI = REDIS.getCacheObject(hori);
        if (HORI != null) {
            return true;
        }
        REDIS.setCacheObject(hori, "protection film", 500L, TimeUnit.MILLISECONDS);
        return false;
    }

    public static void main(String[] args) {
        System.out.println("====hori==="+"gun:8083600030000101".concat(".hori"));
    }
}
