import 'package:bloc/bloc.dart';
import 'package:equatable/equatable.dart';
import 'package:unit2/model/rbac/rbac.dart';
import 'package:unit2/model/rbac/role_under.dart';

import '../../../../../sevices/roles/est_point_person/est_point_person_role_assignment_services.dart';
import '../../../../../sevices/roles/rbac_operations/role_services.dart';
import '../../../../../sevices/roles/rbac_operations/roles_under_services.dart';

part 'est_point_person_assinable_role_event.dart';
part 'est_point_person_assinable_role_state.dart';

class EstPointPersonAssinableRoleBloc extends Bloc<
    EstPointPersonAssinableRoleEvent, EstPointPersonAssinableRoleState> {
  EstPointPersonAssinableRoleBloc()
      : super(EstPointPersonAssinableRoleInitial()) {
    List<RBAC> rolesUnder = [];
    List<RBAC> roles = [];
    RBAC? mainRole;
    on<GetEstPointPersonAssignableRoles>((event, emit) async {
      emit(EstPointPersonAssignableRoleLoadingState());
      try {
        if (rolesUnder.isEmpty) {
          List<RolesUnder> rolesUnders = await EstPointPersonRoleAssignment
              .instance
              .getRolesUnder(roleId: event.roleId);
          for (var roleUnder in rolesUnders) {
            RBAC roleChild = roleUnder.roleUnderChild;
            roleChild.id = roleUnder.id;
            rolesUnder.add(roleUnder.roleUnderChild);
          }
        }
        if (roles.isEmpty) {
          roles = await RbacRoleServices.instance.getRbacRoles();
        }
        RBAC role = roles.firstWhere((e) => e.id == event.roleId);
        mainRole = role;
        emit(EstPointPersonAssignableRoleLoaded(
            roles: roles, rolesUnder: rolesUnder, mainRole: role));
      } catch (e) {
        emit(EstPointPersonAssignableErrorState(message: e.toString()));
      }
    });
    on<AddEstPointPersonAssignableRoles>((event, emit) async {
      try {
        emit(EstPointPersonAssignableRoleLoadingState());
        Map<dynamic, dynamic> statusResponse = await RbacRoleUnderServices
            .instance
            .add(roleId: event.mainRoleId, rolesId: event.rolesUnder);

        if (statusResponse['success']) {
          emit(EstPointPersonAssignableRoleLoaded(
              mainRole: mainRole!, roles: roles, rolesUnder: rolesUnder));
        } else {
          emit(EstPointPersonAssignableRoleLoaded(
              mainRole: mainRole!, roles: roles, rolesUnder: rolesUnder));
        }
      } catch (e) {
        emit(EstPointPersonAssignableErrorState(message: e.toString()));
      }
    });

       on<DeleteEstPointPersonAssignableRoles>((event, emit) async {
      emit(EstPointPersonAssignableRoleLoadingState());
      try {
        bool success = await RbacRoleUnderServices.instance
            .deleteRbacRoleUnder(roleUnderId: event.roleId);
        if (success) {
          rolesUnder.removeWhere((element) => element.id == event.roleId);
          emit(EstPointPersonAssignableDeletedState(success: success));
        } else {
          emit(EstPointPersonAssignableDeletedState(success: success));
        }
      } catch (e) {
        emit(EstPointPersonAssignableErrorState(message: e.toString()));
      }
    });
  }
}
