import 'package:get_it/get_it.dart';
import 'package:shared_preferences/shared_preferences.dart';

final getIt = GetIt.instance;

enum AuthState { pending, unauthenticated, authenticated }

class Auth {
  AuthState _state = AuthState.unauthenticated;
  late String _sessionId = '';

  final SharedPreferences _prefs = getIt<SharedPreferences>();

  Auth({String? sessionId}) {
    if (sessionId != null) {
      _sessionId = sessionId;
      _state = AuthState.authenticated;
    } else {
      sessionId = _prefs.getString('sessionId');
      sessionId ??= 'TEST_SESSION';
      _sessionId = sessionId;
      _state = AuthState.authenticated;
    }
  }

  void resetSessionId() {
    _sessionId = '';
    _state = AuthState.unauthenticated;
  }

  Stream<AuthState> authState() async* {
    yield _state;
  }

  bool get isLoggedIn {
    return _sessionId.isNotEmpty;
  }
}
