package com.bonus.core.allocation.util;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import com.bonus.core.allocation.alloc.model.AllocMetadata;
import com.bonus.core.allocation.alloc.model.MetadataModel;
import com.bonus.core.common.utils.SysUtil;
import com.bonus.core.allocation.canteen.dto.AllocCanteenDeliveryDTO;
import com.bonus.core.allocation.canteen.dto.AllocCanteenDeliveryModel;
import com.bonus.core.allocation.canteen.enums.AllocMinDeliveryFeeOnOffEnum;
import com.bonus.core.common.enums.MetadataModelTypeEnum;
import io.swagger.annotations.ApiModelProperty;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.*;
import java.util.stream.Collectors;

public class MetadataUtil {
   private static final Logger log = LoggerFactory.getLogger(MetadataUtil.class);
   private static final String URL = "url";
   private static final String PATH = "path";
   private static final String CERT = "cert";

   private MetadataUtil() {
   }

   public static <T> T transferToPayModel(List<AllocMetadata> payMetadataList, T t) {
      T t1 = (T) JSONUtil.toBean(JSONUtil.toJsonStr(t), t.getClass());
      if (CollUtil.isEmpty(payMetadataList)) {
         return t1;
      } else {
         Map<String, String> metadataMap = (Map)payMetadataList.stream().collect(Collectors.toMap((s) -> {
            return CharSequenceUtil.nullToEmpty(s.getModelKey());
         }, (s) -> {
            return CharSequenceUtil.nullToEmpty(s.getModelValue());
         }));
         BeanUtil.fillBeanWithMapIgnoreCase(metadataMap, t1, true);
         return t1;
      }
   }

   public static <T> Map<String, MetadataModel> getStringMetadataMap(T t) {
      Map<String, String> methodMap = new HashMap();
      Method[] methods = t.getClass().getMethods();
      Method[] var3 = methods;
      int var4 = methods.length;

      for(int var5 = 0; var5 < var4; ++var5) {
         Method method = var3[var5];
         String methodName = method.getName();
         if (methodName.startsWith("get") && !methodName.contains("Class") && !methodName.contains("AreaId")) {
            String modelKey = CharSequenceUtil.lowerFirst(methodName.substring(3));
            String modelValue = null;

            try {
               modelValue = String.valueOf(method.invoke(t, (Object[])null));
            } catch (InvocationTargetException | IllegalAccessException var13) {
               System.out.println(modelKey + ",数据转换时，未获取到对应方法");
            }

            if (CharSequenceUtil.isNotBlank(modelValue) && !modelValue.contains("null")) {
               methodMap.put(modelKey, modelValue);
            } else {
               methodMap.put(modelKey, "");
            }
         }
      }

      Map<String, String> fieldMap = new HashMap();
      Field[] declaredFields = t.getClass().getDeclaredFields();
      Field[] declaredSuperFields = t.getClass().getSuperclass().getDeclaredFields();
      List<Field> fieldList = new ArrayList(Arrays.asList(declaredFields));
      fieldList.addAll(Arrays.asList(declaredSuperFields));
      Iterator var18 = fieldList.iterator();

      while(var18.hasNext()) {
         Field field = (Field)var18.next();
         if (field.getAnnotation(ApiModelProperty.class) != null) {
            fieldMap.put(field.getName().toLowerCase(), ((ApiModelProperty)field.getAnnotation(ApiModelProperty.class)).value());
         }
      }

      Map<String, MetadataModel> metadataMap = new HashMap();
      Iterator var21 = methodMap.entrySet().iterator();

      while(var21.hasNext()) {
         Map.Entry<String, String> entry = (Map.Entry)var21.next();
         String filed = (String)entry.getKey();
         String lowerCaseField = filed.toLowerCase();
         if (fieldMap.containsKey(lowerCaseField)) {
            MetadataModel metadata = new MetadataModel();
            metadata.setModelKey(filed);
            metadata.setModelValue((String)entry.getValue());
            metadata.setModelKeyRemark(lowerCaseField);
            metadataMap.put(filed, metadata);
         }
      }

      return metadataMap;
   }
   public static String metadataPathFieldHandler(String modelKey, String modelValue, boolean getSetFlag) {
      if (CharSequenceUtil.isBlank(modelValue)) {
         return modelValue;
      } else {
         modelKey = modelKey.toLowerCase();
         if (!modelKey.contains("url") && !modelKey.contains("path")) {
            return modelValue;
         } else {
            return getSetFlag ? SysUtil.getCutFileUrl(modelValue) : SysUtil.getCutPath(modelValue);
         }
      }
   }

   public static AllocCanteenDeliveryModel meteDataListToDelivery(List<AllocMetadata> payMetadataList) {
      int ifEnableDelivery = 1;
      Integer minDeliveryFeeOnOff = AllocMinDeliveryFeeOnOffEnum.NONE.getKey();
      int wholeMinDeliveryFee = 0;
      List<AllocCanteenDeliveryModel.MealtimeMinDeliveryFeeDTO> mealtimeMinDeliveryFeeList = CollUtil.newArrayList(new AllocCanteenDeliveryModel.MealtimeMinDeliveryFeeDTO[0]);
      String deliveries = "";
      List<AllocCanteenDeliveryDTO> deliveryList = CollUtil.newArrayList(new AllocCanteenDeliveryDTO[0]);
      Iterator var7 = payMetadataList.iterator();

      while(var7.hasNext()) {
         AllocMetadata metadata = (AllocMetadata)var7.next();
         if (MetadataModelTypeEnum.DELIVERY.getKey().equals(metadata.getModelType())) {
            if ("ifEnableDelivery".equals(metadata.getModelKey())) {
               ifEnableDelivery = Integer.parseInt(metadata.getModelValue());
            } else if ("deliveries".equals(metadata.getModelKey())) {
               deliveries = metadata.getModelValue();
            } else if ("minDeliveryFeeOnOff".equals(metadata.getModelKey())) {
               minDeliveryFeeOnOff = Integer.parseInt(metadata.getModelValue());
            } else if ("wholeMinDeliveryFee".equals(metadata.getModelKey())) {
               wholeMinDeliveryFee = Integer.parseInt(metadata.getModelValue());
            } else if ("mealtimeMinDeliveryFee".equals(metadata.getModelKey())) {
               mealtimeMinDeliveryFeeList = JSONUtil.toList(metadata.getModelValue(), AllocCanteenDeliveryModel.MealtimeMinDeliveryFeeDTO.class);
            } else {
               AllocCanteenDeliveryDTO dto = (AllocCanteenDeliveryDTO)JSONUtil.toBean(metadata.getModelValue(), AllocCanteenDeliveryDTO.class);
               if (ObjectUtil.isNotNull(dto)) {
                  deliveryList.add(dto);
               }
            }
         }
      }

      return new AllocCanteenDeliveryModel(ifEnableDelivery, minDeliveryFeeOnOff, wholeMinDeliveryFee, (List)mealtimeMinDeliveryFeeList, deliveries, deliveryList);
   }
}
