using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Cornerstone.Database.Tables;
using Cornerstone.GUI.Controls;
using Cornerstone.Database;

namespace Cornerstone.GUI.Filtering {
    public partial class GenericNodeSettingsPanel<T> : UserControl, IFieldDisplaySettingsOwner, INodeSettingsPanel 
        where T : DatabaseTable {

        private bool updating = false;

        public GenericNodeSettingsPanel() {
            InitializeComponent();
        }

        [Category("Cornerstone Settings")]
        [Description("The friendly name for the type of objects being filtered (plural).")]
        public string DisplayName {
            get { return _displayName; }
            set {
                _displayName = value;
            }
        } private string _displayName = "items";

        [Browsable(false)]
        public DatabaseManager DBManager {
            get {
                return _dbManager;
            }
            set {
                _dbManager = value;
            }
        } private DatabaseManager _dbManager;

        public IDBNode Node {
            set {
                if (_node != null)
                    _node.Modified -= new DBNodeEventHandler(NodeModified); 

                if (value is DBNode<T> || value == null) {
                    _node = (DBNode<T>)value;

                    if (_node != null)
                        _node.Modified += new DBNodeEventHandler(NodeModified);             
                }

                PopulateControls();
            }

            get {
                return _node;
            }
        } private DBNode<T> _node = null;

        [Category("Cornerstone Settings")]
        [Description("Manage the type of database table this control connects to and which fields should be displayed.")]
        public FieldDisplaySettings FieldDisplaySettings {
            get {
                if (_fieldSettings == null) {
                    _fieldSettings = new FieldDisplaySettings();
                    _fieldSettings.Owner = this;
                }

                return _fieldSettings;
            }

            set {
                _fieldSettings = value;
                _fieldSettings.Owner = this;
                //initControls();
            }
        } private FieldDisplaySettings _fieldSettings = null;

        public TranslationParserDelegate TranslationParser {
            get { return _translationParser; }
            set { _translationParser = value; }
        } TranslationParserDelegate _translationParser = null;

        public bool ShowFilterHelpButton {
            get;
            set;
        }

        public HelpActionDelegate FilterHelpAction {
            get;
            set;
        }

        public void OnFieldPropertiesChanged() {
            //initControls();
        }

        private void NodeModified(IDBNode node) {
            PopulateControls();
        }

        private void PopulateControls() {
            updating = true;

            if (_node == null) {
                addEditFilterButton.Enabled = false;
                removeFilterButton.Enabled = false;
                nameTextBox.Enabled = false;
                nameTextBox.Text = "";
                return;
            }

            string displayName = _node.Name;
            if (TranslationParser != null)
                displayName = TranslationParser(_node.Name);

            nameTextBox.Text = displayName;
            nameTextBox.Enabled = true;

            if (_node.Filter == null) {
                removeFilterButton.Enabled = false;
                addEditFilterButton.Text = "Add Filter";
            }
            else {
                removeFilterButton.Enabled = true;
                addEditFilterButton.Text = "Edit Filter";
            }

            if (_node.AutoGenerated) {
                addEditFilterButton.Enabled = false;
                removeFilterButton.Enabled = false;
            }
            else {
                addEditFilterButton.Enabled = true;
            }

            updating = false;
        }

        private void nameTextBox_TextChanged(object sender, EventArgs e) {
            if (_node != null && !updating) {
                string currentName = _node.Name;
                if (TranslationParser != null)
                    currentName = TranslationParser(_node.Name);

                if (nameTextBox.Text != currentName)
                    _node.Name = nameTextBox.Text;
            }
        }

        private void addEditFilterButton_Click(object sender, EventArgs e) {
            FilterEditorPopup popup = new FilterEditorPopup();
            popup.FilterEditorPanel.FieldDisplaySettings = FieldDisplaySettings;
            popup.FilterEditorPanel.DisplayName = DisplayName;
            popup.FilterEditorPanel.DBManager = DBManager;
            popup.ShowHelpButton = ShowFilterHelpButton;
            popup.HelpAction = FilterHelpAction;

            DBFilter<T> filter = _node.Filter;
            if (filter == null) {
                filter = new DBFilter<T>();
                filter.Name = _node.Name;
            }

            popup.FilterEditorPanel.AttachedFilter = filter;
            popup.ShowDialog();

            if (_node.Filter == null) 
                _node.Filter = filter;
            else
                _node.OnModified();

            PopulateControls();
        }

        private void removeFilterButton_Click(object sender, EventArgs e) {
            _node.Filter.Delete();
            _node.Filter = null;
        }
    }

    public interface INodeSettingsPanel {
        string DisplayName { get; set; }
        DatabaseManager DBManager { get; set; }

        TranslationParserDelegate TranslationParser { get; set; }

        bool ShowFilterHelpButton { get; set; }
        HelpActionDelegate FilterHelpAction { get; set; }

        IDBNode Node {
            get;
            set;
        }
    }
}
