using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using C1.Silverlight;
using C1.Silverlight.Extended;
using System.ComponentModel;
using System.Windows.Data;
using System.Globalization;

namespace C1Extended_Demo
{
    public partial class SliderNumericEditor : UserControl, ITypeEditorControl
    {
        //add by david @20130509
        private PropertyGroup _selectedGroup;

        private string _valuePropertyName = "IsChecked";
        //end add

        public SliderNumericEditor()
        {
            InitializeComponent();
        }

        #region ITypeEditorControl Members

        public bool Supports(PropertyAttribute property)
        {
            return property.PropertyInfo.PropertyType.GetNonNullableType().IsNumeric() &&
                !double.IsNaN(property.MinimumValue)&&
                !double.IsNaN(property.MaximumValue);
        }

        public ITypeEditorControl Create()
        {
            return new SliderNumericEditor();
        }

        public void Attach(PropertyAttribute property)
        {
            //label binding
            var lblBinding = new Binding(property.PropertyInfo.Name)
            {
                Mode = BindingMode.TwoWay,
                Source = property.SelectedObject
            };
            lblNumber.SetBinding(TextBlock.TextProperty, lblBinding);
            //slider binding's
            var sliderBinding = new Binding(property.PropertyInfo.Name)
            {
                Mode = BindingMode.TwoWay,
                Source = property.SelectedObject,
                ValidatesOnExceptions = true
            };
            slider.SetBinding(Slider.ValueProperty, sliderBinding);
            slider.Minimum = property.MinimumValue;
            slider.Maximum = property.MaximumValue;
            //checkBox binding
            var checkBoxBinding = new Binding(property.PropertyInfo.Name)
            {
                Mode = BindingMode.TwoWay,
                Source = property.SelectedObject,
                Converter = new NullableCheckBoxConverter(),
                ValidatesOnExceptions = true
            };
            checkNullable.SetBinding(CheckBox.IsCheckedProperty, checkBoxBinding);
            //checkNullable.Visibility = Visibility.Collapsed;
        }

        public void Detach(PropertyAttribute property)
        {
        }

        public event PropertyChangedEventHandler ValueChanged;

        #endregion

    }

    internal class NullableCheckBoxConverter : IValueConverter
    {
        #region IValueConverter Members

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (targetType.GetNonNullableType().Name == "Boolean")
            {
                if (value == null)
                {
                    return false;
                }
                if (value.GetType().IsNumericNonIntegral())
                {
                    double dValue = System.Convert.ToDouble(value);
                    return !double.IsNaN(dValue);
                }
            }
            return value;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value is bool && targetType.IsNullableType() && targetType.GetNonNullableType().IsNumericNonIntegral())
            {
                if (!(bool)value)
                {
                    return null;
                }
            }
            return value;
        }

        #endregion
    }
}
