import 'dart:async';

import 'package:connectivity/connectivity.dart';
import 'package:doctor_app_flutter/config/config.dart';
import 'package:doctor_app_flutter/config/shared_pref_kay.dart';
import 'package:doctor_app_flutter/models/doctor/doctor_profile_model.dart';
import 'package:doctor_app_flutter/models/doctor/profile_req_Model.dart';
import 'package:doctor_app_flutter/providers/auth_provider.dart';
import 'package:doctor_app_flutter/util/dr_app_shared_pref.dart';
import 'package:doctor_app_flutter/util/helpers.dart';
import 'package:flutter/cupertino.dart';
import 'package:provider/provider.dart';
Helpers helpers = Helpers();

class ProjectProvider with ChangeNotifier {
  DrAppSharedPreferances sharedPref = DrAppSharedPreferances();
  Locale _appLocale;
  String currentLanguage = 'ar';
  bool _isArabic = false;
  bool isInternetConnection = true;

  Locale get appLocal => _appLocale;

  bool get isArabic => _isArabic;
  StreamSubscription subscription;
  // AuthProvider authProvider = AuthProvider(); 
  ProjectProvider() {
    loadSharedPrefLanguage();

    subscription = Connectivity()
        .onConnectivityChanged
        .listen((ConnectivityResult result) {
      switch (result) {
        case ConnectivityResult.wifi:
          isInternetConnection = true;
          break;
        case ConnectivityResult.mobile:
          isInternetConnection = true;
          break;
        case ConnectivityResult.none:
          isInternetConnection = false;
          break;
      }
      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');
    }
     getProfile();
    notifyListeners();
  }

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

  void getProfile()async{
    Map profile = await sharedPref.getObj(DOCTOR_PROFILE);
    DoctorProfileModel doctorProfile = new DoctorProfileModel.fromJson(profile);
    ProfileReqModel docInfo = new ProfileReqModel(
        doctorID: doctorProfile.doctorID,
        clinicID: doctorProfile.clinicID,
        license: true,
        projectID: doctorProfile.projectID,
        tokenID: '',
        languageID: 2);

    Provider.of<AuthProvider>(AppGlobal.CONTEX,listen: false)
    .getDocProfiles(docInfo.toJson()).then((res) async {
      sharedPref.setObj(DOCTOR_PROFILE, res['DoctorProfileList'][0]);
    }).catchError((err) {
      print(err);
    });
  }

  
}
