import 'dart:async';

import 'package:connectivity_plus/connectivity_plus.dart';
import 'package:connectivity_plus/connectivity_plus.dart';
import 'package:doctor_app_flutter/client/base_app_client.dart';
import 'package:doctor_app_flutter/config/config.dart';
import 'package:doctor_app_flutter/config/shared_pref_kay.dart';
import 'package:doctor_app_flutter/core/model/doctor/clinic_model.dart';
import 'package:doctor_app_flutter/core/model/doctor/doctor_profile_model.dart';
import 'package:doctor_app_flutter/core/model/hospitals/VidaPlusProjectListModel.dart';
import 'package:doctor_app_flutter/utils/dr_app_shared_pref.dart';
import 'package:doctor_app_flutter/utils/utils.dart';
import 'package:flutter/cupertino.dart';
import 'package:provider/provider.dart';

import 'authentication_view_model.dart';

Utils helpers = Utils();

class ProjectViewModel with ChangeNotifier {
  DrAppSharedPreferances sharedPref = DrAppSharedPreferances();

  Locale _appLocale = Locale('ar');

  String? currentLanguage = 'ar';
  bool _isArabic = false;
  static bool _isVidaPlusProject = false;
  bool isInternetConnection = true;
  List<ClinicModel> doctorClinicsList = [];
  bool isLoading = false;
  bool isError = false;
  String error = '';
  BaseAppClient baseAppClient = BaseAppClient();

  Locale get appLocal => _appLocale;

  bool get isArabic => _isArabic;
  late StreamSubscription subscription;

  List<VidaPlusProjectListModel> _vidaPlusProjectListModel = [];

  List<VidaPlusProjectListModel> _vidaPlusInPatientsProjectListModel = [];

  List<VidaPlusProjectListModel> get vidaPlusProjectList => _vidaPlusProjectListModel;

  List<VidaPlusProjectListModel> get vidaPlusInPatientsProjectListModel => _vidaPlusInPatientsProjectListModel;

  ProjectViewModel() {
    loadSharedPrefLanguage();

    subscription = Connectivity().onConnectivityChanged.listen((List<ConnectivityResult> result) {
      isInternetConnection = result.contains(ConnectivityResult.wifi)||result.contains(ConnectivityResult.mobile);
      notifyListeners();
    });
  }

  setVidaPlusProjectList(List<VidaPlusProjectListModel> vidaPlusProjectListModelInput) {
    _vidaPlusProjectListModel = vidaPlusProjectListModelInput;
    notifyListeners();
  }

  setVidaPlusInPatientProjectList(List<VidaPlusProjectListModel> vidaPlusInPatientProjectListModelInput) {
    _vidaPlusInPatientsProjectListModel = vidaPlusInPatientProjectListModelInput;
    notifyListeners();
  }

  void loadSharedPrefLanguage() async {
    currentLanguage = await sharedPref.getString(APP_Language);
    _appLocale = Locale(currentLanguage ?? 'en');
    _isArabic = currentLanguage != null
        ? currentLanguage == 'ar'
            ? true
            : false
        : false;
    notifyListeners();
  }

  void changeLanguage(String lan) {
    if (lan != "en" && currentLanguage != lan) {
      _appLocale = Locale("ar");
      _isArabic = true;
      currentLanguage = 'ar';
      sharedPref.setString(APP_Language, 'ar');
    } else if (lan != "ar" && currentLanguage != lan) {
      _appLocale = Locale("en");
      _isArabic = false;
      currentLanguage = 'en';
      sharedPref.setString(APP_Language, 'en');
    }
    callServicesAfterChangeLang();
    notifyListeners();
  }

  callServicesAfterChangeLang() {
    getProfile();
    getDoctorClinicsList();
  }

  @override
  void dispose() {
    if (subscription != null) subscription.cancel();
    super.dispose();
  }

  Future<dynamic> getDoctorClinicsList() async {
    try {
      dynamic localRes;

      await baseAppClient.post(GET_CLINICS_FOR_DOCTOR, onSuccess: (dynamic response, int statusCode) {
        doctorClinicsList = [];
        response['List_DoctorsClinic'].forEach((v) {
          doctorClinicsList.add(new ClinicModel.fromJson(v));
        });
        _vidaPlusProjectListModel.clear();
        if (response['ProjectListVidaPlus'].length != 0) {
          response['ProjectListVidaPlus'].forEach((item) {
            _vidaPlusProjectListModel.add(VidaPlusProjectListModel.fromJson(item));
          });
          setVidaPlusProjectList(_vidaPlusProjectListModel);
        }

        _vidaPlusInPatientsProjectListModel.clear();
        if (response['ProjectListHMCForDoctorAppInPatient'].length != 0) {
          response['ProjectListHMCForDoctorAppInPatient'].forEach((item) {
            _vidaPlusInPatientsProjectListModel.add(VidaPlusProjectListModel.fromJson(item));
          });
          setVidaPlusInPatientProjectList(_vidaPlusInPatientsProjectListModel);
        }

        localRes = response;
      }, onFailure: (String error, int statusCode) {
        throw error;
      }, body: Map());

      return Future.value(localRes);
    } catch (error) {
      //print(error);
      //throw error;
    }
  }

  void getProfile() async {
    Map<String, dynamic> profile = await sharedPref.getObj(DOCTOR_PROFILE);
    DoctorProfileModel doctorProfile = new DoctorProfileModel.fromJson(profile);
    ClinicModel clinicModel = ClinicModel(
      doctorID: doctorProfile.doctorID,
      clinicID: doctorProfile.clinicID,
      projectID: doctorProfile.projectID,
    );

    await Provider.of<AuthenticationViewModel>(AppGlobal.CONTEX, listen: false).getDoctorProfileBasedOnClinic(clinicModel);
  }


  static setVidaPlusStatus(bool isVidaPlus){
    _isVidaPlusProject = isVidaPlus;
  }

  static getVidaPlusStatus(){
    return _isVidaPlusProject;
  }
}
