import 'package:e_commerce_training/src/model/preference/app_preference.dart';
import 'package:e_commerce_training/src/util/const.dart';
import 'package:e_commerce_training/src/util/util.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

import 'authentication.dart';

class AuthenticationCubit extends Cubit<AuthenticationState> {

  AuthenticationCubit() : super(AuthenticationUninitialized());

  bool get isLoggedIn => !Util.isEmpty(appPreference.getData(Const.TOKEN));

  void openApp() {
    emit(AuthenticationUninitialized());
    if (isLoggedIn)
      emit(AuthenticationAuthenticated());
    else
      emit(AuthenticationUnauthenticated());
  }

  void login() {
    emit(AuthenticationLoading());
    emit(AuthenticationAuthenticated());
  }

  void logout() {
    emit(AuthenticationLoading());
    emit(AuthenticationUnauthenticated());
  }
}
