using ServiceStack.Redis;
using System;
using System.Windows;

namespace DeskRedis
{
    /// <summary>
    /// WinInsert4Hash.xaml 的交互逻辑
    /// </summary>
    public partial class WinInsertValue : Window
    {
        #region 私有变量

        /// <summary>
        /// Redis配置Id。
        /// </summary>
        private readonly string configId;

        /// <summary>
        /// 数据库索引。
        /// </summary>
        private readonly int dbIndex;

        /// <summary>
        /// 键。
        /// </summary>
        private readonly string key;

        /// <summary>
        /// 数据类型。
        /// </summary>
        private readonly RedisKeyType type;

        #endregion


        #region 事件委托

        /// <summary>
        /// 当插入值成功时发生。
        /// <para>string:Redis配置Id。int:数据库索引。string:键。RedisKeyType:数据类型。</para>
        /// </summary>
        public event Action<string, int, string, RedisKeyType> OnValueInserted;

        #endregion


        #region 构造方法

        /// <summary>
        /// 使用指定的参数创建 DeskRedis.WinInsertValue 的实例。
        /// </summary>
        /// <param name="configId">Redis配置Id。</param>
        /// <param name="dbIndex">数据库索引。</param>
        /// <param name="key">键。</param>
        /// <param name="type">数据类型。</param>
        public WinInsertValue(string configId, int dbIndex, string key, RedisKeyType type)
        {
            InitializeComponent();

            this.configId = configId;
            this.dbIndex = dbIndex;
            this.key = key;
            this.type = type;

            switch (type)
            {
                case RedisKeyType.None:
                case RedisKeyType.String:
                    break;
                case RedisKeyType.List:
                case RedisKeyType.Set:
                    this.stackpanelScore.Visibility = Visibility.Collapsed;
                    this.stackpanelKey.Visibility = Visibility.Collapsed;
                    break;
                case RedisKeyType.SortedSet:
                    this.stackpanelKey.Visibility = Visibility.Collapsed;
                    break;
                case RedisKeyType.Hash:
                    this.stackpanelScore.Visibility = Visibility.Collapsed;
                    break;
            }
        }

        #endregion


        /// <summary>
        /// 当鼠标点击“添加”按钮时发生。
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnInsert_Click(object sender, RoutedEventArgs e)
        {
            string val = this.tbValue.Text.Trim();
            if (string.IsNullOrEmpty(val))
            {
                MessageBox.Show("请输入值!");
                return;
            }

            try
            {
                switch (this.type)
                {
                    case RedisKeyType.List:
                        GlobalBusiness.RedisCaches[this.configId]
                            .AddList(this.key, val, this.dbIndex);
                        break;
                    case RedisKeyType.Set:
                        GlobalBusiness.RedisCaches[this.configId]
                            .AddSet(this.key, val, this.dbIndex);
                        break;
                    case RedisKeyType.SortedSet:
                    {
                        string strScore = this.tbScore.Text;
                        if (string.IsNullOrEmpty(strScore))
                        {
                            MessageBox.Show("请输入排序号!");
                            return;
                        }

                        if (!double.TryParse(strScore, out double score))
                        {
                            MessageBox.Show("非法的排序号,只能输入数字!");
                            return;
                        }

                        if (this.cbScore.IsChecked == true)
                        {
                            GlobalBusiness.RedisCaches[this.configId].AddSortedSet(this.key, val, score, this.dbIndex);
                        }
                        else
                        {
                            GlobalBusiness.RedisCaches[this.configId].AddSortedSet(this.key, val, this.dbIndex);
                        }
                    }
                        break;
                    case RedisKeyType.Hash:
                    {
                        string hashKey = this.tbKey.Text.Trim();
                        if (string.IsNullOrEmpty(hashKey))
                        {
                            MessageBox.Show("请输入哈希键!");
                            return;
                        }

                        GlobalBusiness.RedisCaches[this.configId]
                            .AddHash(this.key, hashKey, val, this.dbIndex);
                    }
                        break;
                    default:
                        throw new NotSupportedException("不支持的数据类型:" + this.type);
                }

                MessageBox.Show("添加成功!");
                this.OnValueInserted?.Invoke(this.configId, this.dbIndex, this.key, this.type);
                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show($"添加失败!{ex.Message}");
            }
        }


        /// <summary>
        /// 当鼠标点击“关闭”按钮时发生。
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void CbScore_OnChecked(object sender, RoutedEventArgs e)
        {
            if (!this.IsLoaded)
            {
                return;
            }

            this.tbScore.IsEnabled = this.cbScore.IsChecked.Value;
        }
    }
}