// lib/widgets/cast/animated_cast_icon.dart

import 'package:flutter/material.dart';
import 'package:aves/widgets/cast/cast_controller.dart';

class AnimatedCastIcon extends StatefulWidget {
  const AnimatedCastIcon({super.key});

  @override
  State<AnimatedCastIcon> createState() => _AnimatedCastIconState();
}

class _AnimatedCastIconState extends State<AnimatedCastIcon>
    with SingleTickerProviderStateMixin {
  late AnimationController _controller;

  @override
  void initState() {
    super.initState();

    _controller = AnimationController(
      vsync: this,
      duration: const Duration(milliseconds: 700),
    )..repeat(reverse: true);
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return ValueListenableBuilder<CastIconState>(
      valueListenable: CastController.instance.castIconState,
      builder: (context, state, _) {
        switch (state) {
          case CastIconState.idle:
            return const Icon(
              Icons.cast,
              color: Colors.black,
              size: 26,
            );

          case CastIconState.connected:
            return const Icon(
              Icons.cast,
              color: Colors.blue,
              size: 26,
            );

          case CastIconState.connecting:
            return _BlinkingCastIcon(controller: _controller);
        }
      },
    );
  }
}

// ------------------------------------------------------------
// ⭐ WIDGET CHE LAMPEGGIA NERO ↔ BLU ↔ NERO
// ------------------------------------------------------------
class _BlinkingCastIcon extends StatelessWidget {
  final AnimationController controller;

  const _BlinkingCastIcon({required this.controller});

  @override
/*  Widget build(BuildContext context) {
    return AnimatedBuilder(
      animation: controller,
      builder: (_, __) {
        final color = Color.lerp(
          Colors.grey,
          Colors.blue,
          controller.value,
        )!;

        return Icon(
          Icons.cast,
          color: color,
          size: 26,
        );
      },
    );
  }*/
Widget build(BuildContext context) {
  return AnimatedBuilder(
    animation: controller,
    builder: (_, __) {
      final opacity = controller.value; // 0 → 1 → 0

      return Icon(
        Icons.cast,
        color: Colors.blue.withOpacity(opacity),
        size: 26,
      );
    },
  );
}

}
