import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:gifunavi/model/auth_user.dart';
import 'package:shared_preferences/shared_preferences.dart';

final authUserStateProvider =
    StateNotifierProvider<AuthUserState, AuthUser>((ref) {
  return AuthUserState();
});

class AuthUserState extends StateNotifier<AuthUser> {
  AuthUserState() : super(AuthUser());

  Future<void> saveToDevice(String val) async {
    final SharedPreferences prefs = await SharedPreferences.getInstance();
    await prefs.setString("user_token", val);
  }

  Future<String?> tokenFromDevice() async {
    final SharedPreferences prefs = await SharedPreferences.getInstance();
    return prefs.getString("user_token");
  }

  Future<void> addLogin(AuthUser user) async {
    state = user;
    await saveToDevice(user.auth_token!);
  }

  void removeLogin() {
    state = AuthUser();
  }
}
