import 'dart:async';
import 'dart:math' as math;
import 'dart:ui';

import 'package:flutter/cupertino.dart';
import 'package:hum/core/constants/app_text.dart';
import 'package:hum/core/constants/app_theme.dart';

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

  @override
  State<HomeAIView> createState() => _HomeAIViewState();
}

class _HomeAIViewState extends State<HomeAIView> with SingleTickerProviderStateMixin {
  int _ideaIndex = 0;
  Timer? _ideasTimer;
  late final AnimationController _borderController;
  final List<Color> colorsGlow = [
    Color(0xFF00FFFF),
    Color(0xFF2155E5),
    Color(0xFF2155E5),
    Color(0xFF0DFFEF),
  ];

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

    _borderController = AnimationController(vsync: this, duration: const Duration(seconds: 5))
      ..repeat();

    _ideasTimer = Timer.periodic(const Duration(seconds: 3), (_) {
      if (!mounted) return;
      setState(() {
        _ideaIndex = (_ideaIndex + 1) % homeViewIdeas.length;
      });
    });
  }

  @override
  void dispose() {
    _borderController.dispose();
    _ideasTimer?.cancel();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Expanded(
      child: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.spaceAround,
          children: [
            Spacer(flex: 2),
            SizedBox(
              width: 150,
              child: Stack(
                alignment: Alignment.center,
                children: [
                  AnimatedBuilder(
                    animation: _borderController,
                    builder: (context, _) {
                      final rot = _borderController.value * 2 * math.pi;
                      return ImageFiltered(
                        imageFilter: ImageFilter.blur(sigmaX: 34, sigmaY: 24),
                        child: Container(
                          width: 170,
                          height: 170,
                          decoration: BoxDecoration(
                            borderRadius: BorderRadius.circular(38),
                            gradient: SweepGradient(
                              colors: colorsGlow,
                              stops: const [0.0, 0.33, 0.66, 1.0],
                              transform: GradientRotation(rot),
                            ),
                            boxShadow: [
                              BoxShadow(
                                color: CupertinoDynamicColor.resolve(
                                  colorAccentPrimary,
                                  context,
                                ).withValues(alpha: 0.1),
                                blurRadius: 40,
                                spreadRadius: 12,
                              ),
                              BoxShadow(
                                color: CupertinoDynamicColor.resolve(
                                  colorAccentPrimary,
                                  context,
                                ).withValues(alpha: 0.15),
                                blurRadius: 10,
                                spreadRadius: 24,
                              ),
                            ],
                          ),
                        ),
                      );
                    },
                  ),
                  AnimatedBuilder(
                    animation: _borderController,
                    builder: (context, _) {
                      final rot = _borderController.value * 2 * math.pi;
                      return Container(
                        padding: const EdgeInsets.all(4),
                        decoration: BoxDecoration(
                          borderRadius: BorderRadius.circular(36),
                          gradient: SweepGradient(
                            colors: colorsGlow,
                            stops: const [0.0, 0.33, 0.66, 1.0],
                            transform: GradientRotation(rot),
                          ),
                          boxShadow: [
                            BoxShadow(
                              color: CupertinoDynamicColor.resolve(
                                colorAccentPrimary,
                                context,
                              ).withValues(alpha: 0.1),
                              blurRadius: 30,
                              spreadRadius: 6,
                            ),
                          ],
                        ),
                        child: ClipRRect(
                          borderRadius: BorderRadius.circular(32),
                          child: Image.asset('assets/images/logo_app_large.png', fit: BoxFit.cover),
                        ),
                      );
                    },
                  ),
                ],
              ),
            ),
            Spacer(),
            Column(
              children: [
                Text(
                  homeViewVoiceTitle,
                  style: const TextStyle(fontWeight: FontWeight.bold, fontSize: 20),
                ),
                Padding(
                  padding: const EdgeInsets.only(top: 2.0),
                  child: Opacity(
                    opacity: 0.8,
                    child: Text(
                      homeViewVoiceMessage,
                      style: const TextStyle(fontWeight: FontWeight.w300, fontSize: 17),
                    ),
                  ),
                ),
                Padding(
                  padding: const EdgeInsets.only(top: 24.0, bottom: 40.0),
                  child: Opacity(
                    opacity: 0.8,
                    child: AnimatedSwitcher(
                      duration: const Duration(milliseconds: 350),
                      transitionBuilder: (child, anim) =>
                          FadeTransition(opacity: anim, child: child),
                      child: Text(
                        homeViewIdeas[_ideaIndex],
                        key: ValueKey(_ideaIndex),
                        textAlign: TextAlign.center,
                        style: const TextStyle(
                          fontWeight: FontWeight.w500,
                          fontSize: 17,
                          color: CupertinoColors.systemPurple,
                        ),
                      ),
                    ),
                  ),
                ),
              ],
            ),
            Spacer(),
          ],
        ),
      ),
    );
  }
}
