//------------------------------------------------------------------------------
// <copyright file=”*.cs” company=”EhLib Team”>
//     Copyright (c) 2017 Dmitry V. Bolshakov   
// </copyright>
//------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.ComponentModel.Design;
using System.ComponentModel;
using System.Collections;

namespace EhLib.Design
{
  public class DataGridDynaColumnCellsEditor : CollectionEditor
  {
    private Type[] returnedTypes;

    public DataGridDynaColumnCellsEditor(Type type) : base(type)
    {
    }

    protected override Type[] CreateNewItemTypes()
    {
      return returnedTypes;
    }

    public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
    {
      if (returnedTypes == null)
      {
        returnedTypes = GetReturnedTypes(provider);
        //MessageBox.Show("EditValue - " + returnedTypes.Length.ToString());
      }
      //IWindowsFormsEditorService edSvc = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
      return base.EditValue(context, provider, value);
    }

    private Type[] GetReturnedTypes(IServiceProvider provider)
    {
      List<Type> result = new List<Type>();

      ITypeDiscoveryService tds = 
        (ITypeDiscoveryService)provider.GetService(typeof(ITypeDiscoveryService));

      if (tds != null)
      {
        ICollection allCellTypes = tds.GetTypes(typeof(DataAxisGridDataCellMan), false);
        Type gridDataCellDesignTimeVisibleAttributeType = typeof(DataAxisGridDataCellDesignTimeVisibleAttribute);
        foreach (Type cellManType in allCellTypes)
        {
          var attr = TypeDescriptor.GetAttributes(cellManType)[gridDataCellDesignTimeVisibleAttributeType] as DataAxisGridDataCellDesignTimeVisibleAttribute;
          if ((attr == null) ||
              (attr != null && !attr.Visible))
          {
            continue;
          }

          if (!result.Contains(cellManType) && 
              (cellManType != typeof(DataAxisGridDataCellMan)))
          {
            result.Add(cellManType);
          }
        }
      }

      return result.ToArray();
    }

    protected override object CreateInstance(Type itemType)
    {
      ////Debug.WriteLine("DataGridColumnsEditor.CreateInstance(Type itemType)-" + itemType.ToString());
      //DataGridColumnCollection stColumns = ((DataGridEh)Context.Instance).StaticColumns;

      //IDesignerHost host = (IDesignerHost)srvProvider.GetService(typeof(IDesignerHost));
      //DataGridColumn column = (DataGridColumn)host.CreateComponent(itemType);

      ////DataGridColumn column = (DataGridColumn)Activator.CreateInstance(itemType);
      //stColumns.Add(column);
      //return column;
      return base.CreateInstance(itemType);
    }

    protected override CollectionForm CreateCollectionForm()
    {
      CollectionForm form = base.CreateCollectionForm();
      return form;
    }

  }
}
