using System;
using System.Windows.Forms;
using System.Drawing;
using System.ComponentModel;
using FastReport.Utils;

namespace FastReport.Controls
{
#if !DEBUG
    [DesignTimeVisible(false)]
#endif
    internal class ComponentRefComboBox : ComboBox
    {
        public Base SelectedObject
        {
            get { return SelectedIndex <= 0 ? null : Items[SelectedIndex] as Base; }
        }

        protected override void OnDrawItem(DrawItemEventArgs e)
        {
            e.DrawBackground();

            if (e.Index >= 0)
            {
                Base c = Items[e.Index] as Base;
                Image img = this.GetImage(c == null ? 76 : RegisteredObjects.FindObject(c).ImageIndex);
                string text = c == null ? Res.Get("Misc,None") : c.Name;
                this.DrawImageAndText(e, img, text);
            }
        }

        public void PopulateList(Base parent, Type componentType, object instance)
        {
            Items.Clear();
            Items.Add(0);

            FastReport.ObjectCollection list = parent.AllObjects;
            foreach (Base c in list)
            {
                if (c != instance && (c.GetType() == componentType || c.GetType().IsSubclassOf(componentType)))
                    Items.Add(c);
            }
        }

        public ComponentRefComboBox()
        {
            ItemHeight = 19;
            DropDownStyle = ComboBoxStyle.DropDownList;
            DrawMode = DrawMode.OwnerDrawFixed;
        }
    }
}
