namespace UICommon;
[TemplateVisualState(Name = "Large", GroupName = "SizeStates")]
[TemplateVisualState(Name = "Small", GroupName = "SizeStates")]
[TemplateVisualState(Name = "Inactive", GroupName = "ActiveStates")]
[TemplateVisualState(Name = "Active", GroupName = "ActiveStates")]
public class CycleRingLoading : Control
{
    static CycleRingLoading()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(CycleRingLoading), new FrameworkPropertyMetadata(typeof(CycleRingLoading)));
        VisibilityProperty.OverrideMetadata(typeof(CycleRingLoading), new FrameworkPropertyMetadata(delegate (DependencyObject ringObject, DependencyPropertyChangedEventArgs e)
        {
            bool flag = e.NewValue != e.OldValue;
            if (flag)
            {
                CycleRingLoading CycleRingLoading = (CycleRingLoading)ringObject;
                bool flag2 = (Visibility)e.NewValue > Visibility.Visible;
                if (flag2)
                {
                    CycleRingLoading.SetCurrentValue(IsActiveProperty, false);
                }
                else
                {
                    CycleRingLoading.IsActive = true;
                }
            }
        }));
    }

    public CycleRingLoading()
    {
        SizeChanged += (s, e) =>
        {
            BindableWidth = ActualWidth;
        };

        Unloaded += CycleRingLoading_Unloaded;
    }


    public static readonly DependencyProperty BindableWidthProperty = DependencyProperty.Register("BindableWidth", typeof(double), typeof(CycleRingLoading), new PropertyMetadata(0.0, new PropertyChangedCallback(BindableWidthCallback)));

    public static readonly DependencyProperty IsActiveProperty = DependencyProperty.Register("IsActive", typeof(bool), typeof(CycleRingLoading), new FrameworkPropertyMetadata(true, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, new PropertyChangedCallback(IsActiveChanged)));

    public static readonly DependencyProperty IsLargeProperty = DependencyProperty.Register("IsLarge", typeof(bool), typeof(CycleRingLoading), new PropertyMetadata(true, new PropertyChangedCallback(IsLargeChangedCallback)));

    public static readonly DependencyProperty MaxSideLengthProperty = DependencyProperty.Register("MaxSideLength", typeof(double), typeof(CycleRingLoading), new PropertyMetadata(0.0));

    public static readonly DependencyProperty EllipseDiameterProperty = DependencyProperty.Register("EllipseDiameter", typeof(double), typeof(CycleRingLoading), new PropertyMetadata(0.0));

    public static readonly DependencyProperty EllipseOffsetProperty = DependencyProperty.Register("EllipseOffset", typeof(Thickness), typeof(CycleRingLoading), new PropertyMetadata(default(Thickness)));



    private string StateActive = "Active";

    private string StateInActive = "InActive";

    private string StateLarge = "Large";

    private string StateSmall = "Small";


    public override void OnApplyTemplate()
    {
        base.OnApplyTemplate();
        UpdateLargeState();
        UpdateActiveState();
    }

    public double BindableWidth
    {
        get => (double)GetValue(BindableWidthProperty);
        private set => SetValue(BindableWidthProperty, value);
    }

    private static void BindableWidthCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
    {
        if (dependencyObject is not CycleRingLoading CycleRingLoading)
            return;

        bool flag = CycleRingLoading == null;
        if (!flag)
        {
            CycleRingLoading!.SetEllipseDiameter((double)e.NewValue);
            CycleRingLoading.SetEllipseOffset((double)e.NewValue);
            CycleRingLoading.SetMaxSideLength((double)e.NewValue);
        }
    }

    private void SetEllipseDiameter(double width) => EllipseDiameter = width / 8.0;

    private void SetEllipseOffset(double width) => EllipseOffset = new Thickness(0.0, width / 2.0, 0.0, 0.0);

    private void SetMaxSideLength(double width) => MaxSideLength = width <= 20.0 ? 20.0 : width;

    public bool IsActive
    {
        get => (bool)GetValue(IsActiveProperty);
        set => SetValue(IsActiveProperty, value);
    }

    private static void IsActiveChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
    {
        if (sender is CycleRingLoading CycleRingLoading)
        {
            bool flag = CycleRingLoading == null;
            if (!flag)
                CycleRingLoading!.UpdateActiveState();
        }
    }

    private void UpdateActiveState()
    {
        bool isActive = IsActive;
        if (isActive)
            VisualStateManager.GoToState(this, StateActive, true);
        else
            VisualStateManager.GoToState(this, StateInActive, true);
    }

    public bool IsLarge
    {
        get => (bool)GetValue(IsLargeProperty);
        set => SetValue(IsLargeProperty, value);
    }

    private static void IsLargeChangedCallback(DependencyObject sender, DependencyPropertyChangedEventArgs e)
    {
        if (sender is not CycleRingLoading CycleRingLoading || CycleRingLoading is null)
            return;

        bool flag = CycleRingLoading == null;
        if (!flag)
            CycleRingLoading!.UpdateLargeState();
    }

    private void UpdateLargeState()
    {
        bool isLarge = IsLarge;
        if (isLarge)
            VisualStateManager.GoToState(this, StateLarge, true);
        else
            VisualStateManager.GoToState(this, StateSmall, true);
    }

    private void CycleRingLoading_Unloaded(object sender, RoutedEventArgs e)
    {
        IsActive = false;
    }

    public double MaxSideLength
    {
        get => (double)GetValue(MaxSideLengthProperty);
        set => SetValue(MaxSideLengthProperty, value);
    }

    public double EllipseDiameter
    {
        get => (double)GetValue(EllipseDiameterProperty);
        set => SetValue(EllipseDiameterProperty, value);
    }

    public Thickness EllipseOffset
    {
        get => (Thickness)GetValue(EllipseOffsetProperty);
        set => SetValue(EllipseOffsetProperty, value);
    }




}