import 'dart:async';

import 'package:connectivity/connectivity.dart';
import 'package:diplomaticquarterapp/config/shared_pref_kay.dart';
import 'package:diplomaticquarterapp/core/viewModels/base_view_model.dart';
import 'package:diplomaticquarterapp/locator.dart';
import 'package:diplomaticquarterapp/uitl/app_shared_preferences.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_datetime_picker/flutter_datetime_picker.dart';

class ProjectViewModel extends BaseViewModel {
  AppSharedPreferences sharedPref = AppSharedPreferences();
  Locale _appLocale;
  String currentLanguage = 'en';
  bool _isArabic = false;
  bool isInternetConnection = true;
  bool isLoading = false;
  bool isError = false;
  String error = '';
  dynamic searchvalue;
  dynamic get searchValue => searchvalue;
  Locale get appLocal => _appLocale;

  LocaleType get localeType => isArabic? LocaleType.en:LocaleType.ar;
  bool get isArabic => _isArabic;
  // BaseViewModel baseViewModel = locator<BaseViewModel>()
  StreamSubscription subscription;

  ProjectViewModel() {
    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
        : true;
    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');
    }

    notifyListeners();
  }

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

  setSearchValue(data) {
    searchvalue = data;
    notifyListeners();
  }
}
