package com.bonus.core.order.utils;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.NumberUtil;

import java.math.BigDecimal;
import java.time.*;
import java.util.Collection;
import java.util.List;
import java.util.function.BiConsumer;
import java.util.stream.Collectors;

public class LeOrderUtil {
   private static final List<LocalDate> DEFAULT_DATE_RANGE = CollUtil.newArrayList(new LocalDate[]{LocalDate.now().minusYears(1L), LocalDate.now().plusYears(1L)});

//   public static String getDetailDescStr(String goodsDishesName, Integer salesMode, Integer quantity, String unitName, Integer orderType) {
//      StringBuilder sb = new StringBuilder();
//      if (OrderTypeEnum.isShopCategory(orderType)) {
//         sb.append(goodsDishesName).append("*");
//         if (MenuSalesTypeEnum.WEIGHT_ON.key().equals(salesMode)) {
//            sb.append(NumberUtil.toBigDecimal(quantity).multiply(BigDecimal.valueOf(0.001))).append(CharSequenceUtil.isNotBlank(unitName) ? unitName : I18n.getMessage("order.mac.kg", new Object[0]));
//         } else {
//            sb.append(quantity).append(CharSequenceUtil.isNotBlank(unitName) ? unitName : "");
//         }
//      } else {
//         sb.append(goodsDishesName);
//         sb.append("*");
//         String defaultUnitName = MenuSalesTypeEnum.WEIGHT_ON.key().equals(salesMode) ? I18n.getMessage("order.mac.g", new Object[0]) : "";
//         sb.append(quantity).append(CharSequenceUtil.isNotBlank(unitName) ? unitName : defaultUnitName);
//      }
//
//      sb.append("；");
//      return sb.toString();
//   }
//
//   public static String getDetailDescStr(List<OrderDetailVO> orderDetailWebVOList, Integer orderType) {
//      return getDetailDescStr(orderDetailWebVOList, orderType, false);
//   }
//
//   public static String getDetailDescStr(List<OrderDetailVO> orderDetailWebVOList, Integer orderType, boolean filterRefundNum) {
//      return CollUtil.isEmpty(orderDetailWebVOList) ? "" : (String)orderDetailWebVOList.stream().filter((s) -> {
//         return !filterRefundNum || s.getQuantity() - s.getGoodsRefundNum() > 0;
//      }).map((s) -> {
//         return getDetailDescStr(s.getGoodsDishesName(), s.getSalesMode(), filterRefundNum ? s.getQuantity() - s.getGoodsRefundNum() : s.getQuantity(), s.getUnitName(), orderType);
//      }).collect(Collectors.joining());
//   }
//
//   public static String getDetailRefundDescStr(List<OrderDetailVO> orderDetailWebVOList, Integer orderType) {
//      return CollUtil.isEmpty(orderDetailWebVOList) ? "" : (String)orderDetailWebVOList.stream().filter((s) -> {
//         return s.getGoodsRefundNum() > 0;
//      }).map((s) -> {
//         return getDetailDescStr(s.getGoodsDishesName(), s.getSalesMode(), s.getGoodsRefundNum(), s.getUnitName(), orderType);
//      }).collect(Collectors.joining());
//   }
//
//   public static String getRefundDetailDescStr(List<OrderRefundDetailVO> orderRefundDetailVOList, Integer orderType, boolean realQuantity) {
//      return CollUtil.isEmpty(orderRefundDetailVOList) ? "" : (String)orderRefundDetailVOList.stream().map((s) -> {
//         return getDetailDescStr(s.getGoodsDishesName(), s.getSalesMode(), realQuantity ? s.getRealQuantity() : s.getQuantity(), s.getUnitName(), orderType);
//      }).collect(Collectors.joining());
//   }
//
//   public static String getRestoreQuantityDescStr(List<OrderRefundDetailVO> orderRefundDetailVOList, Integer orderType) {
//      return CollUtil.isEmpty(orderRefundDetailVOList) ? "" : (String)orderRefundDetailVOList.stream().filter((s) -> {
//         return s.getRestoreQuantity() > 0;
//      }).map((s) -> {
//         return getDetailDescStr(s.getGoodsDishesName(), s.getSalesMode(), s.getRestoreQuantity(), s.getUnitName(), orderType);
//      }).collect(Collectors.joining());
//   }
//
//   public static String getPayTypesDescStr(List<OrderAmountDetail> amountDetailVOList, Integer defaultPayType, boolean isPartPay) {
//      String str;
//      if (CollUtil.isEmpty(amountDetailVOList)) {
//         str = PayTypeEnum.getDesc(defaultPayType);
//      } else if (CollUtil.size(amountDetailVOList) != 1 && !amountDetailVOList.stream().allMatch((s) -> {
//         return OrderAmountDetailTypeEnum.PAY.getKey().equals(s.getAmountDetailType()) && s.getAmount().compareTo(BigDecimal.ZERO) == 0;
//      })) {
//         str = (String)amountDetailVOList.stream().filter((s) -> {
//            return OrderAmountDetailTypeEnum.PAY.getKey().equals(s.getAmountDetailType()) && s.getAmount().compareTo(BigDecimal.ZERO) > 0;
//         }).map((s) -> {
//            return PayTypeEnum.getDesc(s.getPayType());
//         }).collect(Collectors.joining("+"));
//      } else {
//         str = PayTypeEnum.getDesc(((OrderAmountDetail)amountDetailVOList.getLast()).getPayType());
//      }
//
//      return isPartPay ? str + "（异常）" : str;
//   }
//
//   public static String getPayTypesDescStrByChannel(List<TradeChannelVO> tradeChannelVOList, Integer defaultPayType) {
//      return CollUtil.isEmpty(tradeChannelVOList) ? PayTypeEnum.getDesc(defaultPayType) : (String)tradeChannelVOList.stream().map((s) -> {
//         return PayTypeEnum.getDesc(s.getPayType());
//      }).collect(Collectors.joining("+"));
//   }
//
//   public static void calcOrderAmount(List<OrderInfo> orderInfoList, BigDecimal totalAmount, BiConsumer<OrderInfo, BigDecimal> saveField) {
//      LeNumUtil.calcShareAmount(orderInfoList, totalAmount, OrderInfo::calcNeedPayAmount, saveField);
//   }
//
//   public static boolean checkNowTimeWithin(String startTimeStr, String endTimeStr) {
//      LocalTime startTime = null;
//      LocalTime endTime = null;
//      if (CharSequenceUtil.isNotBlank(startTimeStr)) {
//         startTime = LocalTime.parse(startTimeStr);
//      }
//
//      if (CharSequenceUtil.isNotBlank(endTimeStr)) {
//         endTime = LocalTime.parse(endTimeStr);
//      }
//
//      return checkNowTimeWithin(startTime, endTime);
//   }
//
//   public static boolean checkNowTimeWithin(LocalTime startTime, LocalTime endTime) {
//      LocalTime now = LocalTime.now();
//      if (startTime != null && endTime != null && startTime.isAfter(endTime)) {
//         return now.isAfter(startTime) && now.isBefore(endTime);
//      } else if (startTime != null && now.isBefore(startTime)) {
//         return false;
//      } else {
//         return endTime == null || !now.isAfter(endTime);
//      }
//   }

   public static List<LocalDate> queryDateRange() {
      return DEFAULT_DATE_RANGE;
   }

//   public static List<LocalDateTime> queryCreateTimeRange() {
//      return (List)DEFAULT_DATE_RANGE.stream().map(LocalDate::atStartOfDay).collect(Collectors.toList());
//   }

   public static List<LocalDate> queryDateRange(Long orderId, Integer... days) {
      if (!LeNumUtil.isValidId(orderId)) {
         return DEFAULT_DATE_RANGE;
      } else {
         long timestamp = LeNumUtil.timestampOfId(orderId);
         LocalDate date = LocalDateTime.ofInstant(Instant.ofEpochMilli(timestamp), ZoneId.systemDefault()).toLocalDate();
         return CollUtil.newArrayList(new LocalDate[]{date.minusDays(ArrayUtil.length(days) > 0 ? (long)days[0] : 365L), date.plusDays(ArrayUtil.length(days) > 1 ? (long)days[1] : 365L)});
      }
   }

   public static List<LocalDate> queryDateRange(Collection<Long> orderIds) {
      return queryDateRange((Collection)orderIds, (Collection)null);
   }

   public static List<LocalDate> queryDateRange(Collection<Long> orderIds, Collection<LocalDate> orderDates) {
      if (CollUtil.isEmpty(orderIds) && CollUtil.isEmpty(orderDates)) {
         return DEFAULT_DATE_RANGE;
      } else if (CollUtil.isNotEmpty(orderDates)) {
         return CollUtil.toList(new LocalDate[]{(LocalDate)orderDates.stream().min(LocalDate::compareTo).get(), (LocalDate)orderDates.stream().max(LocalDate::compareTo).get()});
      } else {
         List<LocalDate> dateList = (List)orderIds.stream().flatMap((s) -> {
            return queryDateRange(s).stream();
         }).collect(Collectors.toList());
         return CollUtil.newArrayList(new LocalDate[]{(LocalDate)dateList.stream().min(LocalDate::compareTo).get(), (LocalDate)dateList.stream().max(LocalDate::compareTo).get()});
      }
   }

   public static boolean checkNowTimeWithin(String startTimeStr, String endTimeStr) {
      LocalTime startTime = null;
      LocalTime endTime = null;
      if (CharSequenceUtil.isNotBlank(startTimeStr)) {
         startTime = LocalTime.parse(startTimeStr);
      }

      if (CharSequenceUtil.isNotBlank(endTimeStr)) {
         endTime = LocalTime.parse(endTimeStr);
      }

      return checkNowTimeWithin(startTime, endTime);
   }
   public static boolean checkNowTimeWithin(LocalTime startTime, LocalTime endTime) {
      LocalTime now = LocalTime.now();
      if (startTime != null && endTime != null && startTime.isAfter(endTime)) {
         return now.isAfter(startTime) && now.isBefore(endTime);
      } else if (startTime != null && now.isBefore(startTime)) {
         return false;
      } else {
         return endTime == null || !now.isAfter(endTime);
      }
   }
   public static List<LocalDateTime> queryCreateTimeRange() {
      return (List)DEFAULT_DATE_RANGE.stream().map(LocalDate::atStartOfDay).collect(Collectors.toList());
   }
   public static List<LocalDateTime> queryCreateTimeRange(Long id, Integer... days) {
      if (!LeNumUtil.isValidId(id)) {
         return queryCreateTimeRange();
      } else {
         long timestamp = LeNumUtil.timestampOfId(id);
         LocalDateTime dateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(timestamp), ZoneId.systemDefault());
         return CollUtil.newArrayList(new LocalDateTime[]{dateTime.minusDays(ArrayUtil.length(days) > 0 ? (long)days[0] : 1L), dateTime.plusDays(ArrayUtil.length(days) > 1 ? (long)days[1] : 1L)});
      }
   }
   public static List<LocalDateTime> queryCreateTimeRange(Collection<Long> ids) {
      if (!CollUtil.isEmpty(ids)) {
         return queryCreateTimeRange();
      } else {
         List<LocalDateTime> dateTimeList = (List)ids.stream().flatMap((s) -> {
            return queryCreateTimeRange(s).stream();
         }).collect(Collectors.toList());
         return CollUtil.newArrayList(new LocalDateTime[]{(LocalDateTime)dateTimeList.stream().min(LocalDateTime::compareTo).get(), (LocalDateTime)dateTimeList.stream().max(LocalDateTime::compareTo).get()});
      }
   }
//
//   public static List<LocalDateTime> queryCreateTimeRange(Long id, Integer... days) {
//      if (!LeNumUtil.isValidId(id)) {
//         return queryCreateTimeRange();
//      } else {
//         long timestamp = LeNumUtil.timestampOfId(id);
//         LocalDateTime dateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(timestamp), ZoneId.systemDefault());
//         return CollUtil.newArrayList(new LocalDateTime[]{dateTime.minusDays(ArrayUtil.length(days) > 0 ? (long)days[0] : 1L), dateTime.plusDays(ArrayUtil.length(days) > 1 ? (long)days[1] : 1L)});
//      }
//   }
//
//   public static List<LocalDateTime> queryCreateTimeRange(Collection<Long> ids) {
//      if (!CollUtil.isEmpty(ids)) {
//         return queryCreateTimeRange();
//      } else {
//         List<LocalDateTime> dateTimeList = (List)ids.stream().flatMap((s) -> {
//            return queryCreateTimeRange(s).stream();
//         }).collect(Collectors.toList());
//         return CollUtil.newArrayList(new LocalDateTime[]{(LocalDateTime)dateTimeList.stream().min(LocalDateTime::compareTo).get(), (LocalDateTime)dateTimeList.stream().max(LocalDateTime::compareTo).get()});
//      }
//   }
}
