import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:logger/logger.dart';
import 'package:{{#snakeCase}}{{project_name}}{{/snakeCase}}/core/extensions/object_extension.dart';

class AppBlocObserver extends BlocObserver {
  final bool printEvent;
  final bool printError;
  final bool printChange;
  final bool printTransition;

  final Logger logger = Logger(printer: SimplePrinter());

  AppBlocObserver({
    this.printEvent = true,
    this.printError = true,
    this.printTransition = true,
    this.printChange = true,
  });

  @override
  void onEvent(Bloc<dynamic, dynamic> bloc, Object? event) {
    super.onEvent(bloc, event);
    if (printEvent) {
      event?.d(wrap: (obj) => 'onEvent $event');
    }
  }

  @override
  void onError(BlocBase<dynamic> bloc, Object error, StackTrace stackTrace) {
    if (printError) {
      error.e(
        wrap: (obj) => 'onError(${bloc.runtimeType}, $obj, $stackTrace)',
      );
    }
    super.onError(bloc, error, stackTrace);
  }

  @override
  void onChange(BlocBase<dynamic> bloc, Change<dynamic> change) {
    super.onChange(bloc, change);
    if (printChange) {
      change.d(wrap: (obj) => 'onChange(${bloc.runtimeType}, $obj)');
    }
  }

  @override
  void onTransition(
    Bloc<dynamic, dynamic> bloc,
    Transition<dynamic, dynamic> transition,
  ) {
    super.onTransition(bloc, transition);
    if (printTransition) {
      transition.d(wrap: (obj) => 'onTransition $obj');
    }
  }
}
