package com.fufan.mtsaas.config;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.redisson.Redisson;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@Slf4j
@Component
public class RedissonCommand {

  @Value("${spring.redis.host}")
  private String host;
  @Value("${spring.redis.port}")
  private String port;
  @Value("${spring.redis.password}")
  private String password;
  private RedissonClient redissonClient;

  public RLock getFairLock(String key){
    try {
      if(StringUtils.isNotEmpty(key)){
        return getRedissonClient().getFairLock(key);
      }
    }catch (Exception e){

    }
    return null;
  }


  public boolean tryLock(RLock rLock){
    boolean lock;
    try {
      lock = rLock.tryLock(10,10,TimeUnit.SECONDS);
    }catch (Exception e){
      lock = false;
    }
    return lock;
  }

  private synchronized RedissonClient getRedissonClient(){
    if(redissonClient == null){
      Config config = new Config();
      config.useSingleServer().setDnsMonitoringInterval(10);
      config.useSingleServer().setAddress("redis://" + host + ":" + port);
      config.useSingleServer().setPassword(password);
      // 2. Create Redisson instance
      redissonClient = Redisson.create(config);
    }
    return redissonClient;
  }


  @Bean
  private  RedissonClient initRedissonClient() {
    // 1. Create config object
    Config config = new Config();
    config.useSingleServer().setDnsMonitoringInterval(1000000);
    config.useSingleServer().setAddress("redis://" + host + ":" + port);
    config.useSingleServer().setPassword(password);
    // 2. Create Redisson instance
    redissonClient = Redisson.create(config);
    return redissonClient;
  }
//
//    private static void lock() {
//        RLock lock1 = redissonClient.getLock(KEY_LOCKED);
//        log.error("lock1 clas: {}", lock1.getClass());
//        lock1.lock();
//        log.info("lock, ThreadName: {} id: {} locked, 重入次数: {}", Thread.currentThread().getName(), Thread.currentThread().getId(), lock1.getHoldCount());
//
//        // 处理业务逻辑
//        try {
//            Thread.sleep(TIME_LOCKED);
//            reLock();
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        } finally {
//            lock1.unlock();
//            log.info("lock, ThreadName: {} id: {} unlock, 重入次数: {}", Thread.currentThread().getName(), Thread.currentThread().getId(), lock1.getHoldCount());
//        }
//    }
//
//    /**
//     * 测试锁的重入
//     */
//    private static void reLock() {
//        RLock lock1 = redissonClient.getLock(KEY_LOCKED);
//        lock1.lock();
//        log.info("reLock, ThreadName: {} id: {} locked, 重入次数: {}", Thread.currentThread().getName(), Thread.currentThread().getId(), lock1.getHoldCount());
//
//        // 处理业务逻辑
//        try {
//            Thread.sleep(TIME_LOCKED);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        } finally {
//            lock1.unlock();
//            log.info("reLock, ThreadName: {} id: {} unlock, 重入次数: {}", Thread.currentThread().getName(), Thread.currentThread().getId(), lock1.getHoldCount());
//        }
//    }
}