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

namespace NavisworksTransport.UI.WPF.Converters
{
    /// <summary>
    /// 计数值到可见性的转换器
    /// 当计数大于0时显示，等于0时隐藏
    /// </summary>
    public class CountToVisibilityConverter : IValueConverter
    {
        /// <summary>
        /// 将计数值转换为可见性
        /// </summary>
        /// <param name="value">计数值</param>
        /// <param name="targetType">目标类型</param>
        /// <param name="parameter">参数，"Inverse"表示反向转换</param>
        /// <param name="culture">文化信息</param>
        /// <returns>可见性枚举值</returns>
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            int count = 0;
            
            // 尝试转换为整数
            if (value is int intValue)
            {
                count = intValue;
            }
            else if (value is long longValue)
            {
                count = (int)longValue;
            }
            else if (value != null && int.TryParse(value.ToString(), out int parsedValue))
            {
                count = parsedValue;
            }

            // 检查是否需要反向转换
            bool isInverse = parameter?.ToString()?.Equals("Inverse", StringComparison.OrdinalIgnoreCase) == true;
            
            bool isVisible = count > 0;
            
            if (isInverse)
            {
                isVisible = !isVisible;
            }

            return isVisible ? Visibility.Visible : Visibility.Collapsed;
        }

        /// <summary>
        /// 反向转换（不支持）
        /// </summary>
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return Binding.DoNothing;
        }
    }

    /// <summary>
    /// 反向布尔到可见性转换器
    /// </summary>
    public class InverseBooleanToVisibilityConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is bool boolValue)
            {
                return boolValue ? Visibility.Collapsed : Visibility.Visible;
            }
            return Visibility.Visible;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is Visibility visibility)
            {
                return visibility != Visibility.Visible;
            }
            return true;
        }
    }

    /// <summary>
    /// 布尔值到可见性转换器（标准命名）
    /// </summary>
    /// <summary>
    /// 严格布尔值到可见性转换器（只接受布尔值）
    /// </summary>
    public class BooleanToVisibilityConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            // 只接受严格的布尔值
            if (value is bool boolValue)
            {
                // 检查是否需要反向转换
                bool isInverse = parameter?.ToString()?.Equals("Inverse", StringComparison.OrdinalIgnoreCase) == true;
                
                if (isInverse)
                {
                    return boolValue ? Visibility.Collapsed : Visibility.Visible;
                }
                else
                {
                    return boolValue ? Visibility.Visible : Visibility.Collapsed;
                }
            }
            
            // 非布尔值直接返回Collapsed
            return 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>
    /// 等值转换器 - 用于MultiBinding，比较两个值是否相等
    /// </summary>
    public class EqualityToBooleanConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            if (values == null || values.Length < 2)
                return false;
            
            // 比较第一个值和第二个值是否相等
            return Equals(values[0], values[1]);
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException("EqualityToBooleanConverter不支持反向转换");
        }
    }
}