using C1.Xaml.Chart;
using Windows.Foundation;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Media;

namespace DrillDown
{
    class SunburstAdorner
    {
        private C1Sunburst _chart;
        private SunburstDataLayer _layer;
        private Popup _adorner;

        private HyperlinkButton _back;

        private double _centerX;
        private double _centerY;

        public Popup Adorner
        {
            get
            {
                return _adorner;
            }
        }

        public SunburstAdorner(C1Sunburst chart)
        {
            _chart = chart;
            _layer = _chart.DataContext as SunburstDataLayer;
            _chart.SliceRendered += _chart_SliceRendered;

            _chart.Tapped += _chart_Tapped;

            _chart.Loaded += (o, e) =>
            {
                _adorner.Visibility = _layer.CanBack ? Windows.UI.Xaml.Visibility.Visible : Windows.UI.Xaml.Visibility.Collapsed;
            };
            _chart.Unloaded += (o, e) =>
            {
                _adorner.Visibility = _layer.CanBack ? Windows.UI.Xaml.Visibility.Visible : Windows.UI.Xaml.Visibility.Collapsed;
            };
            _chart.RegisterPropertyChangedCallback(Control.VisibilityProperty, new Windows.UI.Xaml.DependencyPropertyChangedCallback((o, e)=>{
                if (_chart.Visibility == Windows.UI.Xaml.Visibility.Visible && _layer.CanBack)
                {
                    this._adorner.IsOpen = true;
                }
                else
                {
                    this._adorner.IsOpen = false;
                }

            }));

            _layer.PropertyChanged += (o, e) =>
            {
                if (e.PropertyName == "CanBack")
                {
                    _adorner.Visibility = _layer.CanBack ? Windows.UI.Xaml.Visibility.Visible : Windows.UI.Xaml.Visibility.Collapsed;
                }
            };

            _chart.SizeChanged += (o, e) =>
            {
                var finalSize = this._chart.RenderSize;
                if (finalSize.Width != 0 && finalSize.Height != 0)
                {
                    var x = _centerX - _back.ActualWidth / 2.0;
                    var y = _centerY - _back.ActualHeight / 2.0;


                    var transformToVisual = _chart.TransformToVisual(_adorner);
                    var borderCoordinats = transformToVisual.TransformPoint(new Point(x, y));

                    _back.Margin = new Windows.UI.Xaml.Thickness(borderCoordinats.X, borderCoordinats.Y, 0, 0);
                }
            };
            
            _back = new HyperlinkButton()
            {
                Content = "\uE0C4",
                Width = 50,
                Height = 30,
                FontFamily = new FontFamily("Segoe MDL2 Assets"),
                
                HorizontalAlignment = Windows.UI.Xaml.HorizontalAlignment.Left,
                VerticalAlignment = Windows.UI.Xaml.VerticalAlignment.Top
            };
            _back.Click += _back_Click;

            this._adorner = new Popup()
            {
                Visibility = Windows.UI.Xaml.Visibility.Collapsed,
                HorizontalAlignment = Windows.UI.Xaml.HorizontalAlignment.Left,
                VerticalAlignment = Windows.UI.Xaml.VerticalAlignment.Top
            };
            this._adorner.Child = _back;
            this._adorner.IsOpen = true;
        }

        private void _back_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
        {
            _layer.Back();
        }

        private void _chart_Tapped(object sender, Windows.UI.Xaml.Input.TappedRoutedEventArgs e)
        {
            var hitInfo = _chart.HitTest(e.GetPosition(_chart));
            if (hitInfo != null && hitInfo.Distance < 2)
            {
                _layer.DrillDown(hitInfo.Item);
            }
        }

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

        }
    }
}
