import 'package:comwell_key_app/check_out/bloc/check_out_cubit.dart';
import 'package:comwell_key_app/check_out/bloc/check_out_state.dart';
import 'package:comwell_key_app/routing/app_routes.dart';
import 'package:comwell_key_app/themes/dark_theme.dart';
import 'package:comwell_key_app/utils/lottie_utils.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:go_router/go_router.dart';
import 'package:lottie/lottie.dart';

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

  @override
  State<CheckOutProcessingPage> createState() => _CheckOutProcessingPageState();
}

class _CheckOutProcessingPageState extends State<CheckOutProcessingPage>
    with SingleTickerProviderStateMixin {
  LottieComposition? loadingComposition;
  late final AnimationController animationController;

  @override
  void initState() {
    animationController = AnimationController(vsync: this);
    super.initState();
  }

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

  void onAnimationEnd() {
    playLoading();
  }

  void playLoading() {
    loadingComposition?.playBetween(
      animationController,
      "spinner",
      markerEnd: "success",
      repeat: true,
    );
  }

  void playError() async {
    loadingComposition?.playBetween(
      animationController,
      "error",
      repeat: true,
    );
    await Future<void>.delayed(const Duration(seconds: 1));
    if (mounted) Navigator.of(context).pop();
  }

  void playSuccess() async {
    await loadingComposition?.playBetween(
      animationController,
      "success",
      markerEnd: "error",
    );
    await Future<void>.delayed(const Duration(seconds: 1));
    if (mounted) Navigator.of(context).pop();
  }

  @override
  Widget build(BuildContext context) {
    final cubit = context.read<CheckoutCubit>();
    return BlocListener<CheckoutCubit, CheckoutState>(
      listenWhen: (previous, current) =>
          previous.successfulCheckout != current.successfulCheckout ||
          previous.hasCheckoutError != current.hasCheckoutError,
      listener: (context, state) {
        if (state.successfulCheckout) {
          context.push(
            AppRoutes.checkOutSuccess,
            extra: [cubit.booking.digitalCard, cubit.booking],
          );
        } else if (state.hasCheckoutError) {
          context.push(AppRoutes.checkOutError, extra: cubit.booking);
        }
      },
      child: Scaffold(
        body: Container(
          alignment: Alignment.center,
          color: sandColor[80],
          child: Builder(builder: (context) {
            return Lottie.asset(
              'assets/animations/load_animation.json',
              controller: animationController,
              onLoaded: (composition) {
                if (loadingComposition == null) {
                  loadingComposition = composition;
                  animationController.duration = composition.duration;
                  playLoading();
                }
              },
              fit: BoxFit.cover,
              width: 64,
              height: 64,
            );
          }),
        ),
      ),
    );
  }
}