#if NET40 || NET461

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media;

namespace Apewer.Surface
{

    /// <summary></summary>
    internal class FontInstance
    {

        private FontFamily _fontfamily = null;
        private Dictionary<string, string> _names = null;

        private FontInstance(FontFamily fontFamily) { _fontfamily = fontFamily; }

        /// <summary></summary>
        public Dictionary<string, string> FontFamiliy
        {
            get
            {
                if (_names == null)
                {
                    _names = new Dictionary<string, string>();
                    if (_fontfamily.FamilyNames.Keys.Count > 0)
                    {
                        foreach (var name in _fontfamily.FamilyNames)
                        {
                            var key = name.Key.IetfLanguageTag;
                            var value = name.Value;
                            if (!_names.ContainsKey(key)) _names.Add(key, value);
                        }
                    }
                }
                var result = new Dictionary<string, string>();
                foreach (var item in _names) result.Add(item.Key, item.Value);
                return result;
            }
        }

        /// <summary></summary>
        public List<string> EnemerateChars()
        {
            var list = new List<string>();
            if (_fontfamily == null) return list;
            var typefaces = _fontfamily.GetTypefaces();
            foreach (var typeface in typefaces)
            {
                GlyphTypeface glyph;
                var tried = typeface.TryGetGlyphTypeface(out glyph);
                if (glyph != null)
                {
                    list.Clear();
                    var map = glyph.CharacterToGlyphMap;
                    for (int i = 0; i < map.Count; i++)
                    {
                        long index = map.Keys.ElementAt(i);
                        try
                        {
                            var c = Convert.ToChar(index);
                            list.Add(c.ToString());
                        }
                        catch { }
                    }
                }
            }
            return list;
        }

        /// <summary></summary>
        public List<string> EnemerateChars(string fontFamily)
        {
            var list = new List<string>();
            if (string.IsNullOrEmpty(fontFamily)) return list;

            var fontfamily = new FontFamily(fontFamily);
            var typefaces = fontfamily.GetTypefaces();
            foreach (var typeface in typefaces)
            {
                GlyphTypeface glyph;
                var tried = typeface.TryGetGlyphTypeface(out glyph);
                if (glyph != null)
                {
                    var map = glyph.CharacterToGlyphMap;
                    for (int i = 0; i < map.Count; i++)
                    {
                        long index = map.Keys.ElementAt(i);
                        try
                        {
                            var c = Convert.ToChar(index);
                            list.Add(c.ToString());
                        }
                        catch { }
                    }
                }
            }
            return list;
        }

        /// <summary></summary>
        public static List<FontInstance> EnumerateSystemFontFamilies()
        {
            var list = new List<FontInstance>();
            var fontfamilies = Fonts.SystemFontFamilies;
            foreach (var fontfamily in fontfamilies)
            {
                list.Add(new FontInstance(fontfamily));
            }
            return list;
        }

        /// <summary></summary>
        public static List<FontInstance> FromFile(string path)
        {
            var list = new List<FontInstance>();
            var fontfamilies = Fonts.GetFontFamilies(path);
            foreach (var fontfamily in fontfamilies)
            {
                list.Add(new FontInstance(fontfamily));
            }
            return list;
        }

    }

}

#endif