using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Fuel.Infrastructure.Extension
{
    public class RedisListOptions : IOptions<RedisListOptions>
    {
        public List<RedisConfig> RedisConfigs { get; set; }

        public RedisListOptions Value => this;

        public RedisListOptions()
        {
            RedisConfigs = new List<RedisConfig>();
        }

        /// <summary>
        /// 添加的redis 
        /// </summary>
        /// <param name="redisConfig"></param>
        public void TryAdd(RedisConfig redisConfig)
        {
            if (!RedisConfigs.Any(p => p.Connection == redisConfig.Connection && p.DataBase== redisConfig.DataBase))
            {
                RedisConfigs.Add(redisConfig);
            }
        }

        public void Set(RedisConfig redisConfig)
        {
            if (!RedisConfigs.Any(p => p.Connection == redisConfig.Connection && p.DataBase == redisConfig.DataBase))
            {
                RedisConfigs.Remove(redisConfig);
            }
            RedisConfigs.Add(redisConfig);
        }

    }


    /// <summary>
    /// redis config 配置
    /// </summary>
    public class RedisConfig
    {
        /// <summary>
        /// url 地址
        /// </summary>
        public string Connection { get; set; }

        /// <summary>
        /// 数据库编号
        /// </summary>
        public int DataBase { get; set; }

        /// <summary>
        /// 链接名称
        /// </summary>
        public string KeyName { get; set; }

        /// <summary>
        /// 缓存key 的前缀
        /// </summary>
        public string PrefixKey { get; set; }

        /// <summary>
        /// 哨兵地址
        /// </summary>
        public string[] Sentinels {get;set;}
    }
}
