using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows;
using Aitex.Core.RT.Log;
namespace Aitex.Core.UI.Converters
{
    public class LineColorConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            try
            {
                Color color = (Color)value;
                return new SolidColorBrush(color);
            }
            catch (Exception ex)
            {
                LOG.WriteExeption(ex);
            }
            return Brushes.Black;
        }
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            try
            {
                Brush brush = (SolidColorBrush)value;
                Color color = (Color)ColorConverter.ConvertFromString(brush.ToString());
                return color;
            }
            catch (Exception ex)
            {
                LOG.WriteExeption(ex);
            }
            return Colors.Black;
        }
    }
    public class bool2VisibilityConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            try
            {
                if ((bool)value)
                    return Visibility.Visible;
                return Visibility.Hidden;
            }
            catch (Exception ex)
            {
                LOG.WriteExeption(ex);
            }
            return Visibility.Hidden;
        }
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return null;
        }
    }
    public class enum2BoolConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            try
            {
                if (0 == (int)value)
                    return true;
                return false;
            }
            catch (Exception ex)
            {
                LOG.WriteExeption(ex);
            }
            return false;
        }
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return null;
        }
    }
    public class WaferSizeToVisibility : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            try
            {
                if (int.TryParse(value.ToString().Replace("WS",""), out int size) && size==0)
                    return Visibility.Collapsed;
                return Visibility.Visible;
            }
            catch (Exception ex)
            {
                LOG.WriteExeption(ex);
            }
            return Visibility.Visible;
        }
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return null;
        }
    }
    
    public class Visibility2boolConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            try
            {
                Visibility vi = (Visibility)value;
                if (vi == Visibility.Visible) return true;
                return false;
            }
            catch (Exception ex)
            {
                LOG.WriteExeption(ex);
            }
            return true;
        }
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            try
            {
                bool isChecked = (bool)value;
                if (isChecked) return Visibility.Visible;
                return Visibility.Hidden;
            }
            catch (Exception ex)
            {
                LOG.WriteExeption(ex);
            }
            return Visibility.Visible;
        }
    }
    public class RolloverDataTimeConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value != null)
            {
                DateTime dt;
                var isSucc = DateTime.TryParse(value.ToString(), out dt);
                if (isSucc)
                    return string.Format("{0}{1}{2}", " 【", dt.ToString("yyyy/MM/dd HH:mm:ss"), "】 ");
            }
            return "N/A";
        }
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return null;
        }
    }
    public class BoolToValueConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null) return null;
            return ((bool)value) ? parameter : DependencyProperty.UnsetValue;
        }
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null) return null;
            return object.Equals(value, parameter);
        }
    }
    public class RadioBoolToIntConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            int integer = (int)value;
            if (integer == int.Parse(parameter.ToString()))
                return true;
            else
                return false;
        }
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return parameter;
        }
    }
    public class FlowConverterForWeiQiMultiBinding : IMultiValueConverter
    {
        #region IMultiValueConverter 成员
        public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            string str_Result = string.Empty;
            bool isOpen = false;
            for (int i = 0; i < values.Length; i++)
            {
                bool.TryParse(values.GetValue(i) + "", out isOpen);
                if (isOpen == false)
                    return isOpen;
            }
            return true;
        }
        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
        #endregion
    }
    /// 
    /// 其中有一项为true就是true
    /// 
    public class FlowConverterForAllTrueMultiBinding : IMultiValueConverter
    {
        #region IMultiValueConverter 成员
        public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            string str_Result = string.Empty;
            bool isOpen = false;
            for (int i = 0; i < values.Length; i++)
            {
                bool.TryParse(values.GetValue(i) + "", out isOpen);
                if (isOpen == true)
                    return isOpen;
            }
            return false;
        }
        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
        #endregion
    }
    /// 
    /// 第一项false则关闭,后面的只要有true就打开
    /// 
    public class FlowConverterFirstFalseMultiBinding : IMultiValueConverter
    {
        #region IMultiValueConverter 成员
        public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (values.Length < 2) return false;
            bool isOpen = false;
            bool.TryParse(values.GetValue(0) + "", out isOpen);
            if (!isOpen) return false;
            
            isOpen = false;
            for (int i = 1; i < values.Length; i++)
            {
                bool.TryParse(values.GetValue(i) + "", out isOpen);
                if (isOpen == true)
                    return true;
            }
            return false;
        }
        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
        #endregion
    }
}