using System;
using System.IO;
using System.Collections.Generic;

namespace InvExplorer.Web
{
    /// <summary>
    /// Used only to transfer the list of asset symbols and names to the client.
    /// </summary>
    public class SymbolName : IComparable<SymbolName>
    {
        public string Symbol { get; set; }
        public string Name { get; set; }

        public int CompareTo(SymbolName other)
        {
            return this.Symbol.CompareTo(other.Symbol);
        }

        // ** utilities to load from resource stream
        internal static List<SymbolName> LoadSymbolNames()
        {
            var list = new List<SymbolName>();

            // parse resource
            using (var stream = GetResourceStream("AssetSymbols.txt"))
            using (var sr = new StreamReader(stream))
            {
                for (string line = sr.ReadLine(); line != null; line = sr.ReadLine())
                {
                    var parts = line.Split('\t');
                    if (parts.Length > 1)
                    {
                        var asn = new SymbolName();
                        asn.Symbol = parts[0].Trim();
                        asn.Name = parts[1].Trim();
                        if (!string.IsNullOrEmpty(asn.Symbol) && !string.IsNullOrEmpty(asn.Name))
                        {
                            list.Add(asn);
                        }
                        else
                        {
                            System.Diagnostics.Debug.Assert(false, "shouldn't have assets with no name and symbol...");
                        }
                    }
                }
            }

            // sort by symbol
            list.Sort();

            // done
            return list;
        }
        static Stream GetResourceStream(string name)
        {
            var asm = System.Reflection.Assembly.GetExecutingAssembly(); ;
            foreach (var rn in asm.GetManifestResourceNames())
            {
                if (rn.EndsWith(name))
                {
                    return asm.GetManifestResourceStream(rn);
                }
            }
            return null;
        }
    }
}