import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import '../../blocs/user/user_bloc.dart';
import '../../blocs/user/user_state.dart' as user_state;

class UserStateWrapper extends StatelessWidget {
  final Widget Function(BuildContext context, dynamic user) builder;
  final Widget? loadingWidget;
  final Widget? errorWidget;
  final Widget? noUserWidget;

  const UserStateWrapper({
    super.key,
    required this.builder,
    this.loadingWidget,
    this.errorWidget,
    this.noUserWidget,
  });

  @override
  Widget build(BuildContext context) {
    return BlocBuilder<UserBloc, user_state.UserState>(
      builder: (context, state) {
        if (state is user_state.UserLoading) {
          return loadingWidget ??
              const Center(child: CircularProgressIndicator());
        }

        if (state is user_state.UserError) {
          return errorWidget ?? Center(child: Text('Erreur: ${state.message}'));
        }

        if (state is! user_state.UserLoaded) {
          return noUserWidget ??
              const Center(child: Text('Aucun utilisateur connecté.'));
        }

        return builder(context, state.user);
      },
    );
  }
}
