using Common.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
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 MegaRobo.C00225155App.MenuViews
{
    /// <summary>
    /// BottleUserControl.xaml 的交互逻辑
    /// </summary>
    public partial class PowderBottleUserControl_Show : UserControl
    {
        public SourcePowderBottleModel PowderBottleModel
        {
            get { return (SourcePowderBottleModel)GetValue(PowderBottleModelProperty); }
            set { SetValue(PowderBottleModelProperty, value); }
        }

        // Using a DependencyProperty as the backing store for MyProperty.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty PowderBottleModelProperty =
            DependencyProperty.Register(
                "PowderBottleModel",
                typeof(SourcePowderBottleModel),
                typeof(PowderBottleUserControl_Show),
                new PropertyMetadata(null, OnPowderBottleModelChanged)// 重要：默认值设为 null
                );

        //public Brush BorderBrushColor2
        //{
        //    get { return (Brush)GetValue(BorderBrushColor2Property); }
        //    set { SetValue(BorderBrushColor2Property, value); }
        //}


        //public static readonly DependencyProperty BorderBrushColor2Property =
        //    DependencyProperty.Register(
        //        "BorderBrushColor2",
        //        typeof(Brush),
        //        typeof(PowderBottleUserControl),
        //        new PropertyMetadata(new SolidColorBrush(Color.FromRgb(66, 66, 66)))
        //        );


        public PowderBottleUserControl_Show()
        {
            InitializeComponent();
        }

        private static void OnPowderBottleModelChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var control = (PowderBottleUserControl_Show)d;
            //if (e.OldValue is SourcePowderBottleModel oldModel)
            //{
            //    oldModel.PropertyChanged -= control.PowderBottleModel_PropertyChanged;
            //}
            //if (e.NewValue is SourcePowderBottleModel newModel)
            //{
            //    newModel.PropertyChanged += control.PowderBottleModel_PropertyChanged;
            //}
            //control.UpdateBorderBrushColor();
            control.InvalidateVisual();//强制重绘
        }

        private void PowderBottleModel_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == nameof(SourcePowderBottleModel.SourcePowderName))
            {
                UpdateBorderBrushColor();
            }
        }

        private void UpdateBorderBrushColor()
        {
            if (PowderBottleModel != null)
            {
                if (!string.IsNullOrEmpty(PowderBottleModel.SourcePowderName))
                {
                    PowderBottleModel.HaveBottle = true;
                }
                else
                {
                    PowderBottleModel.HaveBottle = false;
                }
            }
        }
     
        private void BorderMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            //// 创建编辑属性窗口实例
            //var editWindow = new EditPowderBottlePropertiesWindow
            //{
            //    PowderBottle = this.PowderBottleModel,
            //};

            //// 显示编辑属性窗口并等待用户操作
            //if (editWindow.ShowDialog() == true)
            //{
            //    // 更新自定义控件的属性
            //    this.PowderBottleModel = editWindow.PowderBottle;
            //}
        }
    }
}
