import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:krow/features/profile/wages_forms/domain/bloc/wages_form_bloc.dart';
import 'package:krow/features/profile/wages_forms/domain/entities/wage_form_entity.dart';

mixin FormUpdaterMixin<T extends StatefulWidget, F extends WageFormEntity>
    on State<T> {
  late final formBloc = context.read<WagesFormBloc>();
  late F form;
  Timer? updateTimer;

  void updateCurrentForm(F updatedForm) {
    updateTimer?.cancel();
    form = updatedForm;

    updateTimer = Timer(
      const Duration(milliseconds: 500),
      () {
        formBloc.add(UpdateCurrentForm(form: form));
      },
    );
  }

  void handleSubmit() {
    formBloc.add(const ValidateCurrentForm());
  }
}
