import 'package:flutter/material.dart';
import 'package:lottie/lottie.dart';
import 'package:confetti/confetti.dart';
import 'dart:math';
import '../../../app.dart';
import '../../../l10n/app_localizations.dart';

class BeneficiaryResultPage extends StatefulWidget {
  final bool isSuccess;

  const BeneficiaryResultPage({super.key, required this.isSuccess});

  @override
  State<BeneficiaryResultPage> createState() => _BeneficiaryResultPageState();
}

class _BeneficiaryResultPageState extends State<BeneficiaryResultPage> {
  late ConfettiController _confettiController;

  @override
  void initState() {
    super.initState();
    _confettiController =
        ConfettiController(duration: const Duration(seconds: 3));
    if (widget.isSuccess) {
      _confettiController.play();
    }
  }

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

  @override
  Widget build(BuildContext context) {
    const successAnimation = 'assets/animations/done.json';
    const errorAnimation = 'assets/animations/error.json';

    return Scaffold(
      backgroundColor: widget.isSuccess ? Colors.green[50] : Colors.red[50],
      body: Stack(
        alignment: Alignment.center,
        children: [
          Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Lottie.asset(
                widget.isSuccess ? successAnimation : errorAnimation,
                width: 150,
                height: 150,
                repeat: false,
              ),
              const SizedBox(height: 20),
              Text(
                widget.isSuccess
                    ? AppLocalizations.of(context).beneficiaryAddedSuccess
                    : AppLocalizations.of(context).beneficiaryAdditionFailed,
                style: TextStyle(
                  fontSize: 20,
                  fontWeight: FontWeight.bold,
                  color: widget.isSuccess ? Colors.green : Colors.red,
                ),
                textAlign: TextAlign.center,
              ),
            ],
          ),
          Positioned(
            bottom: 20, // keep it slightly above the very bottom
            left: 16,
            right: 16,
            child: SizedBox(
              height: 56, // larger button height
              child: ElevatedButton(
                onPressed: () {
                  Navigator.pushReplacement(
                    // ensures back goes to ScaffoldScreen
                    context,
                    MaterialPageRoute(
                      builder: (context) => const NavigationScaffold(),
                    ),
                  );
                },
                style: ElevatedButton.styleFrom(
                  shape: const StadiumBorder(),
                  padding: const EdgeInsets.symmetric(vertical: 12),
                  backgroundColor: Theme.of(context).primaryColorDark,
                  foregroundColor: Theme.of(context).scaffoldBackgroundColor,
                ),
                child: Text(
                  AppLocalizations.of(context).done,
                  style: TextStyle(
                      color: Theme.of(context)
                          .colorScheme
                          .onPrimaryContainer), // slightly bigger text
                ),
              ),
            ),
          ),
          if (widget.isSuccess)
            Align(
              alignment: Alignment.topCenter,
              child: ConfettiWidget(
                confettiController: _confettiController,
                blastDirection: pi / 2,
                maxBlastForce: 10,
                minBlastForce: 5,
                emissionFrequency: 0.05,
                numberOfParticles: 20,
                gravity: 0.2,
                shouldLoop: false,
              ),
            ),
        ],
      ),
    );
  }
}
