import 'dart:math' show cos, sin;
import 'package:auto_route/auto_route.dart';
import 'package:flutter/material.dart';

import '../../../../../../common/theme/theme.dart';

@RoutePage()
class DrawTodayPage extends StatefulWidget {
  const DrawTodayPage({super.key});

  @override
  State<DrawTodayPage> createState() => _DrawTodayPageState();
}

class _DrawTodayPageState extends State<DrawTodayPage>
    with TickerProviderStateMixin {
  late AnimationController _fadeController;
  late AnimationController _slideController;
  late AnimationController _scaleController;
  late AnimationController _pulseController;
  late AnimationController _celebrationController;

  late Animation<double> _fadeAnimation;
  late Animation<Offset> _slideAnimation;
  late Animation<double> _scaleAnimation;
  late Animation<double> _pulseAnimation;
  late Animation<double> _celebrationAnimation;

  // Lottery Logic
  String globalNumber = '849302';
  String userNumber = '849302'; // Change this to test winner/loser
  bool isWinner = false;
  bool hasCheckedResult = false;

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

    // Check if user is winner
    isWinner = userNumber == globalNumber;

    _fadeController = AnimationController(
      duration: Duration(milliseconds: 1200),
      vsync: this,
    );

    _slideController = AnimationController(
      duration: Duration(milliseconds: 800),
      vsync: this,
    );

    _scaleController = AnimationController(
      duration: Duration(milliseconds: 1000),
      vsync: this,
    );

    _pulseController = AnimationController(
      duration: Duration(milliseconds: 2000),
      vsync: this,
    );

    _celebrationController = AnimationController(
      duration: Duration(milliseconds: 3000),
      vsync: this,
    );

    _fadeAnimation = Tween<double>(begin: 0.0, end: 1.0).animate(
      CurvedAnimation(parent: _fadeController, curve: Curves.easeInOut),
    );

    _slideAnimation = Tween<Offset>(begin: Offset(0, 0.3), end: Offset.zero)
        .animate(
          CurvedAnimation(parent: _slideController, curve: Curves.easeOutCubic),
        );

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

    _pulseAnimation = Tween<double>(begin: 1.0, end: 1.02).animate(
      CurvedAnimation(parent: _pulseController, curve: Curves.easeInOut),
    );

    _celebrationAnimation = Tween<double>(begin: 0.0, end: 1.0).animate(
      CurvedAnimation(parent: _celebrationController, curve: Curves.elasticOut),
    );

    _startAnimations();
  }

  void _startAnimations() async {
    await Future.delayed(Duration(milliseconds: 100));
    _fadeController.forward();

    await Future.delayed(Duration(milliseconds: 200));
    _slideController.forward();

    await Future.delayed(Duration(milliseconds: 300));
    _scaleController.forward();

    if (isWinner) {
      await Future.delayed(Duration(milliseconds: 600));
      _celebrationController.forward();
    }

    await Future.delayed(Duration(milliseconds: 800));
    _pulseController.repeat(reverse: true);
  }

  @override
  void dispose() {
    _fadeController.dispose();
    _slideController.dispose();
    _scaleController.dispose();
    _pulseController.dispose();
    _celebrationController.dispose();
    super.dispose();
  }

  void _checkMyNumber() {
    setState(() {
      hasCheckedResult = true;
    });

    if (isWinner) {
      _showWinnerDialog();
    } else {
      _showLoserDialog();
    }
  }

  void _showWinnerDialog() {
    showDialog(
      context: context,
      barrierDismissible: false,
      builder: (BuildContext context) {
        return Dialog(
          backgroundColor: Colors.transparent,
          child: Container(
            padding: EdgeInsets.all(24),
            decoration: BoxDecoration(
              gradient: LinearGradient(
                colors: [AppColor.warning, AppColor.warning.withOpacity(0.8)],
                begin: Alignment.topLeft,
                end: Alignment.bottomRight,
              ),
              borderRadius: BorderRadius.circular(24),
              boxShadow: [
                BoxShadow(
                  color: AppColor.warning.withOpacity(0.5),
                  blurRadius: 30,
                  spreadRadius: 5,
                ),
              ],
            ),
            child: Column(
              mainAxisSize: MainAxisSize.min,
              children: [
                // Animated Trophy
                TweenAnimationBuilder<double>(
                  tween: Tween(begin: 0.0, end: 1.0),
                  duration: Duration(milliseconds: 1000),
                  curve: Curves.elasticOut,
                  builder: (context, value, child) {
                    return Transform.scale(
                      scale: value,
                      child: Transform.rotate(
                        angle: (1 - value) * 0.5,
                        child: Icon(
                          Icons.emoji_events_rounded,
                          color: AppColor.white,
                          size: 80,
                        ),
                      ),
                    );
                  },
                ),

                SizedBox(height: 20),

                Text(
                  'SELAMAT!',
                  style: AppStyle.h2.copyWith(
                    color: AppColor.white,
                    fontWeight: FontWeight.w900,
                    letterSpacing: 2,
                  ),
                ),

                SizedBox(height: 8),

                Text(
                  'Kamu Pemenang Hari Ini!',
                  style: AppStyle.lg.copyWith(
                    color: AppColor.white,
                    fontWeight: FontWeight.w600,
                  ),
                ),

                SizedBox(height: 16),

                Container(
                  padding: EdgeInsets.all(16),
                  decoration: BoxDecoration(
                    color: AppColor.white.withOpacity(0.2),
                    borderRadius: BorderRadius.circular(16),
                  ),
                  child: Column(
                    children: [
                      Text(
                        'Nomor Pemenang',
                        style: AppStyle.sm.copyWith(
                          color: AppColor.white.withOpacity(0.8),
                          fontWeight: FontWeight.w600,
                        ),
                      ),
                      SizedBox(height: 8),
                      Text(
                        globalNumber,
                        style: TextStyle(
                          color: AppColor.white,
                          fontSize: 32,
                          fontWeight: FontWeight.w900,
                          letterSpacing: 8,
                        ),
                      ),
                    ],
                  ),
                ),

                SizedBox(height: 20),

                Container(
                  padding: EdgeInsets.all(12),
                  decoration: BoxDecoration(
                    color: AppColor.primary,
                    borderRadius: BorderRadius.circular(12),
                  ),
                  child: Text(
                    '🏆 Hadiah: 3KG Emas Batangan 🏆',
                    style: AppStyle.md.copyWith(
                      color: AppColor.white,
                      fontWeight: FontWeight.w700,
                    ),
                  ),
                ),

                SizedBox(height: 24),

                Row(
                  children: [
                    Expanded(
                      child: ElevatedButton(
                        onPressed: () {
                          Navigator.of(context).pop();
                        },
                        style: ElevatedButton.styleFrom(
                          backgroundColor: AppColor.white,
                          foregroundColor: AppColor.primary,
                          padding: EdgeInsets.symmetric(vertical: 12),
                          shape: RoundedRectangleBorder(
                            borderRadius: BorderRadius.circular(12),
                          ),
                        ),
                        child: Text(
                          'Tutup',
                          style: AppStyle.md.copyWith(
                            fontWeight: FontWeight.w600,
                          ),
                        ),
                      ),
                    ),
                    SizedBox(width: 12),
                    Expanded(
                      child: ElevatedButton(
                        onPressed: () {
                          Navigator.of(context).pop();
                          ScaffoldMessenger.of(context).showSnackBar(
                            SnackBar(
                              content: Text(
                                'Tim kami akan menghubungimu segera!',
                              ),
                              backgroundColor: AppColor.success,
                              shape: RoundedRectangleBorder(
                                borderRadius: BorderRadius.circular(12),
                              ),
                              behavior: SnackBarBehavior.floating,
                            ),
                          );
                        },
                        style: ElevatedButton.styleFrom(
                          backgroundColor: AppColor.primary,
                          foregroundColor: AppColor.white,
                          padding: EdgeInsets.symmetric(vertical: 12),
                          shape: RoundedRectangleBorder(
                            borderRadius: BorderRadius.circular(12),
                          ),
                        ),
                        child: Text(
                          'Klaim Hadiah',
                          style: AppStyle.md.copyWith(
                            fontWeight: FontWeight.w600,
                          ),
                        ),
                      ),
                    ),
                  ],
                ),
              ],
            ),
          ),
        );
      },
    );
  }

  void _showLoserDialog() {
    showDialog(
      context: context,
      builder: (BuildContext context) {
        return Dialog(
          backgroundColor: Colors.transparent,
          child: Container(
            padding: EdgeInsets.all(24),
            decoration: BoxDecoration(
              color: AppColor.white,
              borderRadius: BorderRadius.circular(24),
              boxShadow: [
                BoxShadow(
                  color: AppColor.black.withOpacity(0.1),
                  blurRadius: 20,
                  spreadRadius: 0,
                ),
              ],
            ),
            child: Column(
              mainAxisSize: MainAxisSize.min,
              children: [
                Container(
                  padding: EdgeInsets.all(16),
                  decoration: BoxDecoration(
                    color: AppColor.primary.withOpacity(0.1),
                    shape: BoxShape.circle,
                  ),
                  child: Icon(
                    Icons.close_rounded,
                    color: AppColor.primary,
                    size: 40,
                  ),
                ),

                SizedBox(height: 16),

                Text(
                  'Belum Beruntung',
                  style: AppStyle.lg.copyWith(
                    color: AppColor.primary,
                    fontWeight: FontWeight.w700,
                  ),
                ),

                SizedBox(height: 8),

                Text(
                  'Jangan menyerah! Coba lagi besok ya',
                  textAlign: TextAlign.center,
                  style: AppStyle.md.copyWith(
                    color: AppColor.textSecondary,
                    fontWeight: FontWeight.w500,
                  ),
                ),

                SizedBox(height: 16),

                Container(
                  padding: EdgeInsets.all(12),
                  decoration: BoxDecoration(
                    color: AppColor.primary.withOpacity(0.05),
                    borderRadius: BorderRadius.circular(12),
                  ),
                  child: Column(
                    children: [
                      Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          Text(
                            'Nomor Global:',
                            style: AppStyle.sm.copyWith(
                              color: AppColor.textSecondary,
                              fontWeight: FontWeight.w500,
                            ),
                          ),
                          Text(
                            globalNumber,
                            style: AppStyle.sm.copyWith(
                              color: AppColor.primary,
                              fontWeight: FontWeight.w700,
                              letterSpacing: 2,
                            ),
                          ),
                        ],
                      ),
                      SizedBox(height: 4),
                      Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          Text(
                            'Nomor Kamu:',
                            style: AppStyle.sm.copyWith(
                              color: AppColor.textSecondary,
                              fontWeight: FontWeight.w500,
                            ),
                          ),
                          Text(
                            userNumber,
                            style: AppStyle.sm.copyWith(
                              color: AppColor.textPrimary,
                              fontWeight: FontWeight.w700,
                              letterSpacing: 2,
                            ),
                          ),
                        ],
                      ),
                    ],
                  ),
                ),

                SizedBox(height: 20),

                SizedBox(
                  width: double.infinity,
                  child: ElevatedButton(
                    onPressed: () {
                      Navigator.of(context).pop();
                    },
                    style: ElevatedButton.styleFrom(
                      backgroundColor: AppColor.primary,
                      foregroundColor: AppColor.white,
                      padding: EdgeInsets.symmetric(vertical: 12),
                      shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.circular(12),
                      ),
                    ),
                    child: Text(
                      'Oke, Sampai Jumpa Besok!',
                      style: AppStyle.md.copyWith(fontWeight: FontWeight.w600),
                    ),
                  ),
                ),
              ],
            ),
          ),
        );
      },
    );
  }

  Widget _buildTitle() {
    return SlideTransition(
      position: _slideAnimation,
      child: Container(
        padding: EdgeInsets.symmetric(horizontal: 20, vertical: 10),
        child: Row(
          children: [
            Container(
              width: 3,
              height: 28,
              decoration: BoxDecoration(
                color: AppColor.white,
                borderRadius: BorderRadius.circular(2),
              ),
            ),
            SizedBox(width: 12),
            Text(
              'Undian Hari Ini',
              style: AppStyle.h3.copyWith(
                color: AppColor.textWhite,
                fontWeight: FontWeight.w600,
                letterSpacing: -0.5,
              ),
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildLotteryCard() {
    return ScaleTransition(
      scale: _scaleAnimation,
      child: Container(
        margin: EdgeInsets.symmetric(horizontal: 20),
        decoration: BoxDecoration(
          color: AppColor.white,
          borderRadius: BorderRadius.circular(24),
          boxShadow: [
            BoxShadow(
              color: isWinner
                  ? AppColor.warning.withOpacity(0.3)
                  : AppColor.black.withOpacity(0.08),
              blurRadius: 30,
              spreadRadius: 0,
              offset: Offset(0, 15),
            ),
          ],
        ),
        child: Stack(
          children: [
            // Winner celebration overlay
            if (isWinner)
              Positioned.fill(
                child: AnimatedBuilder(
                  animation: _celebrationAnimation,
                  builder: (context, child) {
                    return CustomPaint(
                      painter: WinnerCelebrationPainter(
                        _celebrationAnimation.value,
                      ),
                    );
                  },
                ),
              ),

            Padding(
              padding: EdgeInsets.all(24),
              child: Column(
                children: [
                  // Label
                  Container(
                    padding: EdgeInsets.symmetric(horizontal: 16, vertical: 8),
                    decoration: BoxDecoration(
                      color: isWinner
                          ? AppColor.warning.withOpacity(0.15)
                          : AppColor.primary.withOpacity(0.08),
                      borderRadius: BorderRadius.circular(20),
                    ),
                    child: Text(
                      isWinner ? '🎉 NOMOR PEMENANG 🎉' : 'NOMOR UNDIAN GLOBAL',
                      style: AppStyle.xs.copyWith(
                        color: isWinner ? AppColor.warning : AppColor.primary,
                        fontWeight: FontWeight.w700,
                        letterSpacing: 1,
                      ),
                    ),
                  ),

                  SizedBox(height: 20),

                  // Animated Number
                  AnimatedBuilder(
                    animation: _pulseAnimation,
                    builder: (context, child) {
                      return Transform.scale(
                        scale: _pulseAnimation.value,
                        child: Container(
                          width: double.infinity,
                          padding: EdgeInsets.symmetric(
                            horizontal: 20,
                            vertical: 16,
                          ),
                          decoration: BoxDecoration(
                            gradient: LinearGradient(
                              colors: isWinner
                                  ? [
                                      AppColor.warning.withOpacity(0.1),
                                      AppColor.warning.withOpacity(0.05),
                                    ]
                                  : [
                                      AppColor.primary.withOpacity(0.05),
                                      AppColor.primary.withOpacity(0.02),
                                    ],
                              begin: Alignment.topLeft,
                              end: Alignment.bottomRight,
                            ),
                            borderRadius: BorderRadius.circular(16),
                            border: Border.all(
                              color: isWinner
                                  ? AppColor.warning.withOpacity(0.3)
                                  : AppColor.primary.withOpacity(0.1),
                              width: 1,
                            ),
                          ),
                          child: Text(
                            globalNumber,
                            style: TextStyle(
                              color: isWinner
                                  ? AppColor.warning
                                  : AppColor.primary,
                              fontSize: 42,
                              fontWeight: FontWeight.w900,
                              letterSpacing: 12,
                              height: 1.0,
                            ),
                          ),
                        ),
                      );
                    },
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildPrizeCard() {
    return SlideTransition(
      position: Tween<Offset>(begin: Offset(0, 0.5), end: Offset.zero).animate(
        CurvedAnimation(
          parent: _slideController,
          curve: Interval(0.3, 1.0, curve: Curves.easeOutCubic),
        ),
      ),
      child: Container(
        margin: EdgeInsets.symmetric(horizontal: 20),
        padding: EdgeInsets.all(20),
        decoration: BoxDecoration(
          color: AppColor.white.withOpacity(0.12),
          borderRadius: BorderRadius.circular(20),
          border: Border.all(color: AppColor.white.withOpacity(0.2), width: 1),
          boxShadow: [
            BoxShadow(
              color: AppColor.black.withOpacity(0.05),
              blurRadius: 15,
              offset: Offset(0, 8),
            ),
          ],
        ),
        child: Column(
          children: [
            // Animated Icon
            TweenAnimationBuilder<double>(
              tween: Tween(begin: 0.0, end: 1.0),
              duration: Duration(milliseconds: 1500),
              curve: Curves.elasticOut,
              builder: (context, value, child) {
                return Transform.scale(
                  scale: value,
                  child: Container(
                    padding: EdgeInsets.all(12),
                    decoration: BoxDecoration(
                      gradient: LinearGradient(
                        colors: [
                          AppColor.warning,
                          AppColor.warning.withOpacity(0.8),
                        ],
                        begin: Alignment.topLeft,
                        end: Alignment.bottomRight,
                      ),
                      shape: BoxShape.circle,
                      boxShadow: [
                        BoxShadow(
                          color: AppColor.warning.withOpacity(0.3),
                          blurRadius: 15,
                          offset: Offset(0, 6),
                        ),
                      ],
                    ),
                    child: Icon(
                      Icons.stars_rounded,
                      color: AppColor.white,
                      size: 24,
                    ),
                  ),
                );
              },
            ),

            SizedBox(height: 16),

            Text(
              'HADIAH UTAMA',
              style: AppStyle.xs.copyWith(
                color: AppColor.white.withOpacity(0.7),
                fontWeight: FontWeight.w600,
                letterSpacing: 1.5,
              ),
            ),

            SizedBox(height: 6),

            Text(
              'Jika nomor undianmu sama,\nkamu pemenang 3KG Emas!',
              textAlign: TextAlign.center,
              style: AppStyle.md.copyWith(
                color: AppColor.white,
                fontWeight: FontWeight.w600,
                height: 1.4,
              ),
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildInfoCards() {
    return SlideTransition(
      position: Tween<Offset>(begin: Offset(0, 0.8), end: Offset.zero).animate(
        CurvedAnimation(
          parent: _slideController,
          curve: Interval(0.5, 1.0, curve: Curves.easeOutCubic),
        ),
      ),
      child: Padding(
        padding: EdgeInsets.symmetric(horizontal: 20),
        child: Column(
          children: [
            // Check My Number Button
            Container(
              width: double.infinity,
              margin: EdgeInsets.only(bottom: 12),
              child: Material(
                color: Colors.transparent,
                child: InkWell(
                  onTap: hasCheckedResult ? null : _checkMyNumber,
                  borderRadius: BorderRadius.circular(16),
                  child: Container(
                    padding: EdgeInsets.all(16),
                    decoration: BoxDecoration(
                      gradient: hasCheckedResult
                          ? null
                          : LinearGradient(
                              colors: [
                                AppColor.warning.withOpacity(0.8),
                                AppColor.warning,
                              ],
                              begin: Alignment.topLeft,
                              end: Alignment.bottomRight,
                            ),
                      color: hasCheckedResult
                          ? AppColor.black.withOpacity(0.15)
                          : null,
                      borderRadius: BorderRadius.circular(16),
                      border: Border.all(
                        color: hasCheckedResult
                            ? AppColor.white.withOpacity(0.15)
                            : AppColor.warning.withOpacity(0.3),
                        width: 1,
                      ),
                      boxShadow: hasCheckedResult
                          ? null
                          : [
                              BoxShadow(
                                color: AppColor.warning.withOpacity(0.3),
                                blurRadius: 15,
                                offset: Offset(0, 5),
                              ),
                            ],
                    ),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        Icon(
                          hasCheckedResult
                              ? Icons.check_circle_rounded
                              : Icons.casino_rounded,
                          color: hasCheckedResult
                              ? AppColor.white.withOpacity(0.5)
                              : AppColor.white,
                          size: 20,
                        ),
                        SizedBox(width: 8),
                        Text(
                          hasCheckedResult
                              ? 'Sudah Dicek'
                              : 'Cek Nomorku Sekarang!',
                          style: AppStyle.md.copyWith(
                            color: hasCheckedResult
                                ? AppColor.white.withOpacity(0.5)
                                : AppColor.white,
                            fontWeight: FontWeight.w700,
                          ),
                        ),
                      ],
                    ),
                  ),
                ),
              ),
            ),

            // Next Draw Card
            Container(
              width: double.infinity,
              padding: EdgeInsets.all(16),
              decoration: BoxDecoration(
                color: AppColor.black.withOpacity(0.15),
                borderRadius: BorderRadius.circular(16),
                border: Border.all(
                  color: AppColor.white.withOpacity(0.15),
                  width: 1,
                ),
              ),
              child: Row(
                children: [
                  Container(
                    padding: EdgeInsets.all(10),
                    decoration: BoxDecoration(
                      color: AppColor.white.withOpacity(0.1),
                      borderRadius: BorderRadius.circular(10),
                    ),
                    child: Icon(
                      Icons.schedule_rounded,
                      color: AppColor.white,
                      size: 18,
                    ),
                  ),

                  SizedBox(width: 12),

                  Expanded(
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Text(
                          'Undian Berikutnya',
                          style: AppStyle.xs.copyWith(
                            color: AppColor.white.withOpacity(0.7),
                            fontWeight: FontWeight.w500,
                          ),
                        ),
                        SizedBox(height: 2),
                        Text(
                          'Diumumkan jam 8 malam',
                          style: AppStyle.sm.copyWith(
                            color: AppColor.white,
                            fontWeight: FontWeight.w600,
                          ),
                        ),
                      ],
                    ),
                  ),
                ],
              ),
            ),

            SizedBox(height: 12),

            // Check Ticket Card
            Container(
              width: double.infinity,
              padding: EdgeInsets.all(14),
              decoration: BoxDecoration(
                color: AppColor.white.withOpacity(0.08),
                borderRadius: BorderRadius.circular(14),
                border: Border.all(
                  color: AppColor.white.withOpacity(0.12),
                  width: 1,
                ),
              ),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Icon(
                    Icons.confirmation_number_outlined,
                    color: AppColor.white.withOpacity(0.8),
                    size: 16,
                  ),
                  SizedBox(width: 8),
                  Text(
                    'Nomormu hari ini: $userNumber',
                    style: AppStyle.xs.copyWith(
                      color: AppColor.white.withOpacity(0.9),
                      fontWeight: FontWeight.w500,
                    ),
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        decoration: BoxDecoration(
          gradient: LinearGradient(
            colors: AppColor.primaryGradient,
            begin: Alignment.topLeft,
            end: Alignment.bottomRight,
          ),
        ),
        child: SafeArea(
          child: FadeTransition(
            opacity: _fadeAnimation,
            child: ListView(
              physics: BouncingScrollPhysics(),
              children: [
                SizedBox(height: 20),

                // Title
                _buildTitle(),

                SizedBox(height: 24),

                // Lottery Card
                _buildLotteryCard(),

                SizedBox(height: 20),

                // Prize Card
                _buildPrizeCard(),

                SizedBox(height: 16),

                // Info Cards
                _buildInfoCards(),

                SizedBox(height: 20),
              ],
            ),
          ),
        ),
      ),
    );
  }
}

// Custom painter for winner celebration effect
class WinnerCelebrationPainter extends CustomPainter {
  final double animationValue;

  WinnerCelebrationPainter(this.animationValue);

  @override
  void paint(Canvas canvas, Size size) {
    final paint = Paint()..style = PaintingStyle.fill;

    // Draw floating golden particles
    for (int i = 0; i < 12; i++) {
      final angle = (i * 30) * (3.14159 / 180);
      final radius = animationValue * 50;
      final x = size.width / 2 + radius * cos(angle);
      final y = size.height / 2 + radius * sin(angle);

      paint.color = AppColor.warning.withOpacity(0.4 * animationValue);
      canvas.drawCircle(Offset(x, y), 3 * animationValue, paint);
    }
  }

  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) => true;
}
