using System;
using System.Globalization;
using System.Windows;
using System.Windows.Data;

namespace NavisworksTransport.UI.WPF.Converters
{
    /// <summary>
    /// 布尔值到可见性的转换器
    /// </summary>
    /// <summary>
    /// 通用值到可见性转换器
    /// - 布尔值：true/false → Visible/Collapsed
    /// - 字符串：非空非白字符 → Visible，空/null → Collapsed  
    /// - 数字：非零 → Visible，零 → Collapsed
    /// - 对象：非null → Visible，null → Collapsed
    /// </summary>
    public class BoolToVisibilityConverter : IValueConverter
    {
        /// <summary>
        /// 静态实例：将非空对象转换为true
        /// </summary>
        public static readonly IValueConverter IsNotNullToBoolConverter = new IsNotNullToBoolConverter();
        
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            bool isVisible = false;
            
            // 处理布尔值
            if (value is bool boolValue)
            {
                isVisible = boolValue;
            }
            // 处理字符串 - 非空非白字符字符串为true
            else if (value is string stringValue)
            {
                isVisible = !string.IsNullOrWhiteSpace(stringValue);
            }
            // 处理数字类型
            else if (value is int intValue)
            {
                isVisible = intValue != 0;
            }
            else if (value is long longValue)
            {
                isVisible = longValue != 0;
            }
            else if (value is double doubleValue)
            {
                isVisible = Math.Abs(doubleValue) > double.Epsilon;
            }
            else if (value is float floatValue)
            {
                isVisible = Math.Abs(floatValue) > float.Epsilon;
            }
            // 处理其他对象 - 非null为true
            else
            {
                isVisible = value != null;
            }
            
            // 检查是否需要反向转换
            bool isInverse = parameter?.ToString()?.Equals("Inverse", StringComparison.OrdinalIgnoreCase) == true;
            
            if (isInverse)
            {
                return isVisible ? Visibility.Collapsed : Visibility.Visible;
            }
            else
            {
                return isVisible ? Visibility.Visible : Visibility.Collapsed;
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is Visibility visibility)
            {
                return visibility == Visibility.Visible;
            }
            return false;
        }
    }
    
    /// <summary>
    /// 对象非空到布尔值的转换器
    /// </summary>
    public class IsNotNullToBoolConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return value != null;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return Binding.DoNothing;
        }
    }
}