package com.bonus.core.order.common.constants;

import cn.hutool.core.collection.CollUtil;
import com.bonus.core.common.converter.LeExcelFieldConvertor;
import com.bonus.core.common.converter.LeExcelFieldEnum;
import com.bonus.core.pay.common.constants.PayStateEnum;

import java.util.*;

public enum OrderStateMixEnum implements LeExcelFieldEnum<OrderStateMixEnum> {
   PAID(1, "已支付"),
   CANCEL(9, "已取消"),
   REFUND(10, "已退单"),
   COMPLETE(11, "已完成"),
   UN_PAY(22, "未支付"),
   PART_PAY(23, "部分支付"),
   PAY_FAILED(24, "支付失败"),
   PART_REFUND(25, "部分退单"),
   PAYING(26, "支付中");

   private final Integer key;
   private final String desc;

   private OrderStateMixEnum(Integer key, String desc) {
      this.key = key;
      this.desc = desc;
   }

   public static String getDesc(Integer key) {
      OrderStateMixEnum[] var1 = values();
      int var2 = var1.length;

      for(int var3 = 0; var3 < var2; ++var3) {
         OrderStateMixEnum temp = var1[var3];
         if (temp.getKey().equals(key)) {
            return temp.getDesc();
         }
      }

      return "";
   }

   public static List<Map<String, Object>> getAllEnumsList() {
      OrderStateMixEnum[] typeEnums = values();
      List<Map<String, Object>> typeList = new ArrayList();
      OrderStateMixEnum[] var2 = typeEnums;
      int var3 = typeEnums.length;

      for(int var4 = 0; var4 < var3; ++var4) {
         OrderStateMixEnum typeEnum = var2[var4];
         Map<String, Object> typeMap = new HashMap();
         typeMap.put("key", typeEnum.getKey());
         typeMap.put("value", typeEnum.getDesc());
         typeList.add(typeMap);
      }

      return typeList;
   }

   public static Integer mixStateBy(Integer orderState, Integer payState, Integer orderRefundState) {
      if (OrderRefundStateEnum.FINISH.getKey().equals(orderRefundState)) {
         return REFUND.getKey();
      } else if (OrderStateEnum.CANCEL.getKey().equals(orderState)) {
         return CANCEL.getKey();
      } else if (PayStateEnum.isFailedState(payState)) {
         return PAY_FAILED.getKey();
      } else if (PayStateEnum.isPayingState(payState)) {
         return PAYING.getKey();
      } else if (PayStateEnum.isUnPayState(payState)) {
         return UN_PAY.getKey();
      } else if (OrderRefundStateEnum.PART_SUCC.getKey().equals(orderRefundState)) {
         return PART_REFUND.getKey();
      } else if (PayStateEnum.PART_PAY.getKey().equals(payState)) {
         return PART_PAY.getKey();
      } else if (OrderStateEnum.FINISH.getKey().equals(orderState)) {
         return COMPLETE.getKey();
      } else {
         return PayStateEnum.PAY_SUCC.getKey().equals(payState) ? PAID.getKey() : UN_PAY.getKey();
      }
   }

   public static List<Integer> convertToOrderStateList(List<Integer> orderStateMixList) {
      Set<Integer> results = CollUtil.newHashSet(new Integer[0]);
      if (CollUtil.isEmpty(orderStateMixList)) {
         return CollUtil.newArrayList(results);
      } else {
         if (orderStateMixList.contains(PAID.getKey()) || orderStateMixList.contains(UN_PAY.getKey()) || orderStateMixList.contains(PART_PAY.getKey()) || orderStateMixList.contains(PART_REFUND.getKey()) || orderStateMixList.contains(PAYING.getKey())) {
            results.add(OrderStateEnum.PLACE.getKey());
         }

         if (orderStateMixList.contains(COMPLETE.getKey()) || orderStateMixList.contains(PART_PAY.getKey()) || orderStateMixList.contains(PART_REFUND.getKey())) {
            results.add(OrderStateEnum.FINISH.getKey());
         }

         if (orderStateMixList.contains(CANCEL.getKey()) || orderStateMixList.contains(REFUND.getKey())) {
            results.add(OrderStateEnum.CANCEL.getKey());
         }

         if (orderStateMixList.contains(UN_PAY.getKey()) || orderStateMixList.contains(PAY_FAILED.getKey()) || orderStateMixList.contains(PAYING.getKey())) {
            results.add(OrderStateEnum.WAIT_PLACE.getKey());
         }

         return CollUtil.newArrayList(results);
      }
   }

   public static List<Integer> convertToPayStateList(List<Integer> orderStateMixList) {
      Set<Integer> results = CollUtil.newHashSet(new Integer[0]);
      if (CollUtil.isEmpty(orderStateMixList)) {
         return CollUtil.newArrayList(results);
      } else {
         if (orderStateMixList.contains(CANCEL.getKey()) || orderStateMixList.contains(UN_PAY.getKey())) {
            results.add(PayStateEnum.UN_PAY.getKey());
         }

         if (orderStateMixList.contains(PAYING.getKey())) {
            results.add(PayStateEnum.PAY_INPROCESS.getKey());
         }

         if (orderStateMixList.contains(PAID.getKey()) || orderStateMixList.contains(REFUND.getKey()) || orderStateMixList.contains(PART_REFUND.getKey())) {
            results.add(PayStateEnum.PAY_SUCC.getKey());
         }

         if (orderStateMixList.contains(PAY_FAILED.getKey())) {
            results.add(PayStateEnum.PAY_FAIL.getKey());
         }

         if (orderStateMixList.contains(CANCEL.getKey()) || orderStateMixList.contains(UN_PAY.getKey())) {
            results.add(PayStateEnum.PAY_CLOSE.getKey());
         }

         if (orderStateMixList.contains(PART_PAY.getKey()) || orderStateMixList.contains(REFUND.getKey()) || orderStateMixList.contains(PART_REFUND.getKey())) {
            results.add(PayStateEnum.PART_PAY.getKey());
         }

         return CollUtil.newArrayList(results);
      }
   }

   public static List<Integer> convertToOrderRefundStateList(List<Integer> orderStateMixList) {
      Set<Integer> results = CollUtil.newHashSet(new Integer[0]);
      if (CollUtil.isEmpty(orderStateMixList)) {
         return CollUtil.newArrayList(results);
      } else {
         if (orderStateMixList.contains(CANCEL.getKey()) || orderStateMixList.contains(COMPLETE.getKey()) || orderStateMixList.contains(PAID.getKey()) || orderStateMixList.contains(PART_PAY.getKey()) || orderStateMixList.contains(PAY_FAILED.getKey()) || orderStateMixList.contains(PAYING.getKey()) || orderStateMixList.contains(UN_PAY.getKey())) {
            results.add(OrderRefundStateEnum.UN_APPLY.getKey());
         }

         if (orderStateMixList.contains(REFUND.getKey())) {
            results.add(OrderRefundStateEnum.FINISH.getKey());
         }

         if (orderStateMixList.contains(PART_REFUND.getKey())) {
            results.add(OrderRefundStateEnum.PART_SUCC.getKey());
         }

         return CollUtil.newArrayList(results);
      }
   }

   public Integer getKey() {
      return this.key;
   }

   public String getDesc() {
      return this.desc;
   }

   public static class ExcelConvertor extends LeExcelFieldConvertor<OrderStateMixEnum> {
      public ExcelConvertor() {
         super(OrderStateMixEnum.class);
      }
   }
}
