import 'package:doctor_app_flutter/config/shared_pref_kay.dart';
import 'package:doctor_app_flutter/core/enum/view_state.dart';
import 'package:doctor_app_flutter/core/model/doctor/doctor_profile_model.dart';
import 'package:doctor_app_flutter/utils/dr_app_shared_pref.dart';
import 'package:flutter/material.dart';

class BaseViewModel extends ChangeNotifier {
  DrAppSharedPreferances sharedPref = new DrAppSharedPreferances();
  DoctorProfileModel? doctorProfile;

  ViewState _state = ViewState.Idle;
  bool isInternetConnection = true;

  ViewState get state => _state;

  String? error = "";

  //TODO add the user login model when we need it

  void setState(ViewState viewState) {
    _state = viewState;

    notifyListeners();
  }

  Future<DoctorProfileModel> getDoctorProfile({bool isGetProfile = false}) async {
    if (isGetProfile) {
      Map<String, dynamic>? profile = await sharedPref.getObj(DOCTOR_PROFILE);
      if (profile != null) {
        doctorProfile = DoctorProfileModel.fromJson(profile);
        if (doctorProfile != null) {
          return doctorProfile!;
        }
      }
    }
    if (doctorProfile == null) {
      Map<String, dynamic>? profile = await sharedPref.getObj(DOCTOR_PROFILE);
      if (profile != null) {
        doctorProfile = DoctorProfileModel.fromJson(profile);
        if (doctorProfile != null) {
          return doctorProfile!;
        }
      }
      return DoctorProfileModel();
    } else {
      return doctorProfile!;
    }
  }

  void getIsolateDoctorProfile(bool isGetProfile) async {
    if (isGetProfile) {
      Map<String, dynamic>? profile = await sharedPref.getObj(DOCTOR_PROFILE);
      if (profile != null) {
        doctorProfile = DoctorProfileModel.fromJson(profile);
      }
    }
    if (doctorProfile == null) {
      Map<String, dynamic>? profile = await sharedPref.getObj(DOCTOR_PROFILE);
      if (profile != null) {
        doctorProfile = DoctorProfileModel.fromJson(profile);
      }
    }
  }

  setDoctorProfile(DoctorProfileModel doctorProfile) async {
    await sharedPref.setObj(DOCTOR_PROFILE, doctorProfile);
    this.doctorProfile = doctorProfile;
    notifyListeners();
  }
}
