using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace DansoriEQ.App.Controls;

/// <summary>Concentric rings that expand and fade — a "sound is emanating" effect. Pure WPF (경로 A).</summary>
public partial class SoundwaveRings : UserControl
{
    private readonly List<Ellipse> _rings = new();
    private bool _running;

    public int RingCount { get; set; } = 3;
    public Brush RingBrush { get; set; } = new SolidColorBrush(Color.FromRgb(0x4C, 0xC2, 0xFF));
    public double FromScale { get; set; } = 0.7;
    public double ToScale { get; set; } = 1.7;
    public int PeriodMs { get; set; } = 1800;

    public SoundwaveRings()
    {
        InitializeComponent();
        Loaded += (_, __) => Build();
    }

    private void Build()
    {
        if (_rings.Count > 0) return;
        for (int i = 0; i < RingCount; i++)
        {
            var e = new Ellipse
            {
                Stroke = RingBrush,
                StrokeThickness = 2,
                Fill = Brushes.Transparent,
                Opacity = 0,
                RenderTransformOrigin = new Point(0.5, 0.5),
                HorizontalAlignment = HorizontalAlignment.Stretch,
                VerticalAlignment = VerticalAlignment.Stretch,
                RenderTransform = new ScaleTransform(FromScale, FromScale)
            };
            _rings.Add(e);
            Host.Children.Add(e);
        }
    }

    /// <summary>Begin emanating rings.</summary>
    public void Start()
    {
        Build();
        if (_running) return;
        _running = true;
        for (int i = 0; i < _rings.Count; i++)
            Animate(_rings[i], TimeSpan.FromMilliseconds(i * (PeriodMs / (double)RingCount)));
    }

    private void Animate(Ellipse ring, TimeSpan begin)
    {
        var dur = TimeSpan.FromMilliseconds(PeriodMs);
        var scale = new DoubleAnimation(FromScale, ToScale, dur)
        {
            RepeatBehavior = RepeatBehavior.Forever, BeginTime = begin,
            EasingFunction = new SineEase { EasingMode = EasingMode.EaseOut }
        };
        var fade = new DoubleAnimation(0.5, 0, dur)
        {
            RepeatBehavior = RepeatBehavior.Forever, BeginTime = begin
        };
        var st = (ScaleTransform)ring.RenderTransform;
        st.BeginAnimation(ScaleTransform.ScaleXProperty, scale);
        st.BeginAnimation(ScaleTransform.ScaleYProperty, scale);
        ring.BeginAnimation(OpacityProperty, fade);
    }

    /// <summary>Stop and hide the rings.</summary>
    public void Stop()
    {
        _running = false;
        foreach (var e in _rings)
        {
            var st = (ScaleTransform)e.RenderTransform;
            st.BeginAnimation(ScaleTransform.ScaleXProperty, null);
            st.BeginAnimation(ScaleTransform.ScaleYProperty, null);
            e.BeginAnimation(OpacityProperty, null);
            e.Opacity = 0;
        }
    }
}
