import 'package:diplomaticquarterapp/config/shared_pref_kay.dart';
import 'package:diplomaticquarterapp/core/enum/viewstate.dart';
import 'package:diplomaticquarterapp/core/service/AuthenticatedUserObject.dart';
import 'package:diplomaticquarterapp/models/Authentication/authenticated_user.dart';
import 'package:diplomaticquarterapp/uitl/app_shared_preferences.dart';
import 'package:flutter/material.dart';

import '../../locator.dart';

class BaseViewModel extends ChangeNotifier {
  ViewState _state = ViewState.Idle;
  bool isInternetConnection = true;
  bool isLogin = false;

  ViewState get state => _state;

  String error = "";

  AuthenticatedUser user;
  AppSharedPreferences sharedPref = AppSharedPreferences();
  AuthenticatedUserObject authenticatedUserObject = locator<AuthenticatedUserObject>();

  void setState(ViewState viewState) {
    _state = viewState;

    if (viewState == ViewState.Busy || viewState == ViewState.BusyLocal)
      error = "";

    if (hasListeners) notifyListeners();
  }

  BaseViewModel() {
    _getUser();
  }

  _getUser() async {
    var userData = await sharedPref.getObject(USER_PROFILE);
    if (userData != null) user = AuthenticatedUser.fromJson(userData);

    var isLogin = await sharedPref.getString(LOGIN_TOKEN_ID);
    this.isLogin = isLogin != null;

    notifyListeners();
  }

  @override
  void dispose() {
    removeListener(() {});
    super.dispose();
  }
}
