using Apewer.Internals;
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;

namespace Apewer
{

    /// <summary>标题特性。</summary>
    [Serializable]
    [AttributeUsage(AttributeTargets.All, AllowMultiple = false, Inherited = true)]
    public class CaptionAttribute : Attribute
    {

        private string _title, _description, _remark;

        /// <summary></summary>
        public CaptionAttribute(string title = null, string description = null, string remark = null)
        {
            Title = title;
            Description = description;
            Remark = remark;
        }

        /// <summary></summary>
        public string Title
        {
            get { return _title; }
            set { _title = value ?? Constant.EmptyString; }
        }

        /// <summary></summary>
        public string Description
        {
            get { return _description; }
            set { _description = value ?? Constant.EmptyString; }
        }

        /// <summary></summary>
        public string Remark
        {
            get { return _remark; }
            set { _remark = value ?? Constant.EmptyString; }
        }

        /// <summary>从 <see cref="CaptionAttribute"/> 到 Boolean 的隐式转换,判断 <see cref="CaptionAttribute"/> 有效。</summary>
        public static implicit operator bool(CaptionAttribute instance) => instance != null;

        #region static

        static Dictionary<string, CaptionAttribute> _cache = new Dictionary<string, CaptionAttribute>();

        /// <summary>解析标题特性,默认使用缓存以提升性能。</summary>
        /// <remarks>若修改已缓存实例的属性值,则下次从缓存中获取实例时将带有新的属性值。</remarks>
        /// <returns>已存在的 <see cref="CaptionAttribute"/> 实例。</returns>
        public static CaptionAttribute Parse(Type type, bool useCache = true)
        {
            if (type == null) return null;

            var cacheKey = type.FullName;
            if (useCache)
            {
                var hint = null as CaptionAttribute;
                lock (_cache)
                {
                    if (_cache.ContainsKey(cacheKey))
                    {
                        hint = _cache[cacheKey];
                    }
                }
                if (hint != null) return hint;
            }

            var attributes = type.GetCustomAttributes(typeof(CaptionAttribute), false);
            if (attributes.LongLength < 1L) return null;
            var attribute = (CaptionAttribute)attributes[0];

            if (useCache)
            {
                lock (_cache)
                {
                    if (!_cache.ContainsKey(cacheKey)) _cache.Add(cacheKey, attribute);
                }
            }

            return attribute;
        }

        /// <summary>解析标题特性,默认使用缓存以提升性能。</summary>
        /// <remarks>若修改已缓存实例的属性值,则下次从缓存中获取实例时将带有新的属性值。</remarks>
        /// <returns>已存在的 <see cref="CaptionAttribute"/> 实例。</returns>
        public static CaptionAttribute Parse<T>(bool useCache = true) => Parse(typeof(T), useCache);

        #endregion

    }

}