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 CyberX8_Themes.UserControls
{
    /// <summary>
    /// GroupTextBoxControl.xaml 的交互逻辑
    /// </summary>
    public partial class GroupDigitalBoxControl : UserControl
    {
        #region 属性
        public static readonly DependencyProperty TitleProperty = DependencyProperty.Register("Title", typeof(string), typeof(GroupDigitalBoxControl),
          new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.AffectsRender));
        /// <summary>
        /// 标题
        /// </summary>
        public string Title
        {
            get
            {
                return (string)this.GetValue(TitleProperty);
            }
            set
            {
                this.SetValue(TitleProperty, value);
            }
        }
        public static readonly DependencyProperty UnitProperty = DependencyProperty.Register("Unit", typeof(string), typeof(GroupDigitalBoxControl),
         new FrameworkPropertyMetadata("deg", FrameworkPropertyMetadataOptions.AffectsRender));
        /// <summary>
        /// 单位
        /// </summary>
        public string Unit
        {
            get
            {
                return (string)this.GetValue(UnitProperty);
            }
            set
            {
                this.SetValue(UnitProperty, value);
            }
        }
        public static readonly DependencyProperty DigtalValueProperty = DependencyProperty.Register("DigitalValue", typeof(double), typeof(GroupDigitalBoxControl),
        new FrameworkPropertyMetadata((double)-1, new PropertyChangedCallback(DigitalValueChanged)));
        /// <summary>
        /// 数值
        /// </summary>
        public double DigitalValue
        {
            get
            {
                return (double)this.GetValue(DigtalValueProperty);
            }
            set
            {
                this.SetValue(DigtalValueProperty, value);
            }
        }

        /// <summary>
        /// 数值发生变化事件
        /// </summary>
        /// <param name="d"></param>
        /// <param name="e"></param>
        private static void DigitalValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (e.NewValue != null)
            {
                double newValue = (double)e.NewValue;
                double minValue = (double)d.GetValue(MinValueProperty);
                double maxValue = (double)d.GetValue(MaxValueProperty);
                bool validResult = newValue >= minValue && newValue <= maxValue;
                d.SetValue(ValidResultProperty, validResult);
            }
        }

        public static readonly DependencyProperty MinValueProperty = DependencyProperty.Register("MinValue", typeof(double), typeof(GroupDigitalBoxControl),
        new FrameworkPropertyMetadata((double)0, FrameworkPropertyMetadataOptions.AffectsRender));
        /// <summary>
        /// 最小数值
        /// </summary>
        public double MinValue
        {
            get
            {
                return (double)this.GetValue(MinValueProperty);
            }
            set
            {
                this.SetValue(MinValueProperty, value);
            }
        }
        public static readonly DependencyProperty MaxValueProperty = DependencyProperty.Register("MaxValue", typeof(double), typeof(GroupDigitalBoxControl),
        new FrameworkPropertyMetadata((double)0, FrameworkPropertyMetadataOptions.AffectsRender));
        /// <summary>
        /// 最大数值
        /// </summary>
        public double MaxValue
        {
            get
            {
                return (double)this.GetValue(MaxValueProperty);
            }
            set
            {
                this.SetValue(MaxValueProperty, value);
            }
        }
        public static readonly DependencyProperty EnableProperty = DependencyProperty.Register("Enable", typeof(bool), typeof(GroupDigitalBoxControl),
        new FrameworkPropertyMetadata(true, FrameworkPropertyMetadataOptions.AffectsRender));
        /// <summary>
        /// 可用性
        /// </summary>
        public bool Enable
        {
            get
            {
                return (bool)this.GetValue(EnableProperty);
            }
            set
            {
                this.SetValue(EnableProperty, value);
            }
        }

        public static readonly DependencyProperty ValidResultProperty = DependencyProperty.Register("ValidResult", typeof(bool), typeof(GroupDigitalBoxControl),
     new FrameworkPropertyMetadata(true, FrameworkPropertyMetadataOptions.AffectsRender));
        /// <summary>
        /// 合理性
        /// </summary>
        public bool ValidResult
        {
            get
            {
                return (bool)this.GetValue(ValidResultProperty);
            }
            set
            {
                this.SetValue(ValidResultProperty, value);
            }
        }
        #endregion
        public GroupDigitalBoxControl()
        {
            InitializeComponent();
        }

        protected override void OnRender(DrawingContext drawingContext)
        {
            base.OnRender(drawingContext);

            ValidResult = DigitalValue >= MinValue && DigitalValue <= MaxValue;
            RectangleSetPoint.Fill = ValidResult? new SolidColorBrush(Colors.Transparent) : new SolidColorBrush(Colors.Yellow);
            txtValue.Background = ValidResult? new SolidColorBrush(Colors.Transparent) : new SolidColorBrush(Colors.Yellow);
        }
    }
}