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 ryControls;
using ryControls.Skin;
using System.Drawing.Drawing2D;

namespace ryControls
{
    [DefaultEvent("OnSearch")]
    public partial class rySearch : UserControl
    {
        /// <summary>
        /// 在搜索时激发
        /// </summary>
        [Description("在搜索时激发")]
        public event EventHandler OnSearch;
        /// <summary>
        /// 文本变化时激发
        /// </summary>
        [Description("文本变化时激发")]
        public new event EventHandler OnTextChanged;
        /// <summary>
        /// 文本变化时激发
        /// </summary>
        [Description("文本变化时激发")]
        public new event EventHandler TextChanged;
        /// <summary>
        /// 双击时激发
        /// </summary>
        [Description("双击时激发")]
        public new event EventHandler DoubleClick;
        /// <summary>
        /// 
        /// </summary>
        public rySearch()
        {
            InitializeComponent();
            txtSearch.Font = Font;
            txtSearch.ImeMode = ImeMode;
            base.GotFocus += RySearch_GotFocus;
            this.SetStyle(
                ControlStyles.AllPaintingInWmPaint | //忽略擦出的消息，减少闪烁。
                ControlStyles.OptimizedDoubleBuffer |//在缓冲区上绘制，不直接绘制到屏幕上，减少闪烁。
                ControlStyles.ResizeRedraw | //控件大小发生变化时，重绘。                  
                ControlStyles.SupportsTransparentBackColor, true);//支持透明背景颜色
        }

        private void RySearch_GotFocus(object sender, EventArgs e)
        {
            txtSearch.Focus();
            txtSearch.Select();
            txtSearch.SelectionLength = 0;
        }
        /// <summary>
        /// 
        /// </summary>
        public new void Select()
        {
            //txtSearch.Select();
            txtSearch.Focus();
        }
        private string _EmptyText = "";
        /// <summary>
        /// 文本为空时的显示效果
        /// </summary>
        private void EmptyShow()
        {
            if (_text == "")
            {
                if (!txtSearch.Focused)
                {
                    txtSearch.Font = Font;
                    txtSearch.ForeColor = Color.FromArgb(175, 185, 200);
                    isProcUse = true; txtSearch.Text = EmptyText; isProcUse = false;
                    txtSearch.Select(0, 1);
                    txtSearch.Select(0, 0);
                }
                else
                { HaveTextShow(); txtSearch.Text = ""; }
            }
        }
        /// <summary>
        /// 文本不选中
        /// </summary>
        public void TextUnSelected()
        {
            txtSearch.Select(0, 0);
        }
        /// <summary>
        /// 有内容时的显示效果
        /// </summary>
        private void HaveTextShow()
        {
            txtSearch.Font = Font;
            txtSearch.ForeColor = Color.Black;
        }
        /// <summary>
        /// 当文本框为空时,显示的内容。
        /// </summary>
        [Description("当文本框为空时,显示的内容。")]
        public string EmptyText
        {
            get { return _EmptyText; }
            set {
                _EmptyText = value;
                EmptyShow();
            }
        }
        private Font _font = new Font("宋体",9);
        /// <summary>
        /// 字体
        /// </summary>
        public override Font Font
        {
            get
            {
                return _font;
            }

            set
            {
                _font = value;
                txtSearch.Font = value;
                btnSearch.Font = value;
            }
        }
        private bool _UseDefSkin = true;
        /// <summary>
        ///优先使用默认皮肤
        /// </summary>
        [Description("优先使用默认皮肤")]
        [DefaultValue(typeof(bool), "true")]
        public bool UseDefSkin
        {
            get
            {
                return _UseDefSkin;
            }
            set
            {
                _UseDefSkin = value;
                btnSearch.UseDefSkin = _UseDefSkin;
                base.Invalidate();
            }
        }
        /// <summary>
        /// 背景色是否渐变
        /// </summary>
        [DefaultValue(false)]
        [Description("背景色是否渐变")]
        public bool ColorGradient
        {
            get { return btnSearch.ColorGradient; }
            set
            {
                if (value != btnSearch.ColorGradient)
                {

                    btnSearch.ColorGradient = value;
                }
            }
        }
        private Color _baseColor = Color.FromArgb(12, 125, 182);//基颜色
        /// <summary>
        ///按钮基础背景色
        /// </summary>
        [Description("按钮基础背景色")]
        [DefaultValue(typeof(Color), "12, 125, 182")]
        public Color BaseColor
        {
            get
            {
                //if (SkinCommon.UseDefSkin && UseDefSkin)
                //{
                //    return SkinCommon.ButtonSkin.BackColor;
                //}
                return _baseColor;
            }
            set
            {
                _baseColor = value;
                btnSearch.BaseColor = _baseColor;
                //if (SkinCommon.UseDefSkin && UseDefSkin)
                //{
                //    btnSearch.BaseColor = SkinCommon.ButtonSkin.BackColor;
                //}
                //else
                //{
                //    btnSearch.BaseColor = _baseColor;
                //}
            }
        }
        /// <summary>
        ///按钮圆角部分背景颜色
        /// </summary>
        [Description("按钮圆角部分背景颜色")]
        public Color ButtonRoundBackColor
        {
            get
            {
                return panel1.BackColor;
            }
            set
            {
                panel1.BackColor = value;
            }
        }
        private string _text = "";
        /// <summary>
        /// 
        /// </summary>
        public override string Text
        {
            get
            {
                return _text;
            }

            set
            {
                _text = value;
                if(_text=="")
                {
                    EmptyShow();
                }
                else
                {
                    HaveTextShow(); txtSearch.Text = _text;
                    //txtSearch.Select(0, 1);
                    //txtSearch.Select(_text.Length, 0);
                }
            }
        }
        /// <summary>
        /// 重新设置大小
        /// </summary>
        public void ResizeSize()
        {
            txtSearch.Location = new Point(3, 1);
            txtSearch.Top = (Height - txtSearch.Height) / 2;
            //Height = txtSearch.Height+2;
            //btnSearch.Height = Height;
            //btnSearch.Width = Height;
            //btnSearch.Location = new Point(Width - btnSearch.Width, 0);
            panel1.Width = panel1.Height;
            txtSearch.Width = Width - panel1.Width-3;
        }

        private void RySearch_Load(object sender, EventArgs e)
        {
            ResizeSize();
            //EmptyShow();
        }

        private void RySearch_FontChanged(object sender, EventArgs e)
        {
            ResizeSize();
        }

        private void TxtSearch_Enter(object sender, EventArgs e)
        {
            HaveTextShow();
            if (Text == "") { txtSearch.Text = ""; }
        }
        private bool isProcUse = false;
        private void TxtSearch_Leave(object sender, EventArgs e)
        {
            if (Text == "")
            {
                EmptyShow();
            }
            else
            { txtSearch.Font = Font; }
        }
        private void TxtSearch_TextChanged(object sender, EventArgs e)
        {
            if (isProcUse) { return; }
            Text = txtSearch.Text;
            OnTextChanged?.Invoke(this, new EventArgs());
            TextChanged?.Invoke(this, new EventArgs());
        }
        /// <summary>
        /// 点击搜索按钮
        /// </summary>
        public void PerformClick()
        {
            OnSearch?.Invoke(this, new EventArgs());
        }
        private void BtnSearch_Click(object sender, EventArgs e)
        {
            OnSearch?.Invoke(this, new EventArgs());
        }

        private void TxtSearch_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter) { btnSearch.PerformClick(); }
            //if(KeyDown!=null)
            OnKeyDown(e);
        }

        private void RySearch_Resize(object sender, EventArgs e)
        {
            ResizeSize();
        }

        private void RySearch_Paint(object sender, PaintEventArgs e)
        {
            e.Graphics.DrawRectangle(new Pen(SkinHelp.DefalutBorderColor), new Rectangle(0, 0, this.Width - 1, this.Height - 1));
        }

        private void TxtSearch_DoubleClick(object sender, EventArgs e)
        {
            DoubleClick?.Invoke(this, new EventArgs());
        }

        private void RySearch_ImeModeChanged(object sender, EventArgs e)
        {
            txtSearch.ImeMode = base.ImeMode;
        }

        private void RySearch_Enter(object sender, EventArgs e)
        {
            txtSearch.Focus();
        }

        private void TxtSearch_Click(object sender, EventArgs e)
        {
            HaveTextShow();
            if (Text == "") { txtSearch.Text = ""; }
        }

        private void RySearch_Leave(object sender, EventArgs e)
        {
            EmptyShow();
        }
    }
}
