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;

namespace GaugeBuilder.Editors
{
    public partial class ThicknessEditor : C1TextBoxBase, ITypeEditorControl
    {
        PropertyAttribute _selectedProperty;
        bool ITypeEditorControl.Supports(PropertyAttribute property)
        {
            return property.PropertyInfo.PropertyType == typeof(Thickness);
        }

        void ITypeEditorControl.Attach(PropertyAttribute property)
        {
            var binding = new Binding();
            binding.Path = new PropertyPath(property.MemberName);
            binding.Mode = BindingMode.TwoWay;
            binding.NotifyOnValidationError = true;
            binding.ValidatesOnExceptions = true;
            binding.Converter = new Converter();
            binding.Source = property.SelectedObject;
            SetBinding(TextProperty, binding);

            _selectedProperty = property;
            TextChanged += ThicknessEditor_TextChanged;
        }

        void ITypeEditorControl.Detach(PropertyAttribute property)
        {
            _selectedProperty = null;
            TextChanged -= ThicknessEditor_TextChanged;
        }

        void ThicknessEditor_TextChanged(object sender, TextChangedEventArgs e)
        {
            ValueChanged.Invoke(this, new PropertyChangedEventArgs(_selectedProperty.MemberName));
        }

        ITypeEditorControl ITypeEditorControl.Create()
        {
            return new ThicknessEditor();
        }

        public event PropertyChangedEventHandler ValueChanged = delegate { };

        class Converter : IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                var thickness = (Thickness)value;
                return thickness.ToString();
            }

            public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                var text = (string)value;
                var parts = text.Split(',');
                if (parts.Length != 4)
                    return null;

                double left;
                if (!double.TryParse(parts[0], out left))
                    return null;
                double top;
                if (!double.TryParse(parts[1], out top))
                    return null;
                double right;
                if (!double.TryParse(parts[2], out right))
                    return null;
                double bottom;
                if (!double.TryParse(parts[3], out bottom))
                    return null;

                return new Thickness(left, top, right, bottom);
            }
        }
    }
}
