using WCS.Core;

namespace WCS.WorkEngineering.Extensions
{
    /// <summary>
    ///  设备标签扩展
    /// </summary>
    public static class DeviceFlagExtension
    {
        /// <summary>
        ///  获取指定设备标签的值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="device"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static T GetFlag<T>(this Device device, string key)
        {
            if (!device.HasFlag(key))
            {
                if (typeof(T) == typeof(int)) device.AddFlag(key, default(int).ToString());
                else if (typeof(T) == typeof(short)) device.AddFlag(key, default(bool).ToString());
                else if (typeof(T) == typeof(bool)) device.AddFlag(key, default(bool).ToString());
            }
            var valeu = device.GetFlags(key).FirstOrDefault();

            return (T)Convert.ChangeType(valeu, typeof(T));
        }

        /// <summary>
        ///  设置指定设备标签的值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="device"></param>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static void SetFlag<T>(this Device device, string key, T value)
        {
            device.SetFlag(key, value.ToString());
        }
    }
}
