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 = "";
  String languageID = "en";

  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();
  }

  setUser(AuthenticatedUser user) {
    this.user = user;
    notifyListeners();
  }

  BaseViewModel() {
    //authenticatedUserObject.getUser();
    user = authenticatedUserObject.user;
    this.isLogin = authenticatedUserObject.isLogin;
  }

  _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();
  }

  Future getSavedLanguage() async {
    languageID = await sharedPref.getString(APP_LANGUAGE);
  }

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