import 'package:equatable/equatable.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flux/core/blocs/session/session_cubit.dart';
import 'package:get_it/get_it.dart';
import 'package:shared_preferences/shared_preferences.dart';

class SettingsState extends Equatable {
  final bool isSingleUserMode;

  const SettingsState({this.isSingleUserMode = false});

  SettingsState copyWith({bool? isSingleUserMode}) {
    return SettingsState(
      isSingleUserMode: isSingleUserMode ?? this.isSingleUserMode,
    );
  }

  @override
  List<Object?> get props => [isSingleUserMode];
}

class SettingsCubit extends Cubit<SettingsState> {
  final SharedPreferences _prefs = GetIt.I.get<SharedPreferences>();

  SettingsCubit() : super(const SettingsState()) {
    final bool isSingleUserMode = _prefs.getBool('isSingleUserMode') ?? false;
    final sessionCubit = GetIt.I.get<SessionCubit>();
    sessionCubit.setIsSingleUserMode(isSingleUserMode);
    emit(state.copyWith(isSingleUserMode: isSingleUserMode));
  }

  void toggleSingleUserMode() {
    final bool isSingleUserMode = !state.isSingleUserMode;
    GetIt.I.get<SharedPreferences>().setBool(
      'isSingleUserMode',
      isSingleUserMode,
    );
    final sessionCubit = GetIt.I.get<SessionCubit>();
    sessionCubit.setIsSingleUserMode(isSingleUserMode);
    emit(state.copyWith(isSingleUserMode: !state.isSingleUserMode));
  }
}
