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

namespace FastReport.Controls
{
#if !DEBUG
    [DesignTimeVisible(false)]
#endif
    internal class DataColumnDropDown : ToolStripDropDown
    {
        private ToolStripControlHost host;
        private DataTreeView tree;

        public event EventHandler ColumnSelected;

        public Control Owner { get; set; }
        
        public DataTreeView DataTree
        {
            get { return tree; }
        }

        public DataSourceBase DataSource
        {
            get { return tree.DataSource; }
            set { tree.DataSource = value; }
        }

        public string Column
        {
            get { return tree.SelectedItem; }
            set { tree.SelectedItem = value; }
        }

        private void FTree_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if ((e.Node.Tag == null || Column != "") && ColumnSelected != null)
            {
                Close();
                ColumnSelected(this, EventArgs.Empty);
            }
        }

        public void CreateNodes(Report report)
        {
            tree.CreateNodes(report.Dictionary);
        }

        public void SetSize(int width, int height)
        {
            tree.Size = new Size(width - 2, height);
        }

        protected override void OnOpening(CancelEventArgs e)
        {
            base.OnOpening(e);
            if (Owner != null)
            {
                tree.Font = Owner.Font;
                tree.ImageList = Owner.GetImages();
            }
        }

        public DataColumnDropDown()
        {
            tree = new DataTreeView();
            tree.Size = new Size(200, 250);
            tree.BorderStyle = BorderStyle.None;
            tree.HideSelection = false;
            tree.ShowEnabledOnly = true;
            tree.ShowNone = true;
            tree.ShowRelations = true;
            tree.ShowVariables = false;
            tree.ShowParameters = false;
            tree.Font = DrawUtils.DefaultFont;
            tree.AfterSelect += FTree_AfterSelect;
            host = new ToolStripControlHost(tree);
            Items.Add(host);
        }
    }
}
