using System;
using System.Net;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;


namespace ReflectionIT.Windows8.Helpers {

    public class GoToStateServices {

        #region Binding Attached Property

        /// <summary> 
        /// Identifies the Binding attachted property. This enables animation, styling, binding, etc...
        /// </summary>
        public static readonly DependencyProperty BindingProperty =
            DependencyProperty.RegisterAttached("Binding",
                                                "Object",
                                                typeof(GoToStateServices).FullName,
                                                new PropertyMetadata(null, OnBindingChanged));

        /// <summary>
        /// Binding changed handler. 
        /// </summary>
        /// <param name="d">FrameworkElement that changed its Binding attached property.</param>
        /// <param name="e">DependencyPropertyChangedEventArgs with the new and old value.</param> 
        private static void OnBindingChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) {
            var source = d as Control;
            if (source != null) {
                var value = e.NewValue;
                var t = Type.GetType(GoToStateServices.GetBindingType(source).ToString());
                value = Enum.GetName(t, value);
                VisualStateManager.GoToState(source, value.ToString(), true);
            }
        }

        /// <summary>
        /// Gets the value of the Binding attached property from the specified FrameworkElement.
        /// </summary>
        public static object GetBinding(DependencyObject obj) {
            return (object)obj.GetValue(BindingProperty);
        }

        /// <summary>
        /// Sets the value of the Binding attached property to the specified FrameworkElement.
        /// </summary>
        /// <param name="obj">The object on which to set the Binding attached property.</param>
        /// <param name="value">The property value to set.</param>
        public static void SetBinding(DependencyObject obj, object value) {
            obj.SetValue(BindingProperty, value);
        }

        #endregion Binding Attached Property

        #region BindingType Attached Property

        /// <summary> 
        /// Identifies the Binding attachted property. This enables animation, styling, binding, etc...
        /// </summary>
        public static readonly DependencyProperty BindingTypeProperty =
            DependencyProperty.RegisterAttached("BindingType",
                                                "Object",
                                                typeof(GoToStateServices).FullName,
                                                new PropertyMetadata(null, null));


        /// <summary>
        /// Gets the value of the Binding attached property from the specified FrameworkElement.
        /// </summary>
        public static object GetBindingType(DependencyObject obj) {
            return (object)obj.GetValue(BindingTypeProperty);
        }


        /// <summary>
        /// Sets the value of the Binding attached property to the specified FrameworkElement.
        /// </summary>
        /// <param name="obj">The object on which to set the Binding attached property.</param>
        /// <param name="value">The property value to set.</param>
        public static void SetBindingType(DependencyObject obj, object value) {
            obj.SetValue(BindingTypeProperty, value);
        }

        #endregion Binding Attached Property
    }
}
