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>
    /// PressureBlockControl.xaml 的交互逻辑
    /// </summary>
    public partial class PressureBlockControl : UserControl
    {
        #region 属性
        public static readonly DependencyProperty ValueProperty = DependencyProperty.Register("Value", typeof(double), typeof(PressureBlockControl),
      new FrameworkPropertyMetadata((double)0, FrameworkPropertyMetadataOptions.AffectsRender));
        /// <summary>
        /// 数值
        /// </summary>
        public double Value
        {
            get
            {
                return (double)this.GetValue(ValueProperty);
            }
            set
            {
                this.SetValue(ValueProperty, value);
            }
        }
        public static readonly DependencyProperty ForeColorProperty = DependencyProperty.Register("ForeColor", typeof(SolidColorBrush), typeof(PressureBlockControl),
      new FrameworkPropertyMetadata(new SolidColorBrush(Colors.Lime), FrameworkPropertyMetadataOptions.AffectsRender));
        /// <summary>
        /// 文字颜色
        /// </summary>
        public SolidColorBrush ForeColor
        {
            get
            {
                return (SolidColorBrush)this.GetValue(ForeColorProperty);
            }
            set
            {
                this.SetValue(ForeColorProperty, value);
            }
        }
        public static readonly DependencyProperty BackgroundColorProperty = DependencyProperty.Register("BackgroundColor", typeof(SolidColorBrush), typeof(PressureBlockControl),
      new FrameworkPropertyMetadata(new SolidColorBrush(Colors.Black), FrameworkPropertyMetadataOptions.AffectsRender));
        /// <summary>
        /// 背景颜色
        /// </summary>
        public SolidColorBrush BackgroundColor
        {
            get
            {
                return (SolidColorBrush)this.GetValue(BackgroundColorProperty);
            }
            set
            {
                this.SetValue(BackgroundColorProperty, value);
            }
        }

        public static readonly DependencyProperty IsWarningProperty = DependencyProperty.Register("IsWarning", typeof(bool), typeof(PressureBlockControl),
      new FrameworkPropertyMetadata(false, new PropertyChangedCallback(IsWarningPropertyChanged)));
        /// <summary>
        /// 是否告警
        /// </summary>
        public bool IsWarning
        {
            get
            {
                return (bool)this.GetValue(IsWarningProperty);
            }
            set
            {
                this.SetValue(IsWarningProperty, value);
            }
        }

        public static readonly DependencyProperty IsErrorProperty = DependencyProperty.Register("IsError", typeof(bool), typeof(PressureBlockControl),
      new FrameworkPropertyMetadata(false, new PropertyChangedCallback(IsErrorPropertyChanged)));
        /// <summary>
        /// 是否错误
        /// </summary>
        public bool IsError
        {
            get
            {
                return (bool)this.GetValue(IsErrorProperty);
            }
            set
            {
                this.SetValue(IsErrorProperty, value);
            }
        }

        private static void IsWarningPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if(e.NewValue!=null)
            {
                bool isWarning=(bool)e.NewValue;
                bool isError = (bool)d.GetValue(IsErrorProperty);
                if(isWarning)
                {
                    if (!isError)
                    {
                        d.SetValue(BackgroundColorProperty, new SolidColorBrush(Colors.Yellow));
                        d.SetValue(ForeColorProperty, new SolidColorBrush(Colors.Black));
                    }
                }
                else
                {
                    if (!isError)
                    {
                        d.SetValue(BackgroundColorProperty, new SolidColorBrush(Colors.Black));
                        d.SetValue(ForeColorProperty, new SolidColorBrush(Colors.Lime));
                    }
                }
            }
        }
        private static void IsErrorPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (e.NewValue != null)
            {
                bool isError = (bool)e.NewValue;
                bool isWarning = (bool)d.GetValue(IsWarningProperty);
                if (isError)
                {
                    d.SetValue(BackgroundColorProperty, new SolidColorBrush(Colors.Red));
                    d.SetValue(ForeColorProperty, new SolidColorBrush(Colors.Black));
                }
                else
                {
                    if (!isWarning)
                    {
                        d.SetValue(BackgroundColorProperty, new SolidColorBrush(Colors.Black));
                        d.SetValue(ForeColorProperty, new SolidColorBrush(Colors.Lime));
                    }
                }
            }
        }

        #endregion
        public PressureBlockControl()
        {
            InitializeComponent();
        }
    }
}