package com.bonus.canteen.core.common.encrypt;

import cn.hutool.core.util.DesensitizedUtil;
import cn.hutool.core.util.DesensitizedUtil.DesensitizedType;
import com.bonus.canteen.core.allocation.alloc.model.DataManageMetadataModel;
import com.bonus.canteen.core.allocation.api.AllocMetadataApi;
import com.bonus.canteen.core.common.enums.MetadataModelTypeEnum;
import com.github.pagehelper.Page;
import com.github.pagehelper.page.PageMethod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.Iterator;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Component
public class LeNiuDataHandler {
   @Autowired
   @Lazy
   private AllocMetadataApi allocMetadataApi;

   public <T> T resultProcess(T result) throws IllegalAccessException {
      Class<?> resultClass = result.getClass();
      Class<?> superclass = resultClass.getSuperclass();
      Field[] declaredFields = resultClass.getDeclaredFields();
      Field[] superFields = superclass.getDeclaredFields();
      Field[] allFields = (Field[])Stream.concat(Arrays.stream(declaredFields), Arrays.stream(superFields)).toArray((x$0) -> {
         return new Field[x$0];
      });
      Set<Field> decryptField = this.getProcessField(allFields);
      Page localPage = PageMethod.getLocalPage();
      PageMethod.clearPage();
      DataManageMetadataModel metadata = (DataManageMetadataModel)this.allocMetadataApi.queryMetadataModel(MetadataModelTypeEnum.MERCHANT, new DataManageMetadataModel());
      if (Objects.nonNull(localPage)) {
         PageMethod.setLocalPage(localPage);
      }

      Iterator var10 = decryptField.iterator();

      while(var10.hasNext()) {
         Field field = (Field)var10.next();
         Object object = field.get(result);
         if (object instanceof String) {
             String dbValue = (String) object;
             String decrypted;
            if (((LeNiuDecryptField)field.getAnnotation(LeNiuDecryptField.class)).value().equals(HandleType.DECRYPT)) {
               decrypted = SM4EncryptUtils.sm4Decrypt(dbValue);
               field.set(result, decrypted);
            } else {
               decrypted = SM4EncryptUtils.sm4Decrypt(dbValue);
               if (metadata != null && "1".equals(metadata.getPhoneDesensitization())) {
                  String name = ((LeNiuDecryptField)field.getAnnotation(LeNiuDecryptField.class)).value().name();
                  String type = name.replace("DESENSITIZE_", "");
                  DesensitizedType desensitizedType = DesensitizedType.valueOf(type);
                  decrypted = DesensitizedUtil.desensitized(decrypted, desensitizedType);
               }

               field.set(result, decrypted);
            }
         }
      }

      return result;
   }

   protected Set<Field> getProcessField(Field[] declaredFields) {
      Set<Field> fieldSet = (Set)Arrays.stream(declaredFields).filter((fieldx) -> {
         return fieldx.isAnnotationPresent(LeNiuDecryptField.class) && fieldx.getType() == String.class;
      }).collect(Collectors.toSet());
      Iterator var3 = fieldSet.iterator();

      while(var3.hasNext()) {
         Field field = (Field)var3.next();
         field.setAccessible(true);
      }

      return fieldSet;
   }
}
