import 'package:baseproject/core/common/index.dart';
import 'package:baseproject/features/repositories/hra_repository.dart';
import 'package:baseproject/features/usecases/index.dart';
import 'package:baseproject/features/usecases/user/user_use_cases.dart';

class UserBloc extends BaseCubit<BaseStateBloc<UserInfoDto?>> {
  UserBloc(this._userUseCases) : super(InitState<UserInfoDto?>(null));

  final UserUseCases _userUseCases;

  Future<bool> getUserInfo() async {
    final resultRefreshToken = await _userUseCases.refreshToken();
    if (resultRefreshToken.isRight()) {
      final resultUserInfo = await _userUseCases.getUserInfoFromApi();
      emit(LoadedState<UserInfoDto>(resultUserInfo.fold((l) => UserInfoDto(), (r) => r)));
    } else {
      emit(LoadedState<UserInfoDto?>(null));
    }
    return true;
  }

  updateUserInfo(UserInfoDto userInfo) {
    emit(LoadedState<UserInfoDto>(userInfo));
  }

  logout() {
    emit(LoadedState<UserInfoDto?>(null));
    _userUseCases.clearData();
  }
}
