using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Grid;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ExtensionsDev
{
    /// <summary>
    /// 
    /// </summary>
    public static class SearchLookUpEditExtension
    {
        /// <summary>
        /// 设置SearchLookUpEdit默认属性
        /// </summary>
        /// <param name="ctrl"></param>
        /// <param name="nullText">数据源</param>
        /// <param name="showClearBtn"></param>
        public static SearchLookUpEdit UseDefault(this SearchLookUpEdit ctrl, string nullText = "请选择", bool showClearBtn = false)
        {
            ctrl.EditValue = null;
            ctrl.Properties.NullText = null;
            ctrl.Properties.NullValuePrompt = nullText;
            ctrl.Properties.AllowFocused = false;
            ctrl.Properties.ShowNullValuePrompt = ShowNullValuePromptOptions.Default;
            ctrl.Properties.ShowAddNewButton = false;
            ctrl.Properties.ShowClearButton = showClearBtn;
            ctrl.Properties.ShowFooter = false;
            ctrl.Properties.ShowPopupShadow = true;
            return ctrl;
        }



        /// <summary>
        /// 绑定SearchLookUpEdit数据源
        /// </summary>
        /// <param name="ctrl"></param>
        /// <param name="data">数据源</param>
        public static SearchLookUpEdit SetStringData(this SearchLookUpEdit ctrl, IEnumerable<string> data)
        {
            ctrl.Properties.DataSource = data;
            GridView view = ctrl.Properties.View;
            view.Tag = ctrl;
            view.OptionsView.ShowColumnHeaders = false;
            view.GridControl.UseDefault<string>(null);
            return ctrl;
        }
        /// <summary>
        /// 绑定SearchLookUpEdit数据源
        /// </summary>
        /// <param name="ctrl"></param>
        /// <param name="data">数据源</param>
        /// <param name="displayField"></param>
        public static SearchLookUpEdit SetData<T>(this SearchLookUpEdit ctrl, List<T> data, string displayField = "")
        {
            ctrl.QueryPopUp += Ctrl_QueryPopUp;
            ctrl.Properties.DataSource = data;
            GridView view = ctrl.Properties.View;
            view.Tag = ctrl;
            view.OptionsView.ShowColumnHeaders = true;
            view.GridControl.UseDefault<T>(null);
            ctrl.CustomDisplayText += (sender, e) =>
            {
                if (e.Value == null) return;
                if (typeof(T) != e.Value.GetType())
                {
                    e.DisplayText = e.Value.ToString();
                }
                else if (string.IsNullOrWhiteSpace(displayField))
                {
                    e.DisplayText = e.Value.ToString();
                }
                else
                {
                    e.DisplayText = typeof(T).GetProperty(displayField).GetValue(e.Value).ToString();
                }
            };
            return ctrl;
        }

        private static void Ctrl_QueryPopUp(object sender, System.ComponentModel.CancelEventArgs e)
        {
            var ctrl = sender as SearchLookUpEdit;
            var view = ctrl.Properties.View;
            var dataType = view.DataSource.GetType().GetGenericArguments().Single();
            var props = dataType.GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance);
            foreach (GridColumn column in view.Columns)
            {
                if (column.Visible)
                {
                    var prop = props.First(p => p.Name == column.FieldName);
                    var attrDisplay = prop.GetCustomAttribute<DisplayAttribute>();
                    var attrDisplayName = prop.GetCustomAttribute<DisplayNameAttribute>();
                    if (attrDisplay == null && attrDisplayName == null)
                        column.Visible = false;
                    else if (attrDisplay != null && attrDisplay.GetAutoGenerateFilter() != null && !attrDisplay.AutoGenerateFilter)
                        column.Visible = false;
                    else
                        column.Visible = true;
                }
            }

        }

    }
}
