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/widgets/ui_kit/dialogs/kw_dialog.dart';
import 'package:krow/core/presentation/widgets/ui_kit/kw_button.dart';
import 'package:krow/features/earning/domain/bloc/earnings_bloc.dart';
import 'package:krow/features/earning/domain/entities/earning_dispute_info.dart';
import 'package:krow/features/earning/domain/entities/earning_shift_entity.dart';
import 'package:krow/features/earning/presentation/widget/dispute_form_widget.dart';

class EarningActionWidget extends StatefulWidget {
  const EarningActionWidget({
    super.key,
    required this.earningShiftEntity,
  });

  final EarningShiftEntity earningShiftEntity;

  @override
  State<EarningActionWidget> createState() => _EarningActionWidgetState();
}

class _EarningActionWidgetState extends State<EarningActionWidget> {
  CrossFadeState _crossFadeState = CrossFadeState.showFirst;

  Future<void> _onConfirmPaymentPressed() async {
    final bloc = context.read<EarningsBloc>();

    await KwDialog.show<void>(
      context: context,
      icon: Assets.images.icons.moneyRecive,
      state: KwDialogState.positive,
      title: 'your_earnings_are_in'.tr(),
      message: '${'you_earned_for_shift'.tr(args: [
            widget.earningShiftEntity.earned.toString(),
            widget.earningShiftEntity.eventName,
            DateFormat('d MMM yyyy', context.locale.languageCode).format(
              widget.earningShiftEntity.eventDate,
            )
          ])}\n\n${'total_earnings_added'.tr(args: [
            widget.earningShiftEntity.earned.toString()
          ])}',
      primaryButtonLabel: 'confirm_earning'.tr(),
      secondaryButtonLabel: 'dispute_contact_support'.tr(),
      onPrimaryButtonPressed: (dialogContext) {
        Navigator.pop(dialogContext);

        bloc.add(ConfirmStaffEarning(widget.earningShiftEntity.id));
        setState(() => _crossFadeState = CrossFadeState.showSecond);
      },
      onSecondaryButtonPressed: (dialogContext) {
        Navigator.pop(dialogContext);

        _onDisputePaymentPressed();
      },
    );
  }

  Future<void> _onDisputePaymentPressed() async {
    final bloc = context.read<EarningsBloc>();
    final disputeInfo = EarningDisputeInfo();

    await KwDialog.show<void>(
      context: context,
      icon: Assets.images.icons.moneyRecive,
      state: KwDialogState.negative,
      title: 'dispute_earnings'.tr(),
      message: 'dispute_message'.tr(),
      primaryButtonLabel: 'Submit Dispute',
      secondaryButtonLabel: 'cancel'.tr(),
      child: DisputeFormWidget(disputeInfo: disputeInfo),
      onPrimaryButtonPressed: (dialogContext) {
        Navigator.pop(dialogContext);

        bloc.add(
          DisputeStaffEarning(
            earningId: widget.earningShiftEntity.id,
            reason: disputeInfo.reason,
            details: disputeInfo.details,
          ),
        );
        setState(() => _crossFadeState = CrossFadeState.showSecond);
      },
      onSecondaryButtonPressed: (dialogContext) {
        Navigator.pop(dialogContext);
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    return AnimatedCrossFade(
      duration: Durations.medium4,
      crossFadeState: _crossFadeState,
      secondChild: const SizedBox.shrink(),
      firstChild: Padding(
        padding: const EdgeInsets.only(left: 20, right: 20, top: 24),
        child: KwButton.outlinedPrimary(
          label: 'confirm'.tr(),
          onPressed: _onConfirmPaymentPressed,
        ),
      ),
    );
  }
}
