using System;
using System.Collections;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Linq;

using C1.Silverlight.Extended;
using C1.Silverlight.Gauge;
using System.Collections.Generic;
using System.ComponentModel;

namespace GaugeBuilder
{
    // UserControls can not use generic, so, we need to inherit twice.
    public class CollectionEditor<T>
         : CollectionEditorUC, ITypeEditorControl
        where T: C1GaugeDecorator
    {
        private PropertyAttribute _selectedProperty;
        public List<PropertyAttribute> InnerPropertyAttributes { get; private set; }
        public bool AutoGenterateProperties { get; set; }

        public CollectionEditor()
        {
            AutoGenterateProperties = true;
            InnerPropertyAttributes = new List<PropertyAttribute>();
        }

        #region ITypeEditorControl Members

        public event PropertyChangedEventHandler ValueChanged;

        public ITypeEditorControl Create()
        {
            return new CollectionEditor<T>();
        }

        public bool Supports(PropertyAttribute Property)
        {
            return (Property.PropertyInfo.PropertyType.GetInterfaces().Contains(typeof(IList)));
        }

        public void Attach(PropertyAttribute property)
        {
            _selectedProperty = property;
            
            ResetCollection();

            Window.Removed += new EventHandler<ElementRemovedEventArgs>(CollectionEditor_Removed);
            Window.Added += new EventHandler(CollectionEditor_Added);
        }

        public void Detach(PropertyAttribute property)
        {
            Window.ClearItems();

            Window.Removed -= new EventHandler<ElementRemovedEventArgs>(CollectionEditor_Removed);
            Window.Added -= new EventHandler(CollectionEditor_Added);
        }

        #endregion

        private C1Gauge Gauge 
        {
            get 
            {
                if (_selectedProperty == null)
                    return null;

                return (C1Gauge)_selectedProperty.SelectedObject;
            }
        }

        // fill collection
        public void ResetCollection()
        {
            if (Gauge == null)
                return;

            Window.ClearItems();

            _itemsCount = 0;
            foreach (var item in Gauge.Decorators.OfType<T>())
            {
                AddDecorator(item);
            }
        }

        void CollectionEditor_Removed(object sender, ElementRemovedEventArgs e)
        {
            if (Gauge == null)
                return;

            T dec = (T)e.RemovedElement;
            Gauge.Decorators.Remove(dec);

            if (ValueChanged != null)
                ValueChanged(this, new PropertyChangedEventArgs(_selectedProperty.MemberName));
        }

        void CollectionEditor_Added(object sender, EventArgs e)
        {
            if (Gauge == null)
                return;

            T dec = (T)typeof(T).GetConstructor(new Type[] { }).Invoke(new object[] { });
            Gauge.Decorators.Add(dec);
            AddDecorator(dec);

            if (ValueChanged != null)
                ValueChanged(this, new PropertyChangedEventArgs(_selectedProperty.MemberName));
        }

        int _itemsCount = 0;
        private void AddDecorator(T dec)
        {
            _itemsCount++;
            Window.AddItem(String.Format("{0}{1}", typeof(T).Name, _itemsCount), dec, AutoGenterateProperties, InnerPropertyAttributes);
        }

    }


}
