using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace NEIntelligentControl2.Views.AGC
{
    /// <summary>
    /// 小圆点页面
    /// </summary>
    public partial class DotView : UserControl
    {
        // ------------事件--------------
        public static readonly RoutedEvent ChangedEvent = EventManager.RegisterRoutedEvent("Changed", RoutingStrategy.Bubble, typeof(EventHandler<DotChangedRoutedEventArgs>), typeof(DotView));
        public event RoutedEventHandler Changed { add { AddHandler(ChangedEvent, value); } remove { RemoveHandler(ChangedEvent, value); } }

        private Dictionary<RadioButton, int> _ButtonPairs;// 小圆点列表

        private int count;
        /// <summary>
        /// 小圆点数量
        /// </summary>
        public int Count
        {
            get => count;
            set
            {
                count = value;
                _ButtonPairs = new Dictionary<RadioButton, int>();
                _SPMain.Children.Clear();
                var style = this.Resources["MainRadioButtonStyle"] as Style;
                for (int i = 0; i < count; ++i)
                {
                    RadioButton rb = new RadioButton() { Style = style };
                    rb.Checked += DotChecked;
                    _ButtonPairs.Add(rb, i);
                    _SPMain.Children.Add(rb);
                }
            }
        }

        private void DotChecked(object sender, RoutedEventArgs e)
        {
            RadioButton rb = sender as RadioButton;
            if (rb == null) return;
            DotChangedRoutedEventArgs dcre = new DotChangedRoutedEventArgs(ChangedEvent, sender, this[rb]);
            RaiseEvent(dcre);
        }

        /// <summary>
        /// 选择小圆点
        /// </summary>
        /// <param name="index">小圆点序列(从0开始)</param>
        /// <returns>是否成功</returns>
        public bool Check(int index)
        {
            foreach (var v in _ButtonPairs)
            {
                if (v.Value == index)
                {
                    v.Key.IsChecked = true;
                    return true;
                }
            }
            return false;
        }

        /// <summary>
        /// 获取选中的按钮序列
        /// </summary>
        /// <param name="rb">按钮</param>
        /// <returns>序列</returns>
        public int this[RadioButton rb]
        {
            get
            {
                if (_ButtonPairs.ContainsKey(rb))
                    return _ButtonPairs[rb];
                return -1;
            }
        }
        public DotView()
        {
            _ButtonPairs = new Dictionary<RadioButton, int>();
            InitializeComponent();
        }
    }


    /// <summary>
    /// 小圆点选择改变你事件
    /// </summary>
    public class DotChangedRoutedEventArgs : RoutedEventArgs
    {
        /// <summary>
        /// 选择的小圆点序列
        /// </summary>
        public int Index { get; set; }
        /// <summary>
        /// 选择的按钮
        /// </summary>
        public RadioButton SelectedButton { get; set; }
        public DotChangedRoutedEventArgs(RoutedEvent routedEvent, object source, int index) : base(routedEvent, source)
        {
            Index = index;
            SelectedButton = source as RadioButton;
        }
    }
}
