import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:lottie/lottie.dart';
import 'package:nearledaily/constants/color_constants.dart';
import '../../../constants/font_constants.dart';
import '../../controllers/cart_controller/cart.dart';
import '../../controllers/dashboard_controller/dashboard_controller.dart';
import '../../features/orders/presentation/order_tracking.dart';
import '../../widgets/text_widget.dart';
import '../home_view.dart';

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

  @override
  State<OrderSuccessView> createState() => _OrderSuccessViewState();
}

class _OrderSuccessViewState extends State<OrderSuccessView>
    with TickerProviderStateMixin {
  late AnimationController _fadeController;
  late AnimationController _slideController;
  late AnimationController _pulseController;

  late Animation<double> _fadeAnim;
  late Animation<Offset> _slideAnim;
  late Animation<double> _pulseAnim;

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

    _fadeController = AnimationController(
      vsync: this,
      duration: const Duration(milliseconds: 800),
    );
    _slideController = AnimationController(
      vsync: this,
      duration: const Duration(milliseconds: 700),
    );
    _pulseController = AnimationController(
      vsync: this,
      duration: const Duration(milliseconds: 1500),
    )..repeat(reverse: true);

    _fadeAnim = CurvedAnimation(parent: _fadeController, curve: Curves.easeIn);
    _slideAnim = Tween<Offset>(
      begin: const Offset(0, 0.3),
      end: Offset.zero,
    ).animate(CurvedAnimation(parent: _slideController, curve: Curves.easeOut));
    _pulseAnim = Tween<double>(begin: 1.0, end: 1.06).animate(
      CurvedAnimation(parent: _pulseController, curve: Curves.easeInOut),
    );

    Future.delayed(const Duration(milliseconds: 200), () {
      _fadeController.forward();
      _slideController.forward();
    });
  }

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

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

    return PopScope(
      canPop: false,
      onPopInvokedWithResult: (didPop, result) {
        if (!didPop) Get.back();
      },
      child: Scaffold(
        backgroundColor: const Color(0xFFF6FBF4),
        body: Stack(
          children: [
            // Decorative background blobs
            Positioned(
              top: -60,
              right: -60,
              child: Container(
                width: 220,
                height: 220,
                decoration: BoxDecoration(
                  shape: BoxShape.circle,
                  color: ColorConstants.primaryColor.withOpacity(0.08),
                ),
              ),
            ),
            Positioned(
              bottom: 120,
              left: -80,
              child: Container(
                width: 200,
                height: 200,
                decoration: BoxDecoration(
                  shape: BoxShape.circle,
                  color: ColorConstants.primaryColor.withOpacity(0.06),
                ),
              ),
            ),

            SafeArea(
              child: FadeTransition(
                opacity: _fadeAnim,
                child: SlideTransition(
                  position: _slideAnim,
                  child: Padding(
                    padding: EdgeInsets.symmetric(horizontal: size.width * 0.06),
                    child: Column(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        SizedBox(height: size.height * 0.05),

                        // Lottie animation with soft card bg
                        Container(
                          width: size.width * 0.70,
                          height: size.width * 0.70,
                          decoration: BoxDecoration(
                            color: Colors.white,
                            shape: BoxShape.circle,
                            boxShadow: [
                              BoxShadow(
                                color: ColorConstants.primaryColor.withOpacity(0.15),
                                blurRadius: 40,
                                spreadRadius: 8,
                              ),
                            ],
                          ),
                          child: Lottie.asset(
                            repeat: false,
                            'assets/images/orderSuccess.json',
                            fit: BoxFit.contain,
                          ),
                        ),

                        SizedBox(height: size.height * 0.04),

                        // Headline
                        ReusableTextWidget(
                          text: 'Order Placed! 🎉',
                          color: const Color(0xFF1A2E1A),
                          fontFamily: FontConstants.fontFamily,
                          fontSize: 28,
                          fontWeight: FontWeight.w800,
                          textAlign: TextAlign.center,
                        ),

                        SizedBox(height: size.height * 0.012),

                        // Subtitle
                        ReusableTextWidget(
                          text: "Your order is confirmed and\nbeing processed right now.",
                          color: const Color(0xFF6B7C6B),
                          fontFamily: FontConstants.fontFamily,
                          fontSize: 16,
                          fontWeight: FontWeight.w400,
                          maxLines: 3,
                          textAlign: TextAlign.center,
                        ),

                        SizedBox(height: size.height * 0.04),

                        // Status chips row
                        Row(
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: [
                            _StatusChip(icon: Icons.check_circle_rounded, label: 'Confirmed', color: ColorConstants.primaryColor),
                            const SizedBox(width: 10),
                            _StatusChip(icon: Icons.inventory_2_rounded, label: 'Packing', color: Colors.orange),
                            const SizedBox(width: 10),
                            _StatusChip(icon: Icons.local_shipping_rounded, label: 'On the way', color: Colors.blueAccent),
                          ],
                        ),
                      ],
                    ),
                  ),
                ),
              ),
            ),
          ],
        ),

        bottomNavigationBar: SafeArea(
          child: Padding(
            padding: EdgeInsets.fromLTRB(
              size.width * 0.06,
              0,
              size.width * 0.06,
              size.height * 0.02,
            ),
            child: Column(
              mainAxisSize: MainAxisSize.min,
              children: [
                // Primary CTA
                ScaleTransition(
                  scale: _pulseAnim,
                  child: SizedBox(
                    width: double.infinity,
                    height: 56,
                    child: ElevatedButton(
                      onPressed: () {
                        controller.show.value = false;
                        final cartCtrl = Get.find<CartController>();
                        cartCtrl.appliedCoupon.value = "";
                        cartCtrl.amt.value = "";
                        Get.offAll(BottomNavigation());
                      },
                      style: ElevatedButton.styleFrom(
                        backgroundColor: ColorConstants.primaryColor,
                        foregroundColor: Colors.white,
                        elevation: 0,
                        shape: RoundedRectangleBorder(
                          borderRadius: BorderRadius.circular(16),
                        ),
                      ),
                      child: ReusableTextWidget(
                        text: 'Back to Home',
                        color: Colors.white,
                        fontFamily: FontConstants.fontFamily,
                        fontSize: 17,
                        fontWeight: FontWeight.w600,
                        textAlign: TextAlign.center,
                      ),
                    ),
                  ),
                ),

                SizedBox(height: size.height * 0.015),

                // Secondary CTA — Track Order
                SizedBox(
                  width: double.infinity,
                  height: 56,
                  child: OutlinedButton(
                    onPressed: () {
                      Get.to(() => DeliveryTrackingPage());

                    },
                    style: OutlinedButton.styleFrom(
                      foregroundColor: ColorConstants.primaryColor,
                      side: BorderSide(
                        color: ColorConstants.primaryColor,
                        width: 1.5,
                      ),
                      shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.circular(16),
                      ),
                    ),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        const Icon(Icons.local_shipping_outlined, size: 20),
                        const SizedBox(width: 8),
                        ReusableTextWidget(
                          text: 'Track Order',
                          color: ColorConstants.primaryColor,
                          fontFamily: FontConstants.fontFamily,
                          fontSize: 16,
                          fontWeight: FontWeight.w600,
                          textAlign: TextAlign.center,
                        ),
                      ],
                    ),
                  ),
                ),

                SizedBox(height: size.height * 0.015),
              ],
            ),
          ),
        ),
      ),
    );
  }
}

// Small reusable status chip widget
class _StatusChip extends StatelessWidget {
  final IconData icon;
  final String label;
  final Color color;

  const _StatusChip({
    required this.icon,
    required this.label,
    required this.color,
  });

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 8),

      child: SizedBox(),
    );
  }
}