import 'dart:async';
import 'dart:ui';
import 'package:flutter/material.dart';
// import '../theme/app_colors.dart';
import '../services/sms_service.dart';
import '../utils/platform_helper.dart';
import '../routes/app_routes.dart';
import '../l10n/app_localizations.dart';
import '../utils/reduce_motion.dart';

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

  @override
  State<SplashScreen> createState() => _SplashScreenState();
}

class _SplashScreenState extends State<SplashScreen>
    with SingleTickerProviderStateMixin {
  late AnimationController _animationController;
  late Animation<double> _fadeAnimation;
  late Animation<double> _scaleAnimation;
  late Animation<double> _slideAnimation;
  late Animation<double> _rotateAnimation;

  // 파티클 애니메이션을 위한 변수
  final List<Map<String, dynamic>> _particles = [];

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

    // 애니메이션 컨트롤러 초기화
    _animationController = AnimationController(
      duration: const Duration(milliseconds: 2500),
      vsync: this,
    );

    _fadeAnimation = CurvedAnimation(
      parent: _animationController,
      curve: const Interval(0.0, 0.7, curve: Curves.easeInOut),
    );

    _scaleAnimation = Tween<double>(
      begin: 0.6,
      end: 1.0,
    ).animate(CurvedAnimation(
      parent: _animationController,
      curve: Curves.elasticOut,
    ));

    _slideAnimation = Tween<double>(
      begin: 50.0,
      end: 0.0,
    ).animate(CurvedAnimation(
      parent: _animationController,
      curve: const Interval(0.3, 0.8, curve: Curves.easeOutCubic),
    ));

    _rotateAnimation = Tween<double>(
      begin: 0.0,
      end: 0.1,
    ).animate(CurvedAnimation(
      parent: _animationController,
      curve: const Interval(0.0, 0.5, curve: Curves.easeOutBack),
    ));

    // 랜덤 파티클 생성
    // 접근성(모션 축소) 고려한 파티클 생성
    _generateParticles(reduced: ReduceMotion.platform());

    _animationController.forward();

    // 애니메이션 완료 후 메인화면으로 이동
    Timer(const Duration(seconds: 2), () {
      navigateToNextScreen();
    });
  }

  void _generateParticles({bool reduced = false}) {
    final random = DateTime.now().millisecondsSinceEpoch;
    final total = reduced ? 6 : 20;

    for (int i = 0; i < total; i++) {
      final size = (random % 10) / 10 * 8 + 2; // 2-10 사이의 크기
      final x = (random % 100) / 100 * 300; // 랜덤 X 위치
      final y = (random % 100) / 100 * 500; // 랜덤 Y 위치
      final opacity = (random % 10) / 10 * 0.4 + 0.1; // 0.1-0.5 사이의 투명도
      final duration = (random % 10) / 10 * (reduced ? 1800 : 3000) +
          (reduced ? 1200 : 2000); // 축소 시 더 짧게
      final delay = (random % 10) / 10 * 2000; // 0-2초 사이의 지연시간

      _particles.add({
        'size': size,
        'x': x,
        'y': y,
        'opacity': opacity,
        'duration': duration,
        'delay': delay,
        // color computed at render from ColorScheme.primary
      });
    }
  }

  Future<void> navigateToNextScreen() async {
    // Android에서 SMS 권한이 없으면 권한 안내 화면으로 이동
    if (PlatformHelper.isAndroid) {
      final hasPermission = await SMSService.hasSMSPermission();
      if (!hasPermission && mounted) {
        Navigator.of(context).pushNamedAndRemoveUntil(
          AppRoutes.smsPermission,
          (route) => false,
        );
        return;
      }
    }

    if (!mounted) return;
    Navigator.of(context).pushNamedAndRemoveUntil(
      AppRoutes.main,
      (route) => false,
    );
  }

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

  @override
  Widget build(BuildContext context) {
    final size = MediaQuery.of(context).size;

    return Scaffold(
      body: Stack(
        children: [
          // 단색 배경
          Container(color: Theme.of(context).colorScheme.surface),
          // 글래스모피즘 오버레이
          Container(
            decoration: BoxDecoration(
              color: Theme.of(context)
                  .colorScheme
                  .onSurface
                  .withValues(alpha: 0.05),
            ),
          ),
          Stack(
            children: [
              // 배경 파티클
              ..._particles.map((particle) {
                return AnimatedPositioned(
                  duration:
                      Duration(milliseconds: particle['duration'].toInt()),
                  curve: Curves.easeInOut,
                  left: particle['x'] - 50 + (size.width * 0.1),
                  top: particle['y'] - 50 + (size.height * 0.1),
                  child: TweenAnimationBuilder<double>(
                    tween: Tween<double>(begin: 0.0, end: particle['opacity']),
                    duration:
                        Duration(milliseconds: particle['duration'].toInt()),
                    builder: (context, value, child) {
                      return Opacity(
                        opacity: value,
                        child: child,
                      );
                    },
                    child: Container(
                      width: particle['size'],
                      height: particle['size'],
                      decoration: BoxDecoration(
                        color: Theme.of(context).colorScheme.primary,
                        shape: BoxShape.circle,
                        boxShadow: [
                          BoxShadow(
                            color: Theme.of(context)
                                .colorScheme
                                .primary
                                .withValues(alpha: 0.3),
                            blurRadius: 10,
                            spreadRadius: 1,
                          ),
                        ],
                      ),
                    ),
                  ),
                );
              }),

              // 상단 원형 장식 제거(단색 배경 유지)
              Positioned(
                top: -size.height * 0.2,
                right: -size.width * 0.2,
                child: SizedBox(
                  width: size.width * 0.8,
                  height: size.width * 0.8,
                ),
              ),

              // 하단 원형 장식 제거
              Positioned(
                bottom: -size.height * 0.1,
                left: -size.width * 0.3,
                child: SizedBox(
                  width: size.width * 0.9,
                  height: size.width * 0.9,
                ),
              ),

              // 메인 콘텐츠
              Column(
                children: [
                  Expanded(
                    child: Center(
                      child: Column(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: [
                          // 로고 애니메이션
                          AnimatedBuilder(
                            animation: _animationController,
                            builder: (context, child) {
                              return Transform.scale(
                                  scale: _scaleAnimation.value,
                                  child: Transform.rotate(
                                    angle: _rotateAnimation.value,
                                    child: AnimatedContainer(
                                        duration:
                                            const Duration(milliseconds: 200),
                                        width: 120,
                                        height: 120,
                                        child: ClipRRect(
                                          borderRadius:
                                              BorderRadius.circular(30),
                                          child: BackdropFilter(
                                            filter: ImageFilter.blur(
                                                sigmaX: ReduceMotion.scale(
                                                    context,
                                                    normal: 20,
                                                    reduced: 8),
                                                sigmaY: ReduceMotion.scale(
                                                    context,
                                                    normal: 20,
                                                    reduced: 8)),
                                            child: Container(
                                              decoration: BoxDecoration(
                                                color: Theme.of(context)
                                                    .colorScheme
                                                    .surface
                                                    .withValues(alpha: 0.6),
                                                borderRadius:
                                                    BorderRadius.circular(30),
                                                border: Border.all(
                                                  color: Theme.of(context)
                                                      .colorScheme
                                                      .outline
                                                      .withValues(alpha: 0.2),
                                                  width: 1.5,
                                                ),
                                              ),
                                              child: Center(
                                                child: AnimatedBuilder(
                                                    animation:
                                                        _animationController,
                                                    builder: (context, _) {
                                                      return Icon(
                                                        Icons
                                                            .subscriptions_outlined,
                                                        size: 64,
                                                        color: Theme.of(context)
                                                            .colorScheme
                                                            .primary,
                                                      );
                                                    }),
                                              ),
                                            ),
                                          ),
                                        )),
                                  ));
                            },
                          ),

                          const SizedBox(height: 40),

                          // 앱 이름 텍스트
                          AnimatedBuilder(
                            animation: _animationController,
                            builder: (context, child) {
                              return Opacity(
                                opacity: _fadeAnimation.value,
                                child: Transform.translate(
                                  offset: Offset(0, _slideAnimation.value),
                                  child: child,
                                ),
                              );
                            },
                            child: Text(
                              AppLocalizations.of(context).appTitle,
                              style: TextStyle(
                                fontSize: 36,
                                fontWeight: FontWeight.bold,
                                color: Theme.of(context)
                                    .colorScheme
                                    .primary
                                    .withValues(alpha: 0.9),
                                letterSpacing: 1.2,
                              ),
                            ),
                          ),

                          const SizedBox(height: 16),

                          // 부제목 텍스트
                          AnimatedBuilder(
                            animation: _animationController,
                            builder: (context, child) {
                              return Opacity(
                                opacity: _fadeAnimation.value,
                                child: Transform.translate(
                                  offset:
                                      Offset(0, _slideAnimation.value * 1.2),
                                  child: child,
                                ),
                              );
                            },
                            child: Text(
                              AppLocalizations.of(context).appSubtitle,
                              style: TextStyle(
                                fontSize: 16,
                                color: Theme.of(context)
                                    .colorScheme
                                    .primary
                                    .withValues(alpha: 0.7),
                                letterSpacing: 0.5,
                              ),
                            ),
                          ),

                          const SizedBox(height: 60),

                          // 로딩 인디케이터
                          FadeTransition(
                            opacity: _fadeAnimation,
                            child: ClipRRect(
                              borderRadius: BorderRadius.circular(50),
                              child: BackdropFilter(
                                filter:
                                    ImageFilter.blur(sigmaX: 10, sigmaY: 10),
                                child: Container(
                                  width: 60,
                                  height: 60,
                                  padding: const EdgeInsets.all(6),
                                  decoration: BoxDecoration(
                                    color: Theme.of(context)
                                        .colorScheme
                                        .onSurface
                                        .withValues(alpha: 0.1),
                                    borderRadius: BorderRadius.circular(50),
                                    border: Border.all(
                                      color: Theme.of(context)
                                          .colorScheme
                                          .onSurface
                                          .withValues(alpha: 0.2),
                                      width: 1,
                                    ),
                                  ),
                                  child: CircularProgressIndicator(
                                    color:
                                        Theme.of(context).colorScheme.primary,
                                    strokeWidth: 3,
                                  ),
                                ),
                              ),
                            ),
                          ),
                        ],
                      ),
                    ),
                  ),

                  // 카피라이트 텍스트
                  Padding(
                    padding: const EdgeInsets.only(bottom: 24.0),
                    child: FadeTransition(
                      opacity: _fadeAnimation,
                      child: Text(
                        '© 2025 NatureBridgeAI. All rights reserved.',
                        style: TextStyle(
                          fontSize: 12,
                          color: Theme.of(context)
                              .colorScheme
                              .onSurface
                              .withValues(alpha: 0.6),
                          letterSpacing: 0.5,
                        ),
                      ),
                    ),
                  ),
                ],
              ),
            ],
          ),
        ],
      ),
    );
  }
}
