package com.bonus.canteen.core.customer.utils;

import com.bonus.canteen.core.common.utils.TenantContextHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;

@Component
public class CustomerUtil {
   private static CustomerUtil customerUtil;
//   @Autowired
//   private MenuDishesApi menuDishesApi;

//   @PostConstruct
//   private void init() {
//      customerUtil = this;
//      customerUtil.menuDishesApi = this.menuDishesApi;
//   }

   public static Integer getUsableCustLimitId() {
      return -1;
   }

   public static String encoderPwd(String pwd) {
      return (new BCryptPasswordEncoder()).encode(pwd);
   }

   public static void customerUpgradeLock(String lockKey, Integer waitTime) {
      String redisLockKey = "yst:customer:upgrade:lock:" + TenantContextHolder.getTenantId();
      if (null != lockKey) {
         redisLockKey = redisLockKey + ":" + lockKey;
      }

//      boolean lock = RedisUtil.tryLock(redisLockKey, waitTime, 3600);
//      if (!lock) {
//         throw new LeException(I18n.getMessage("customer.common.inExecution.wait", new Object[0]));
//      }
   }

   public static void customerUpgradeUnLock(String lockKey) {
      String redisLockKey = "yst:customer:upgrade:lock:" + TenantContextHolder.getTenantId();
      if (null != lockKey) {
         redisLockKey = redisLockKey + ":" + lockKey;
      }

     // RedisUtil.safeUnLock(redisLockKey);
   }

   public static LocalDate getExpireDate(Integer expirDay) {
      return expirDay != null && expirDay > 0 ? LocalDate.now().plusDays((long)expirDay) : null;
   }

   public static String getRemainingDays(LocalDate expireDate) {
      if (expireDate == null) {
         return "永久有效";
      } else {
         long days = ChronoUnit.DAYS.between(LocalDate.now(), expireDate);
         return days <= 0L ? "已过期" : String.valueOf(days);
      }
   }
}
