using MWS.Library.Common;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Data;

namespace MWS.Library.Enums
{
    public class EnumConverter : IValueConverter
    {
        public EnumConverter()
        {
        }

        private object enumName;

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null)
            {
                return "-";
            }
            Type type = Assembly.Load("HDCraneCIMS.Common").ExportedTypes.FirstOrDefault(c => c.Name == parameter.ToString());
            ArrayList list = null;
            string result = string.Empty;
            if (targetType.Name == "String")
            {
                list = type.ToArrayList();
            }
            else if (targetType.Name == "Brush")
            {
                list = type.ToDefalutValueList();
                result = "#000000";
            }
            if (Regex.Matches(value.ToString(), "[a-zA-Z]").Count > 0)
            {
                enumName = Enum.Parse(type, value.ToString());
            }
            else
            {
                enumName = Enum.GetName(type, value);

            }




            if (enumName == null)
            {
                return "NULL";
            }
            foreach (KeyValuePair<Enum, string> item in list)
            {
                if (enumName.ToString() == item.Key.ToSafeString())
                {
                    return item.Value;
                }
            }
            return result;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
