

Imports C1.Xaml.Chart

Class SunburstAdorner
    Private _chart As C1Sunburst
    Private _layer As SunburstDataLayer
    Private _adorner As Popup

    Private _back As HyperlinkButton

    Private _centerX As Double
    Private _centerY As Double

    Public ReadOnly Property Adorner() As Popup
        Get
            Return _adorner
        End Get
    End Property

    Public Sub New(chart As C1Sunburst)
        _chart = chart
        _layer = TryCast(_chart.DataContext, SunburstDataLayer)
        AddHandler _chart.SliceRendered, AddressOf _chart_SliceRendered
        AddHandler _chart.Tapped, AddressOf _chart_Tapped
        AddHandler _chart.Loaded, AddressOf _chart_Loaded
        AddHandler _chart.Unloaded, AddressOf _chart_Unloaded
        AddHandler _chart.SizeChanged, AddressOf _chart_SizeChanged

        AddHandler _layer.PropertyChanged, AddressOf _layer_PropertyChanged

        _chart.RegisterPropertyChangedCallback(Control.VisibilityProperty, New DependencyPropertyChangedCallback(AddressOf _chart_PropertyChangedCallback))

        _back = New HyperlinkButton() With {
            .Content = ChrW(&HE0C4),
            .Width = 50,
            .Height = 30,
            .FontFamily = New FontFamily("Segoe MDL2 Assets"),
            .HorizontalAlignment = Windows.UI.Xaml.HorizontalAlignment.Left,
            .VerticalAlignment = Windows.UI.Xaml.VerticalAlignment.Top
        }
        AddHandler _back.Click, AddressOf _back_Click

        Me._adorner = New Popup() With {
            .Visibility = Windows.UI.Xaml.Visibility.Collapsed,
            .HorizontalAlignment = Windows.UI.Xaml.HorizontalAlignment.Left,
            .VerticalAlignment = Windows.UI.Xaml.VerticalAlignment.Top
        }
        Me._adorner.Child = _back
        Me._adorner.IsOpen = True
    End Sub

    Private Sub _chart_Tapped(sender As Object, e As Windows.UI.Xaml.Input.TappedRoutedEventArgs)
        Dim hitInfo = _chart.HitTest(e.GetPosition(_chart))
        If hitInfo IsNot Nothing AndAlso hitInfo.Distance < 2 Then
            _layer.DrillDown(hitInfo.Item)
        End If
    End Sub

    Private Sub _chart_SliceRendered(sender As Object, e As RenderSliceEventArgs)
        _centerX = e.CenterX
        _centerY = e.CenterY
    End Sub

    Public Sub _chart_Loaded(ByVal sender As Object, ByVal e As RoutedEventArgs)
        _adorner.Visibility = If(_layer.CanBack, Windows.UI.Xaml.Visibility.Visible, Windows.UI.Xaml.Visibility.Collapsed)
    End Sub
    Public Sub _chart_Unloaded(ByVal sender As Object, ByVal e As RoutedEventArgs)
        _adorner.Visibility = If(_layer.CanBack, Windows.UI.Xaml.Visibility.Visible, Windows.UI.Xaml.Visibility.Collapsed)
    End Sub

    Private Sub _chart_PropertyChangedCallback(ByVal d As DependencyObject, ByVal dp As DependencyProperty)
        If _chart.Visibility = Visibility.Visible AndAlso _layer.CanBack Then
            _adorner.IsOpen = True
        Else
            _adorner.IsOpen = False
        End If
    End Sub

    Public Sub _layer_PropertyChanged(ByVal sender As Object, ByVal e As PropertyChangedEventArgs)
        If e.PropertyName = "CanBack" Then
            _adorner.Visibility = If(_layer.CanBack, Windows.UI.Xaml.Visibility.Visible, Windows.UI.Xaml.Visibility.Collapsed)
        End If
    End Sub

    Public Sub _chart_SizeChanged(ByVal sender As Object, ByVal e As SizeChangedEventArgs)
        Dim finalSize = Me._chart.RenderSize
        If finalSize.Width <> 0 AndAlso finalSize.Height <> 0 Then
            Dim x = _centerX - _back.ActualWidth / 2.0
            Dim y = _centerY - _back.ActualHeight / 2.0

            Dim transformToVisual = _chart.TransformToVisual(_adorner)
            Dim borderCoordinats = transformToVisual.TransformPoint(New Point(x, y))

            _back.Margin = New Windows.UI.Xaml.Thickness(borderCoordinats.X, borderCoordinats.Y, 0, 0)
        End If
    End Sub

    Private Sub _back_Click(sender As Object, e As Windows.UI.Xaml.RoutedEventArgs)
        _layer.Back()
    End Sub
End Class