import 'package:bloc/bloc.dart';
import 'package:equatable/equatable.dart';
import 'package:unit2/utils/global.dart';

import '../../../model/offline/offlane_modules.dart';
import '../../../model/offline/offline_profile.dart';

part 'offline_event.dart';
part 'offline_state.dart';

class OfflineBloc extends Bloc<OfflineEvent, OfflineState> {
  OfflineBloc() : super(OfflineInitial()) {
    on<SwitchOffline>((event, emit) async {
      try {
        List<dynamic> modules = await OFFLINE!.get('modules');
        List<OfflineModules> offlineModules = [];
        for (var module in modules) {
          offlineModules.add(module);
        }
        OfflineProfile offlineProfile = await OFFLINE!.get('offline_profile');
        emit(OfflineModeState(
            offlineModules: offlineModules, offlineProfile: offlineProfile));
      } catch (e) {
        emit(OfflineErrorState(message: e.toString()));
      }
    });
  }
}
