using System;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;

using BS.Shared.Extensions;

namespace BeWo.Controls.Controls.Calendar
{
    public class AppointmentBar : ObservableCollection<Border>
    {
        private readonly Brush _ForeColor;

        private bool _IsChecked;

        public AppointmentBar(Appointment pAppointment, Orientation pOrientation, SolidColorBrush pFillBrush)
        {
            Orientation = pOrientation;
            Appointment = pAppointment;
            FillBrush = pFillBrush;
            _ForeColor = pFillBrush.Color.GetPerceivedBrightness() < 130 ? Brushes.White : Brushes.Black;
        }

        public Appointment Appointment { get; set; }

        public SolidColorBrush FillBrush { get; set; }

        public bool IsChecked
        {
            get { return _IsChecked; }

            set
            {
                if (value != _IsChecked)
                {
                    foreach (Border iBorder in this)
                    {
                        ((TextBlock) ((test) iBorder.Child).Children[0]).Foreground = value ? Brushes.Black : _ForeColor;
                        iBorder.Background = value ? PressedBrush.Clone() : FillBrush;
                    }

                    _IsChecked = value;
                    if (IsCheckedChanged != null)
                    {
                        IsCheckedChanged(this, new EventArgs());
                    }
                }
            }
        }

        public Brush MouseOverBrush { get; set; }

        public Orientation Orientation { get; set; }

        public Brush PressedBrush { get; set; }
        public event EventHandler IsCheckedChanged;

        protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
        {
            if (e.NewItems != null)
            {
                foreach (Border iBorder in e.NewItems)
                {
                    iBorder.Background = FillBrush;
                    iBorder.MouseEnter += iBorder_MouseEnter;
                    iBorder.MouseLeave += iBorder_MouseLeave;
                    iBorder.MouseDown += iBorder_MouseDown;
                    iBorder.MouseUp += iBorder_MouseUp;
                    iBorder.Margin = new Thickness(0);
                    iBorder.Padding = new Thickness(0);

                    iBorder.BorderBrush = Brushes.Black;

                    var lTb = new TextBlock();

                    lTb.TextWrapping = TextWrapping.Wrap;
                    lTb.LineStackingStrategy = LineStackingStrategy.BlockLineHeight;
                    lTb.LineHeight = 13;
                    lTb.Margin = new Thickness(1);
                    lTb.Padding = new Thickness(0);
                    lTb.FontSize = 12;
                    lTb.Text = Appointment.Notice;
                    lTb.VerticalAlignment = VerticalAlignment.Center;
                    lTb.HorizontalAlignment = HorizontalAlignment.Center;
                    lTb.IsHitTestVisible = false;
                    lTb.Foreground = _ForeColor;

                    var k = new test();
                    k.Children.Add(lTb);

                    if (Orientation == Orientation.Vertical)
                    {
                        lTb.LayoutTransform = new RotateTransform(0);
                    }

                    iBorder.Child = k;
                }
            }

            base.OnCollectionChanged(e);
        }

        private void iBorder_MouseDown(object sender, MouseButtonEventArgs e)
        {
            foreach (Border iBorder in this)
            {
                iBorder.Background = PressedBrush;
            }
        }

        private void iBorder_MouseEnter(object sender, MouseEventArgs e)
        {
            if (!_IsChecked)
            {
                foreach (Border iBorder in this)
                {
                    ((TextBlock) ((test) iBorder.Child).Children[0]).Foreground = Brushes.Black;
                    iBorder.Background = MouseOverBrush;
                }
            }
        }

        private void iBorder_MouseLeave(object sender, MouseEventArgs e)
        {
            if (!_IsChecked)
            {
                foreach (Border iBorder in this)
                {
                    ((TextBlock) ((test) iBorder.Child).Children[0]).Foreground = _ForeColor;
                    iBorder.Background = FillBrush;
                }
            }
        }

        private void iBorder_MouseUp(object sender, MouseButtonEventArgs e)
        {
            IsChecked = !IsChecked;
        }
    }

    public class test : Grid
    {
        protected override Size MeasureOverride(Size constraint)
        {
            return new Size(1, 1);
        }
    }
}