using SWRIS.Enums;
using System;
using System.Globalization;
using System.Windows.Data;

namespace SWRIS.Converters
{
    public class RunningStatusToBooleanConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null || parameter == null) { return false; }

            if (value is RunningStatus valueStatus && Enum.TryParse(parameter.ToString(), true, out RunningStatus parameterStatus))
            {
                return GetRunningStatus(valueStatus) == parameterStatus;
            }
            return false;
        }
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (!(value is bool boolValue) || parameter == null)
                return RunningStatus.Stopped;

            if (!Enum.TryParse(parameter.ToString(), true, out RunningStatus targetStatus))
                return RunningStatus.Stopped;

            if (boolValue)
            {
                return targetStatus;
            }

            return RunningStatus.Stopped;
        }
        private RunningStatus GetRunningStatus(RunningStatus runningStatus)
        {
            // 优先级：TemporalNormal > SpatialNormal > Stopped
            if (runningStatus.HasFlag(RunningStatus.TemporalNormal))
                return RunningStatus.TemporalNormal;

            if (runningStatus.HasFlag(RunningStatus.SpatialNormal))
                return RunningStatus.SpatialNormal;

            return RunningStatus.Stopped;
        }
    }
}
