import 'dart:async';

import 'package:auto_route/auto_route.dart';
import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:krow/core/presentation/gen/assets.gen.dart';
import 'package:krow/core/presentation/styles/kw_text_styles.dart';
import 'package:krow/core/presentation/styles/theme.dart';
import 'package:krow/core/presentation/widgets/ui_kit/dialogs/kw_dialog.dart';
import 'package:krow/core/presentation/widgets/ui_kit/kw_button.dart';
import 'package:krow/features/profile/benefits/domain/bloc/benefits_bloc.dart';
import 'package:krow/features/profile/benefits/domain/entities/benefit_entity.dart';
import 'package:krow/features/profile/benefits/presentation/widgets/benefit_history_widget.dart';

class BenefitCardWidget extends StatefulWidget {
  const BenefitCardWidget({super.key, required this.benefit});

  final BenefitEntity benefit;

  @override
  State<BenefitCardWidget> createState() => _BenefitCardWidgetState();
}

class _BenefitCardWidgetState extends State<BenefitCardWidget>
    with TickerProviderStateMixin {
  late final AnimationController _animationController;
  late BenefitEntity _benefit = widget.benefit;
  Completer<BenefitEntity>? _requestCompleter;
  double _progress = 0;
  bool _isReady = false;

  @override
  void initState() {
    _progress = widget.benefit.progress;
    _isReady = _progress == 1;

    super.initState();

    _animationController = AnimationController(vsync: this);
    _animationController.animateTo(
      _progress,
      duration: const Duration(seconds: 1),
      curve: Curves.easeOut,
    );
  }

  Future<void> _handleRequestPress() async {
    _requestCompleter?.completeError(Exception('previous_aborted'.tr()));
    final completer = _requestCompleter = Completer<BenefitEntity>();

    this.context.read<BenefitsBloc>().add(
          SendBenefitRequest(
            benefit: _benefit,
            requestCompleter: completer,
          ),
        );

    final benefit = await completer.future;
    if (!benefit.isClaimed) return;

    setState(() {
      _progress = 0;
      _isReady = false;
      _benefit = _benefit.copyWith(currentHours: 0);
    });

    await _animationController.animateTo(
      _progress,
      duration: const Duration(seconds: 1),
      curve: Curves.easeOut,
    );

    final context = this.context;
    if (!context.mounted) return;

    await KwDialog.show(
      context: context,
      icon: Assets.images.icons.like,
      state: KwDialogState.positive,
      title: 'request_submitted'.tr(),
      message: 'request_submitted_message'.tr(args: [_benefit.name.toLowerCase()]),
      primaryButtonLabel: 'back_to_profile'.tr(),
      onPrimaryButtonPressed: (dialogContext) {
        Navigator.maybePop(dialogContext);
        context.maybePop();
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    return DecoratedBox(
      decoration: const BoxDecoration(
        color: AppColors.grayPrimaryFrame,
        borderRadius: BorderRadius.all(Radius.circular(8)),
      ),
      child: Padding(
        padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 16),
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            Row(
              children: [
                Stack(
                  alignment: AlignmentDirectional.center,
                  children: [
                    AnimatedBuilder(
                      animation: _animationController,
                      builder: (context, _) {
                        return CircularProgressIndicator(
                          constraints:
                              BoxConstraints.tight(const Size.square(90)),
                          strokeWidth: 8,
                          strokeCap: StrokeCap.round,
                          backgroundColor: AppColors.bgColorLight,
                          color: _isReady
                              ? AppColors.statusSuccess
                              : AppColors.primaryBlue,
                          value: _animationController.value,
                        );
                      },
                    ),
                    Column(
                      mainAxisSize: MainAxisSize.min,
                      children: [
                        RichText(
                          text: TextSpan(
                            text: '${_benefit.currentHours}/',
                            style: AppTextStyles.headingH3,
                            children: [
                              TextSpan(
                                text: '${_benefit.requiredHours}',
                                style: AppTextStyles.headingH3.copyWith(
                                  color: _isReady
                                      ? AppColors.blackBlack
                                      : AppColors.blackCaptionText,
                                ),
                              ),
                            ],
                          ),
                        ),
                        const SizedBox(height: 4),
                        Text(
                          'hours'.tr().toLowerCase(),
                          style: AppTextStyles.captionReg
                              .copyWith(color: AppColors.blackCaptionText),
                        ),
                      ],
                    )
                  ],
                ),
                const SizedBox(width: 24),
                Expanded(
                  child: Stack(
                    alignment: AlignmentDirectional.centerStart,
                    clipBehavior: Clip.none,
                    children: [
                      const SizedBox(height: 90),
                      Column(
                        mainAxisSize: MainAxisSize.min,
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Text(_benefit.name, style: AppTextStyles.headingH3),
                          const SizedBox(height: 6),
                          Text(
                            _benefit.requirement,
                            style: AppTextStyles.bodySmallReg.copyWith(
                              color: AppColors.blackGray,
                            ),
                          ),
                        ],
                      ),
                      Positioned(
                        top: -4,
                        right: 0,
                        child: Assets.images.icons.alertCircle.svg(
                          height: 16,
                          width: 16,
                          colorFilter: const ColorFilter.mode(
                            AppColors.grayStroke,
                            BlendMode.srcIn,
                          ),
                        ),
                      )
                    ],
                  ),
                ),
              ],
            ),
            const SizedBox(height: 20),
            if (_isReady)
              Container(
                margin: const EdgeInsets.only(bottom: 20),
                padding: const EdgeInsets.all(8),
                decoration: const BoxDecoration(
                  color: AppColors.grayWhite,
                  borderRadius: BorderRadius.all(Radius.circular(8)),
                ),
                child: Row(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    DecoratedBox(
                      decoration: const BoxDecoration(
                        color: AppColors.tintGreen,
                        shape: BoxShape.circle,
                      ),
                      child: SizedBox.square(
                        dimension: 28,
                        child: Assets.images.icons.checkCircle.svg(
                          height: 10,
                          width: 10,
                          fit: BoxFit.scaleDown,
                          colorFilter: const ColorFilter.mode(
                            AppColors.statusSuccess,
                            BlendMode.srcIn,
                          ),
                        ),
                      ),
                    ),
                    const SizedBox(width: 8),
                    Expanded(
                      child: Text(
                        _benefit.info,
                        style: AppTextStyles.bodyTinyMed.copyWith(
                          color: AppColors.statusSuccess,
                        ),
                      ),
                    ),
                  ],
                ),
              ),
            BenefitHistoryWidget(benefit: _benefit),
            if (_isReady)
              Padding(
                padding: const EdgeInsets.only(top: 20),
                child: KwButton.primary(
                  label: '${'request_payment_for'.tr()} ${_benefit.name}',
                  onPressed: _handleRequestPress,
                ),
              ),
          ],
        ),
      ),
    );
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
  }
}
