import 'package:bloc/bloc.dart';
import 'package:flutter/material.dart';

import 'invest_payment_event.dart';
import 'invest_payment_state.dart';

class InvestPaymentBloc extends Bloc<InvestPaymentEvent, InvestPaymentState> {
  final GlobalKey<FormState> formKey = GlobalKey<FormState>();
  TextEditingController amountController = TextEditingController();

  GlobalKey<FormState> getFormKey() {
    return formKey;
  }

  InvestPaymentBloc()
      : super(const InvestPaymentState(isFormValid: false, amount: 0)) {
    on<FormTextChanged>((event, emit) {
      final amount = double.tryParse(event.text.replaceAll(',', '')) ?? 0;
      emit(state.copyWith(isFormValid: event.text.isNotEmpty, amount: amount));
    });
  }

  @override
  Future<void> close() {
    amountController.dispose();

    return super.close();
  }
}
