using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Reflection;
using System.IO;
using Mesnac.Controls.Base;

namespace Mesnac.Controls.Default
{
    [ToolboxBitmap(typeof(SpecialLabelGreen), "Resources.SpecialLabelGreen.png")]
    public partial class SpecialLabelGreen : Label, IBaseControl
    {
        #region 字段定义

        protected Assembly _assembly = Assembly.GetExecutingAssembly();
        protected Stream _imageStream;

        private bool _mcVisible = true; //保存可见性
        private bool _mcEnabled = true; //保存可用性
        private bool _isValid = true; //保存有效性
        private bool _isEventValid = true;      //保存事件有效性
        private bool _bHaveAction = false;
        private string _format = string.Empty;
        private string _textName = null;
        private string _textValue = string.Empty;
        private string _xName = null;
        private int? _xValue = null;
        private string _yName = null;
        private int? _yValue = null;
        private string _widthName = null;
        private int? _widthValue = null;
        private string _heightName = null;
        private int? _heightValue = null;

        #endregion

        #region 构造方法

        public SpecialLabelGreen()
        {
            InitializeComponent(); 
            this.Size = new Size(116, 23);
            this.TextAlign = ContentAlignment.MiddleCenter; //文字对齐
            this.ForeColor = Color.White; //文字颜色
            _imageStream = _assembly.GetManifestResourceStream("Mesnac.Controls.Default.Resources.SpecialLabelGreen.png");
            this.BackgroundImage = Image.FromStream(_imageStream);
            this.BackgroundImageLayout = ImageLayout.Stretch;
        }

        public SpecialLabelGreen(IContainer container)
        {
            container.Add(this);

            InitializeComponent();
        }

        #endregion

        #region 属性定义

        #region bHaveAction

        public bool bHaveAction
        {
            get
            {
                return _bHaveAction;
            }
            set
            {
                _bHaveAction = value;
            }
        }
        #endregion

        #region Text

        [Description("格式化"), Category("Data")]
        public string Format
        {
            get
            {
                return _format;
            }
            set
            {
                _format = value;
            }
        }

        public string TextName
        {
            get
            {
                return _textName;
            }
            set
            {
                _textName = value;
            }
        }

        public string TextValue
        {
            set
            {
                this._textValue = value;
                if (!this.DesignMode)
                {
                    if (string.IsNullOrWhiteSpace(this._format))
                    {
                        this.Text = this._textValue;
                    }
                    else
                    {
                        try
                        {
                            this.Text = this.ExecFormat(this._format, this._textValue);
                        }
                        catch { }
                    }
                }
            }
            get
            {
                return this._textValue;
            }
        }
        #endregion

        #region Left

        public string XName
        {
            get
            {
                return _xName;
            }
            set
            {
                _xName = value;
            }
        }
        public int? XValue
        {
            set
            {
                this._xValue = value;
                if (!this.DesignMode)
                {
                    if (this._xValue != null)
                    {
                        this.Left = (int)this._xValue;
                    }
                }
            }
            get
            {
                return this._xValue;
            }
        }
        #endregion

        #region Top
        public string YName
        {
            get
            {
                return _yName;
            }
            set
            {
                _yName = value;
            }
        }
        public int? YValue
        {
            set
            {
                this._yValue = value;
                if (!this.DesignMode)
                {
                    if (this._yValue != null)
                    {
                        this.Top = (int)this._yValue;
                    }
                }
            }
            get
            {
                return this._yValue;
            }
        }
        #endregion

        #region Width

        public string WidthName
        {
            get
            {
                return _widthName;
            }
            set
            {
                _widthName = value;
            }
        }

        public int? WidthValue
        {
            set
            {
                this._widthValue = value;
                if (!this.DesignMode)
                {
                    if (this._widthValue != null)
                    {
                        this.Width = (int)this._widthValue;
                    }
                }
            }
            get
            {
                return this._widthValue;
            }
        }
        #endregion

        #region Height

        public string HeightName
        {
            get
            {
                return _heightName;
            }
            set
            {
                _heightName = value;
            }
        }

        public int? HeightValue
        {
            set
            {
                this._heightValue = value;
                if (!this.DesignMode)
                {
                    if (this._heightValue != null && !this.DesignMode)
                    {
                        this.Height = (int)this._heightValue;
                    }
                }
            }
            get
            {
                return this._heightValue;
            }
        }
        #endregion

        #endregion

        #region 接口成员实现

        public string MCKey
        {
            get;
            set;
        }

        public object MCValue
        {
            get
            {
                return this.Text;
            }
            set
            {
                this.Text = value == null ? "" : value.ToString();
            }
        }

        public IBaseControl MCRoot
        {
            get;
            set;
        }

        [TypeConverter(typeof(DataSourceConverter))]

        [Description("数据连接"), Category("Data")]
        public string MCDataSourceID
        {
            get;
            set;
        }

        public MCDataSource MCDataSource
        {
            get;
            set;
        }

        [Description("是否为数据库控件"), Category("Data")]
        public bool IsDbControl
        {
            get;
            set;
        }

        [Description("初始化SQL"), Category("Data")]
        public string InitDataSource
        {
            get;
            set;
        }
        [Description("执行SQL"), Category("Data")]
        public string ActionDataSource
        {
            get;
            set;
        }

        [Description("绑定数据源"), Category("Data")]
        public object BindDataSource
        {
            get;
            set;
        }

        [Description("操作类型"), Category("Data")]
        public DbOptionTypes DbOptionType
        {
            get;
            set;
        }

        [Description("是否可见"), Category("Behavior")]
        public bool MCVisible
        {
            get
            {
                return this._mcVisible;
            }
            set
            {
                this._mcVisible = value == null ? true : value;
                if (this.Site.DesignMode)
                {
                    this.Visible = true;
                }
                else
                {
                    this.Visible = this._mcVisible;
                }
            }
        }

        [Description("是否可用"), Category("Behavior")]
        public bool MCEnabled
        {
            get
            {
                return this._mcEnabled;
            }
            set
            {
                this._mcEnabled = value == null ? true : value;
                if (this.Site.DesignMode)
                {
                    this.Enabled = true;
                }
                else
                {
                    this.Enabled = this._mcEnabled;
                }
            }
        }

        public bool IsValid
        {
            get { return _isValid; }
            set { _isValid = value; }
        }

        public bool IsEventValid
        {
            get { return this._isEventValid; }
            set { this._isEventValid = value; }
        }

        #endregion

        #region 辅助方法

        #region 执行格式化方法
        /// <summary>
        /// 执行格式化方法
        /// </summary>
        /// <param name="_format">格式化字符串</param>
        /// <param name="strValue">要格式化的值</param>
        /// <returns>返回格式化之后的字符串</returns>
        public string ExecFormat(string _format, string strValue)
        {
            try
            {
                string result = "";
                DateTime dateResult = DateTime.Now;
                bool boolResult = false;
                byte byteResult = 0;
                short shortResult = 0;
                int intResult = 0;
                decimal decimalResult = 0;
                double doubleResult = 0.0;

                //日期和时间
                if (strValue.IndexOf("-") > 0 || strValue.IndexOf(":") > 0)
                {
                    if (DateTime.TryParse(strValue, out dateResult))
                    {
                        result = string.Format(_format, dateResult);
                    }
                }
                else if (strValue.IndexOf(".") > 0)
                {
                    if (decimal.TryParse(strValue, out decimalResult))
                    {
                        result = string.Format(_format, decimalResult);
                    }
                    else if (double.TryParse(strValue, out doubleResult))
                    {
                        result = string.Format(_format, doubleResult);
                    }
                }
                else if (bool.TryParse(strValue, out boolResult))
                {
                    result = boolResult.ToString();
                }
                else if (byte.TryParse(strValue, out byteResult))
                {
                    result = byteResult.ToString();
                }
                else if (short.TryParse(strValue, out shortResult))
                {
                    result = shortResult.ToString();
                }
                else if (int.TryParse(strValue, out intResult))
                {
                    result = intResult.ToString();
                }
                else
                {
                    result = strValue;
                }
                return result;
            }
            catch
            {
                return strValue;
            }
        }

        #endregion

        #endregion


    }
}