using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using BeWo.View.Detail;
using BeWo.ViewModel;

namespace BeWo.View.Controls
{
    /// <summary>
    ///     Interaction logic for AssessmentSheetValueControl.xaml
    /// </summary>
    public partial class AssessmentSheetValueControl : UserControl
    {
        public static readonly DependencyProperty AssessmetSheetValueProperty = DependencyProperty.Register(
            "AssessmetSheetValue",
            typeof (AssessmentSheetValueVM),
            typeof (AssessmentSheetValueControl),
            new UIPropertyMetadata(
                (s, e) =>
                    {
                        var vm = e.NewValue as AssessmentSheetValueVM;
                        var c = s as AssessmentSheetValueControl;

                        if (c != null)
                        {
                            if (vm != null)
                            {
                                c.shine.Visibility = Visibility.Visible;
                                c.tb.Text = vm.Sign;
                                c.border.Background = vm.Brush;
                                c.border.ToolTip = vm.Description;
                            }
                            else
                            {
                                c.shine.Visibility = Visibility.Hidden;
                                c.tb.Text = string.Empty;
                                c.border.Background = Brushes.White;
                                c.border.ToolTip = string.Empty;
                            }
                        }
                    }));

        public AssessmentSheetValueControl()
        {
            InitializeComponent();
            MouseDown += AssessmentSheetValueControl_MouseDown;
        }

        public AssessmentSheetValueVM AssessmetSheetValue
        {
            get { return (AssessmentSheetValueVM) GetValue(AssessmetSheetValueProperty); }

            set { SetValue(AssessmetSheetValueProperty, value); }
        }

        private void AssessmentSheetValueControl_MouseDown(object sender, MouseButtonEventArgs e)
        {
            AssessmentSheetEntryView entryView = null;

            DependencyObject parent = VisualTreeHelper.GetParent(this);

            while (parent != null && entryView == null)
            {
                entryView = parent as AssessmentSheetEntryView;
                var fe = parent as FrameworkElement;
                parent = fe != null ? VisualTreeHelper.GetParent(fe) : null;
            }

            if (entryView != null)
            {
                entryView.AssessmentSheetValueControl_MouseDown(sender, e);
            }
        }
    }
}