using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media.Imaging;

namespace NEIntelligentControl2.Models
{
    public static class Helper
    {
        /// <summary>
        /// 获取字符串的配置值
        /// </summary>
        /// <param name="cfgname"></param>
        /// <returns></returns>
        public static string GetConfiguration(this string cfgname)
        {
            try
            {
                return System.Configuration.ConfigurationManager.AppSettings[cfgname];
            }
            catch { }
            return "";
        }



        /// <summary>
        /// 从时间戳获取时间字符串
        /// </summary>
        /// <param name="l">时间戳</param>
        /// <returns>时间字符串</returns>
        public static string GetTimeString(this long l)
        {
            if (l <= 10000) return "";

            DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
            return startTime.AddMilliseconds(l).ToString("yyyy-MM-dd hh:mm:ss");
        }



        /// <summary>
        /// 从时间戳获取时间字符串
        /// </summary>
        public static string GetTimeSpanString(this long l)
        {
            if (l < 10000) return "";

            DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
            startTime = startTime.AddMilliseconds(l);

            TimeSpan tts = DateTime.Now - startTime;
            var ll = (long)tts.TotalMinutes;
            ll = ll < 0 ? 0 : ll;
            return ll.ToString();
        }


        /// <summary>
        /// 获取实例中的值
        /// </summary>
        public static object GetValue(this object op, string name)
        {
            string[] names = name.Split('.');
            object obj = null;
            if (names.Length == 1)
            {
                obj = op.GetType().GetProperty(name)?.GetValue(op);
            }
            else
            {
                var v1 = op.GetType().GetProperty(names[0])?.GetValue(op);
                if (v1 != null)
                    obj = v1.GetType().GetProperty(names[1])?.GetValue(v1);
            }
            return obj;
        }


        /// <summary>
        /// 获取枚举的字符
        /// </summary>
        public static string GetStringValue(this System.Enum value)
        {
            string str = "";
            var type = value.GetType();
            var fi = type.GetField(value.ToString());
            var attrs = fi.GetCustomAttributes(typeof(Attributes.StringValue), false) as Attributes.StringValue[];
            if(attrs.Length > 0)
            {
                str = attrs[0].Value;
            }
            return str;
        }

        /// <summary>
        /// 从时间获取时间戳值
        /// </summary>
        /// <param name="dateTime"></param>
        /// <returns></returns>
        public static long GetTimeSpan(this DateTime dateTime)
        {
            DateTime dt = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
            TimeSpan time = dateTime.Subtract(dt);

            return long.Parse(time.Ticks.ToString().Substring(0, time.Ticks.ToString().Length - 4));
        }
        /// <summary>
        /// 从时间戳获取时间
        /// </summary>
        public static DateTime GetLongDateTime(this long utc)
        {
            DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
            startTime = startTime.AddMilliseconds(utc);
            return startTime;
        }

        public static BitmapImage GetBitmapImage(this Bitmap obj)
        {
            using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
            {
                obj.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
                var bs = ms.GetBuffer();

                BitmapImage bi = new BitmapImage();
                bi.BeginInit();
                bi.StreamSource = new System.IO.MemoryStream(bs);
                bi.EndInit();
                return bi;
            }
        }
    }
}
