package com.mosty.common.base.autoconfig;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import org.apache.commons.lang3.StringUtils;

import java.io.IOException;

/**
 *  脱敏类
 * @author kevin
 * @date 2022/3/8 11:10 PM
 * @since 1.0.0
 */
@SuppressWarnings("unused")
public class Desensitization {

    /**
     *  手机类型脱敏
     * @author kevin
     * @date 2022/3/8 11:13 PM
     * @since 1.0.0
     */
    public static class Phone extends JsonSerializer<String> {
        @Override
        public void serialize(String value, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
            if (StringUtils.isEmpty(value)) {
                jsonGenerator.writeObject(value);
                return;
            }
            String trim = value.trim();
            if (trim.length() == 11) {
                String phone = trim.replaceAll("(\\d{3})\\d*(\\d{4})", "$1****$2");
                jsonGenerator.writeObject(phone);
                return;
            }
            jsonGenerator.writeObject(value);
        }
    }

    /**
     *  银行卡号类型脱敏
     * @author kevin
     * @date 2022/3/8 11:13 PM
     * @since 1.0.0
     */
    public static class BankCard extends JsonSerializer<String> {

        @Override
        public void serialize(String value, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
            if (StringUtils.isEmpty(value)) {
                jsonGenerator.writeObject(value);
                return;
            }
            String trim = value.trim();
            String phone = trim.replaceAll("(\\d{6})\\d*(\\d{4})", "$1****$2");
            jsonGenerator.writeObject(phone);
        }
    }

    /**
     *  身份证号类型脱敏
     * @author kevin
     * @date 2022/3/8 11:13 PM
     * @since 1.0.0
     */
    public static class IdentityCard extends JsonSerializer<String> {

        @Override
        public void serialize(String value, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
            if (StringUtils.isEmpty(value)) {
                jsonGenerator.writeObject(value);
                return;
            }
            String trim = value.trim();
            String phone = trim.replaceAll("(\\d{6})\\d*(\\w{4})", "$1****$2");
            jsonGenerator.writeObject(phone);
        }
    }

    /**
     *  姓名类型脱敏
     * @author kevin
     * @date 2022/3/8 11:13 PM
     * @since 1.0.0
     */
    public static class FullName extends JsonSerializer<String> {

        @Override
        public void serialize(String value, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
            if (StringUtils.isEmpty(value) || value.length() == 1 || value.trim().length() == 1) {
                jsonGenerator.writeObject(value);
                return;
            }
            String trim = value.trim();
            if (trim.length() == 2) {
                jsonGenerator.writeObject(value.charAt(0) + "*");
                return;
            }
            StringBuilder stringBuffer = new StringBuilder();
            for (int i = 0; i < value.length(); i++) {
                if (i == 0 || i == value.length() - 1) {
                    stringBuffer.append(value.charAt(i));
                } else {
                    stringBuffer.append("*");
                }
            }
            jsonGenerator.writeObject(stringBuffer.toString());
        }
    }

}
