using C1.WPF;
using C1.WPF.Chart;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;

namespace DrillDown
{
    public class SunburstAdorner : Adorner
    {
        private readonly C1Sunburst _sunburst;
        private Button _back;
        private SunburstDataLayer _layer;
        private double _centerX;
        private double _centerY;

        public SunburstAdorner(UIElement adorneredElement)
            : base(adorneredElement)
        {
            _sunburst = adorneredElement as C1Sunburst;
            _sunburst.MouseLeftButtonDown += HandleSunburstDrillDown;
            _sunburst.SliceRendered += HandleSliceRendered;
            _layer = _sunburst.DataContext as SunburstDataLayer;
            _back = new C1HyperlinkButton()
            {
                Content = "\uE0C4",
                FontFamily = new FontFamily("Segoe MDL2 Assets"),
                Width = 50,
                Height = 30,
                Style = Application.Current.Resources["BackStyle"] as Style
            };
            var multi = new MultiBinding()
            {
                Converter = new MultipleConverter()
            };
            multi.Bindings.Add(new Binding("Visibility")
            {
                Source = _sunburst
            });
            multi.Bindings.Add(new Binding("CanBack")
            {
                Source = _layer,
            });
            _back.SetBinding(C1HyperlinkButton.VisibilityProperty, multi);
            _back.Click += HandleBackButtonClick;
            AddVisualChild(_back);
            AddLogicalChild(_back);
        }

        protected override int VisualChildrenCount
        {
            get
            {
                return 1;
            }
        }

        protected override Visual GetVisualChild(int index)
        {
            return _back;
        }

        protected override Size ArrangeOverride(Size finalSize)
        {
            if (finalSize.Width != 0 && finalSize.Height != 0)
            {
                var x = _centerX - _back.Width / 2.0;
                var y = _centerY - _back.Height / 2.0;
                _back.Arrange(new Rect(x, y, _back.Width, _back.Height));
            }
            return base.ArrangeOverride(finalSize);
        }

        private void HandleSliceRendered(object sender, RenderSliceEventArgs e)
        {
            _centerX = e.CenterX;
            _centerY = e.CenterY;
        }

        private void HandleBackButtonClick(object sender, RoutedEventArgs e)
        {
            _layer.Back();
        }

        private void HandleSunburstDrillDown(object sender, MouseButtonEventArgs e)
        {
            if (_layer == null)
                return;

            var hitInfo = _sunburst.HitTest(e.GetPosition(_sunburst));
            if (hitInfo != null && hitInfo.Distance < 2)
            {
                _layer.DrillDown(hitInfo.Item);
            }
        }
    }
}