using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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;
using Aitex.Core.Equipment.SusceptorDefine;

namespace Aitex.Core.UI.Control
{
    /// <summary>
    /// Interaction logic for SusceptorNotchControl.xaml
    /// </summary>
    public partial class SusceptorNotchControl : UserControl
    {
        public Action<SusceptorNotchControl, bool> MouseDownCallback;

        public int Index { get; set; }
        public bool IsInputValid { private set; get; }

        public static readonly DependencyProperty DisplayIndexProperty = DependencyProperty.Register(
            "DisplayIndex", typeof(int), typeof(SusceptorNotchControl),
            new FrameworkPropertyMetadata(0, FrameworkPropertyMetadataOptions.AffectsRender));
        public int DisplayIndex
        {
            get
            {
                return (int)GetValue(DisplayIndexProperty);
            }
            set
            {
                SetValue(DisplayIndexProperty, value);
            }
        }

        public static readonly DependencyProperty CurrentSusceptorStatusProperty = DependencyProperty.Register(
            "CurrentSusceptorStatus", typeof(SusceptorStatus), typeof(SusceptorNotchControl),
            new FrameworkPropertyMetadata(SusceptorStatus.Empty, FrameworkPropertyMetadataOptions.AffectsRender));
        public SusceptorStatus CurrentSusceptorStatus
        {
            get
            {
                return (SusceptorStatus)GetValue(CurrentSusceptorStatusProperty);
            }
            set
            {
                SetValue(CurrentSusceptorStatusProperty, value);
            }
        }

        public static readonly DependencyProperty CurrentWaferStatusProperty = DependencyProperty.Register(
            "CurrentWaferStatus", typeof(WaferStatus), typeof(SusceptorNotchControl),
            new FrameworkPropertyMetadata(WaferStatus.Normal, FrameworkPropertyMetadataOptions.AffectsRender));
        public WaferStatus CurrentWaferStatus
        {
            get
            {
                return (WaferStatus)GetValue(CurrentWaferStatusProperty);
            }
            set
            {
                SetValue(CurrentWaferStatusProperty, value);
            }
        }

        public static readonly DependencyProperty CurrentWaferTypeProperty = DependencyProperty.Register(
            "CurrentWaferType", typeof(WaferType), typeof(SusceptorNotchControl),
            new FrameworkPropertyMetadata(WaferType.Empty, FrameworkPropertyMetadataOptions.AffectsRender));
        public WaferType CurrentWaferType
        {
            get
            {
                return (WaferType)GetValue(CurrentWaferTypeProperty);
            }
            set
            {
                SetValue(CurrentWaferTypeProperty, value);
            }
        }

        //如果Ture,鼠标在notch上面移动,显示颜色变化
        public static readonly DependencyProperty IsActiveProperty = DependencyProperty.Register(
            "IsActive", typeof(Boolean), typeof(SusceptorNotchControl),
            new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsRender));
        public bool IsActive
        {
            get
            {
                return (bool)GetValue(IsActiveProperty);
            }
            set
            {
                SetValue(IsActiveProperty, value);
            }
        }

        //如果Ture,nnotch可以编辑
        public static readonly DependencyProperty IsReadonlyProperty = DependencyProperty.Register(
            "IsReadonly", typeof(Boolean), typeof(SusceptorNotchControl),
            new FrameworkPropertyMetadata(true, FrameworkPropertyMetadataOptions.AffectsRender));
        public bool IsReadonly
        {
            get
            {
                return (bool)GetValue(IsReadonlyProperty);
            }
            set
            {
                SetValue(IsReadonlyProperty, value);
            }
        }


        public SusceptorNotchControl()
        {
            InitializeComponent();
        }

        public void Update()
        {
            textBoxWafer.Text = DisplayIndex.ToString();
            textBoxWafer.Background = SusceptorBrush.GetNotchBackgroundBrush(CurrentSusceptorStatus, CurrentWaferType, CurrentWaferStatus);
        }

        protected override void OnRender(DrawingContext drawingContext)
        {
            Update();
        } 

        private void Label_MouseEnter(object sender, MouseEventArgs e)
        {
            if (!IsActive) return;
            if (CurrentWaferType == WaferType.Empty)
                textBoxWafer.Background = SusceptorBrush.OnEnteringBrush;            
        }

        private void Label_MouseLeave(object sender, MouseEventArgs e)
        {
            if (!IsActive) return;
            if (CurrentWaferType == WaferType.Empty)
                textBoxWafer.Background = SusceptorBrush.EmptyBrush;
        }

        private void Label_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (IsActive && MouseDownCallback != null)
            {
                MouseDownCallback.Invoke(this, IsReadonly &&( Keyboard.IsKeyDown(Key.LeftShift)||Keyboard.IsKeyDown(Key.RightShift)));
            }

            e.Handled = false;
        }

        private void textBoxWafer_TextChanged(object sender, TextChangedEventArgs e)
        {
            int result;
            IsInputValid = !string.IsNullOrWhiteSpace(textBoxWafer.Text) && int.TryParse(textBoxWafer.Text, out result);

            Rectangle r = FindFirstVisualChild<Rectangle>(textBoxWafer, "rectangle1");
            if (r != null)
                r.Stroke = new SolidColorBrush(IsInputValid ? Colors.Black : Colors.Red);
        } 

        T FindFirstVisualChild<T>(DependencyObject obj, string childName) where T : DependencyObject
        {
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
            {
                DependencyObject child = VisualTreeHelper.GetChild(obj, i);
                if (child is T && child.GetValue(NameProperty).ToString() == childName)
                {
                    return (T)child;
                }
                else
                {
                    T childOfChild = FindFirstVisualChild<T>(child, childName);
                    if (childOfChild != null)
                    {
                        return childOfChild;
                    }
                }
            }
            return null;
        }
    }
}