import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:proxibuy/core/gen/assets.gen.dart';
import 'package:proxibuy/domain/entities/category_entity.dart';
import 'notification_preferences_event.dart';
import 'notification_preferences_state.dart';

class NotificationPreferencesBloc
    extends Bloc<NotificationPreferencesEvent, NotificationPreferencesState> {
  NotificationPreferencesBloc() : super(const NotificationPreferencesState()) {
    on<LoadCategories>(_onLoadCategories);
    on<ToggleCategorySelection>(_onToggleCategorySelection);
  }

  void _onLoadCategories(
      LoadCategories event, Emitter<NotificationPreferencesState> emit) {
    final categories = [
      CategoryEntity(id: 1, name: 'تریا', icon: Assets.icons.teria),
      CategoryEntity(id: 2, name: 'پوشاک', icon: Assets.icons.pooshak),
      CategoryEntity(id: 3, name: 'فست‌فود', icon: Assets.icons.fastfood),
      CategoryEntity(id: 4, name: 'کافی‌شاپ', icon: Assets.icons.coffeeshop),
      CategoryEntity(id: 5, name: 'رستوران', icon: Assets.icons.resturan),
      CategoryEntity(id: 6, name: 'لوازم دیجیتال', icon: Assets.icons.digital),
      CategoryEntity(id: 7, name: 'کیف‌وکفش', icon: Assets.icons.kafsh),
      CategoryEntity(id: 8, name: 'سینما', icon: Assets.icons.cinama),
      CategoryEntity(id: 9, name: 'لوازم آرایشی', icon: Assets.icons.arayesh),
      CategoryEntity(id: 10, name: 'طلا و زیورآلات', icon: Assets.icons.tala),
    ];
    emit(state.copyWith(categories: categories));
  }

  void _onToggleCategorySelection(ToggleCategorySelection event,
      Emitter<NotificationPreferencesState> emit) {
    final selectedIds = Set<int>.from(state.selectedCategoryIds);
    if (selectedIds.contains(event.categoryId)) {
      selectedIds.remove(event.categoryId);
    } else {
      selectedIds.add(event.categoryId);
    }
    emit(state.copyWith(selectedCategoryIds: selectedIds));
  }
}