using FLY.OBJComponents.Common;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows;

namespace FLY.Thick.Base.Common
{
    /// <summary>
    /// 异常常量
    /// </summary>
    public class ERRNOs
    {
        protected static Dictionary<int, string> errnoDist;


        static ERRNOs()
        {
            errnoDist = new Dictionary<int, string>();
            updateErrnoDist(typeof(ERRNOs));
        }
        protected static void updateErrnoDist(Type errnos_type) 
        {
            string proStr = "str.Thk.ERRNO";

            Type t = errnos_type;

            var fields = t.GetFields(BindingFlags.Static | BindingFlags.Public);//一定要写 public

            foreach (var field in fields)
            {
                if (field.Name.StartsWith("ERRNO_"))
                {
                    var errno = (ERRNO)field.GetValue(null);

                    var ecode = errno.Code;

                    string rd_key = proStr + "." + field.Name;

                    if (errnoDist.ContainsKey(ecode))
                        continue;//已经加载过了

                    errno.Description = (string)Application.Current.FindResource(rd_key);

                    field.SetValue(null, errno);

                    errnoDist.Add(ecode, rd_key);
                }
            }
        }
        public static List<ERRNO> GetAll()
        {
            List<ERRNO> list = new List<ERRNO>();
            foreach (var code in errnoDist.Keys)
            {

                list.Add(new ERRNO() { Code = code, Description = GetDescription(code) });
            }
            return list;
        }
        //string ToDescription(ResourceDictionary rd_errnos, string errNoKey)
        //{
        //    string resourceName = $"str.ERRNO.{errNoKey}";
        //    return (string)rd_errnos[resourceName];//异常直接让它出错
        //}

        public static string GetDescription(int errcode)
        {
            string str_unknown = "unknown";

            
            if(errnoDist.TryGetValue(errcode, out string rkey))
            {
                string msg = (string)Application.Current.FindResource(rkey);
                return msg;
            }
            else
            {
                return str_unknown;
            }
        }
        public static ERRNO ERRNO_AUTH_EXP = new ERRNO() { Code = 0};

        public static ERRNO ERRNO_FLYAD7_DC = new ERRNO() { Code = 1};

        public static ERRNO ERRNO_AD_MIN = new ERRNO() { Code = 2};

        public static ERRNO ERRNO_AD_MAX = new ERRNO() { Code = 3};

        public static ERRNO ERRNO_SCRAM = new ERRNO() { Code = 4 };

        public static ERRNO ERRNO_LIMIT = new ERRNO() { Code = 5};

        public static ERRNO ERRNO_OVERTOL = new ERRNO() { Code = 6};

        public static ERRNO ERRNO_SCAN_FATAL = new ERRNO() { Code = 7};

        public static ERRNO ERRNO_SAMPLE_FATAL = new ERRNO() { Code = 8};

        public static ERRNO ERRNO_ALARM_TEST = new ERRNO() { Code = 9};

        public static ERRNO ERRNO_WILL_EXP = new ERRNO() { Code = 10 };

        public static ERRNO ERRNO_OVERCTRL = new ERRNO() { Code = 11 };
        
        public static ERRNO ERRNO_DB_BACKUP = new ERRNO() { Code = 12 };        
    }

}
