import 'package:food_delivery_client/food_delivery_client.dart';

part 'browse_event.dart';

part 'browse_state.dart';

part 'browse_bloc.freezed.dart';

@injectable
class BrowseBloc extends Bloc<BrowseEvent, BrowseState> {
  BrowseBloc() : super(const BrowseState()) {
    on<_GetTopCategories>(_onGetTopCategories);
    on<_GetAllCategories>(_onGetAllCategories);
    on<_GetSearchHistory>(_onGetSearchHistory);
    on<_FocusChanged>(_onFocusChanged);
  }

  Future<void> _onGetTopCategories(
    _GetTopCategories event,
    Emitter<BrowseState> emit,
  ) async {
    emit(state.copyWith(topCategorySt: RequestStatus.loading));
    await Future.delayed(TimeDelayConst.duration3);
    emit(state.copyWith(topCategorySt: RequestStatus.loaded));
  }

  Future<void> _onGetAllCategories(
    _GetAllCategories event,
    Emitter<BrowseState> emit,
  ) async {
    emit(state.copyWith(allCategorySt: RequestStatus.loading));
    await Future.delayed(TimeDelayConst.duration3);
    emit(state.copyWith(allCategorySt: RequestStatus.loaded));
  }

  Future<void> _onGetSearchHistory(
    _GetSearchHistory event,
    Emitter<BrowseState> emit,
  ) async {
    emit(state.copyWith(searchHistorySt: RequestStatus.loading));
    await Future.delayed(TimeDelayConst.duration3);
    emit(state.copyWith(searchHistorySt: RequestStatus.loading));
  }

  void _onFocusChanged(_FocusChanged event, Emitter<BrowseState> emit) {
    emit(state.copyWith(isActive: event.isActive ?? !(state.isActive)));
  }
}
