import cn.reghao.tnb.admin.AdminApplication;
import cn.reghao.tnb.admin.db.mongo.TnbCommentMongo;
import cn.reghao.tnb.admin.model.po.TnbComment;
import io.lettuce.core.RedisClient;
import io.lettuce.core.RedisURI;
import io.lettuce.core.api.StatefulRedisConnection;
import io.lettuce.core.api.sync.RedisCommands;
import io.lettuce.core.sentinel.api.StatefulRedisSentinelConnection;
import io.lettuce.core.sentinel.api.sync.RedisSentinelCommands;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.env.Environment;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisSentinelConnection;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.test.context.ActiveProfiles;

import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author reghao
 * @date 2026-01-23 14:31:47
 */
@Slf4j
@ActiveProfiles("dev")
@SpringBootTest(classes = AdminApplication.class)
public class RedisTest {
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Qualifier("lettuceConnectionFactory")
    RedisConnectionFactory connectionFactory;

    @Test
    void test() throws InterruptedException {
        final String key = "hao";
        String value0 = "1234567890";
        Boolean flag = stringRedisTemplate.opsForValue().setIfAbsent(key, value0, 600, TimeUnit.SECONDS);
        log.info("flag: {}", flag);

        while (!Thread.interrupted()) {
            String value = stringRedisTemplate.opsForValue().get(key);
            log.info("value: {}", value);

            Long expire = stringRedisTemplate.getExpire(key, TimeUnit.SECONDS);
            log.info("expire: {}", expire);

            Thread.sleep(3000);
        }
    }

    @Test
    public void connTest() {
        String masterId = "mymaster";
        RedisURI redisUri = RedisURI.Builder
                .sentinel("192.168.0.214", 26379, masterId, "Test_123456")
                .withSentinel("192.168.0.215")
                .withSentinel("192.168.0.216")
                .withPassword("Test_123456".toCharArray())
                .build();
        RedisClient redisClient = RedisClient.create(redisUri);

        StatefulRedisConnection<String, String> redisConn = redisClient.connect();
        System.out.println("Connected to Redis using Redis Sentinel");
        RedisCommands<String, String> redisCommands = redisConn.sync();

        // 包括 Replication 信息
        String redisInfo = redisCommands.info();

        StatefulRedisSentinelConnection<String, String> sentinelConn =  redisClient.connectSentinel();
        RedisSentinelCommands<String, String> sentinelCommands = sentinelConn.sync();
        // 包括 sentinel 的所有 master
        List<Map<String, String>> allMastersInfo = sentinelCommands.masters();
        Map<String, String> masterInfo = sentinelCommands.master(masterId);

        //connection.close();
        redisClient.shutdown();
    }

    @Autowired
    Environment environment;
    @Test
    public void zkTest() {
        String address = environment.getProperty("dubbo.registry.address");
        String connectString = address.replace("zookeeper://", "").replace("?backup=", ",");
    }

    @Autowired
    TnbCommentMongo tnbCommentMongo;
    @Test
    public void mongoTest() {
        TnbComment tnbComment = new TnbComment();
        tnbComment.setContent("halo world");
        tnbCommentMongo.save(tnbComment);

        List<TnbComment> list = tnbCommentMongo.findAll();
        System.out.println();
    }
}
