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

using BeWo.Converter;

using BS.Shared;

namespace BeWo.Security
{
    public class DemandUserRight
    {
        public static readonly DependencyProperty EnabledDemandsProperty = DependencyProperty.RegisterAttached("EnabledDemands", typeof(UserRightType[]), typeof(FrameworkElement), new FrameworkPropertyMetadata());

        public static readonly DependencyProperty VisibleDemandsProperty = DependencyProperty.RegisterAttached("VisibleDemands", typeof(UserRightType[]), typeof(FrameworkElement), new FrameworkPropertyMetadata());

        [TypeConverter(typeof(UserRightTypeArrayConverter))]
        public static UserRightType[] GetEnabledDemands(FrameworkElement element)
        {
            return (UserRightType[])element.GetValue(EnabledDemandsProperty);
        }

        [TypeConverter(typeof(UserRightTypeArrayConverter))]
        public static UserRightType[] GetVisibleDemands(FrameworkElement element)
        {
            return (UserRightType[])element.GetValue(VisibleDemandsProperty);
        }

        [TypeConverter(typeof(UserRightTypeArrayConverter))]
        public static void SetEnabledDemands(FrameworkElement element, UserRightType[] value)
        {
            element.SetBinding(UIElement.IsEnabledProperty, new Binding("LoggedOnUsersRights") { Source = BeWoApp.CurrentBeWo, Converter = new Right2IsEnabledConverter(), ConverterParameter = value });
        }

        [TypeConverter(typeof(UserRightTypeArrayConverter))]
        public static void SetVisibleDemands(FrameworkElement element, UserRightType[] value)
        {
            element.SetBinding(UIElement.VisibilityProperty, new Binding("LoggedOnUsersRights") { Source = BeWoApp.CurrentBeWo, Converter = new Right2VisibilityConverter(), ConverterParameter = value });
        }
    }

    public class Right2VisibilityConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return BeWoApp.HasLoggedOnUserRight((UserRightType[])parameter) ? Visibility.Visible : Visibility.Collapsed;
        }

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

    public class Right2IsEnabledConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return BeWoApp.HasLoggedOnUserRight((UserRightType[])parameter);
        }

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