import 'dart:async';
import 'package:bloc/bloc.dart';
import 'package:syncrow_web/pages/roles_and_permission/bloc/roles_permission_event.dart';
import 'package:syncrow_web/pages/roles_and_permission/bloc/roles_permission_state.dart';
import 'package:syncrow_web/pages/roles_and_permission/model/role_model.dart';

class RolesPermissionBloc
    extends Bloc<RolesPermissionEvent, RolesPermissionState> {
  RolesPermissionBloc() : super(RolesInitial()) {
    on<GetRoles>(_getRoles);
    on<ChangeTapSelected>(changeTapSelected);
  }
  List<RoleModel> roleModel = [];

  FutureOr<void> _getRoles(
      GetRoles event, Emitter<RolesPermissionState> emit) async {
    emit(UsersLoadingState());
    try {
      roleModel = [
        RoleModel(roleId: '1', roleImage: '', roleName: 'Admin'),
        RoleModel(roleId: '2', roleImage: '', roleName: 'Security'),
        RoleModel(roleId: '2', roleImage: '', roleName: 'Reception'),
      ];
      emit(UsersLoadedState());
    } catch (e) {
      emit(ErrorState(e.toString()));
    }
  }

  bool tapSelect = true;

  changeTapSelected(
      ChangeTapSelected event, Emitter<RolesPermissionState> emit) {
    try {
      emit(RolesLoadingState());
      tapSelect = event.selected;
      emit(ChangeTapStatus(select: !tapSelect));
    } catch (e) {
      emit(ErrorState(e.toString()));
    }
  }
}
