package com.zm.order.tool.server.cipher;

import cn.qmai.data.encrypt.service.DataEncryptService;
import com.zm.order.tool.server.model.Order;
import com.zm.order.tool.server.utils.JacksonUtils;
import net.sf.jsqlparser.JSQLParserException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.Map;


/**
 * 退款单加解密处理器
 *
 * <p>
 *
 * </p>
 *
 * @author nianchang
 * @date 2023/12/19 5:06 下午
 */
@Component
public class OrderEncryptionHandler extends AbstractEncryptionHandler {

    @Autowired
    private DataEncryptService dataEncryptService;

    /**
     * 加密过程
     *
     * <p>
     *
     * </p>
     *
     * @param order datasource
     */
    @SuppressWarnings("unchecked")
    public void encrypt(Order order) {
        if (order == null) {
            return;
        }
        if (!StringUtils.isEmpty(order.getMobile())) {
            try {
                order.setMobileCipher(dataEncryptService.encrypt(order.getMobile()));
            } catch (JSQLParserException e) {
                e.printStackTrace();
            }
        }
//        Map<String, Object> extraMap = order.getExtraMap();
//        if (!CollectionUtils.isEmpty(extraMap)) {
//            order.setExtraMap(handleExtEncrypt(extraMap));
//        }
//        if (!StringUtils.isEmpty(order.getPerformanceInfo())) {
//            Map<String, Object> perfMap = JacksonUtils.getObjectFromJsonString(order.getPerformanceInfo(), Map.class);
//            order.setPerformanceInfo(handlePerfInfoEncrypt(perfMap));
//        }
    }

    private Map<String, Object> handleExtEncrypt(Map<String, Object> extraMap) {
        ext.forEach((key, value) -> parseExt(extraMap, key, value, CipherEnum.ENCRYPT));
        return extraMap;
    }

    private String handlePerfInfoEncrypt(Map<String, Object> performanceMap) {
        Map result = lastRank(perfList, performanceMap, CipherEnum.ENCRYPT);
        return JacksonUtils.toJsonString(result);
    }

    /**
     * 解密过程
     *
     * <p>
     *
     * </p>
     *
     * @param order datasource
     */
    @SuppressWarnings("unchecked")
    public void decrypt(Order order) {
        if (order == null) {
            return;
        }
        Map<String, Object> extraMap = order.getExtraMap();
        if (!CollectionUtils.isEmpty(extraMap)) {
            order.setExtraMap(handleExtDecrypt(extraMap));
        }
        if (!StringUtils.isEmpty(order.getPerformanceInfo())) {
            Map<String, Object> perfMap = JacksonUtils.getObjectFromJsonString(order.getPerformanceInfo(), Map.class);
            order.setPerformanceInfo(handlePerfInfoDecrypt(perfMap));
        }
    }

    private Map<String, Object> handleExtDecrypt(Map<String, Object> extraMap) {
        ext.forEach((key, value) -> parseExt(extraMap, key, value, CipherEnum.DECRYPT));
        return extraMap;
    }

    private String handlePerfInfoDecrypt(Map<String, Object> performanceMap) {
        Map result = lastRank(perfList, performanceMap, CipherEnum.DECRYPT);
        return JacksonUtils.toJsonString(result);
    }
}

