import 'dart:async';

import 'package:connectivity/connectivity.dart';
import 'package:diplomaticquarterapp/analytics/google-analytics.dart';
import 'package:diplomaticquarterapp/config/config.dart';
import 'package:diplomaticquarterapp/config/shared_pref_kay.dart';
import 'package:diplomaticquarterapp/core/model/privilege/PrivilegeModel.dart';
import 'package:diplomaticquarterapp/core/viewModels/base_view_model.dart';
import 'package:diplomaticquarterapp/locator.dart';
import 'package:diplomaticquarterapp/models/Appointments/laser_body_parts.dart';
import 'package:diplomaticquarterapp/uitl/PlatformBridge.dart';
import 'package:diplomaticquarterapp/uitl/app_shared_preferences.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_datetime_picker/flutter_datetime_picker.dart';

var isAppArabic = false;
class ProjectViewModel extends BaseViewModel {

  GAnalytics get analytics => locator<GAnalytics>();

  // Platform Bridge
  PlatformBridge platformBridge() {
    return PlatformBridge.shared();
  }

  AppSharedPreferences sharedPref = AppSharedPreferences();
  Locale _appLocale = Locale('ar');
  String currentLanguage = 'ar';
  bool _isArabic = false;
  bool isInternetConnection = true;
  bool isLoading = false;
  bool isError = false;
  String error = '';
  dynamic searchvalue;
  bool isLogin = false;
  int laserSelectionDuration;

  double _latitude;
  double _longitude;

  double get latitude => _latitude;
  double get longitude => _longitude;

  dynamic get searchValue => searchvalue;

  Locale get appLocal => _appLocale;

  LocaleType get localeType => isArabic ? LocaleType.ar : LocaleType.en;

  bool get isArabic => _isArabic;

  bool isLoginChild = false;
  List<PrivilegeModel> privilegeRootUser = List();
  List<PrivilegeModel> privilegeChildUser = List();

  List<PrivilegeModel> get privileges =>
      isLoginChild ? privilegeChildUser : privilegeChildUser;

  List<LaserBodyPart> selectedBodyPartList = [];

  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 setLatitudeLongitude(double lat, double long) {
    _latitude = lat;
    _longitude = long;
    notifyListeners();
  }

  Future loadSharedPrefLanguage() async {
    currentLanguage =
        await sharedPref.getStringWithDefaultValue(APP_LANGUAGE, 'ar');
    _appLocale = Locale(currentLanguage);
    isAppArabic = _isArabic = currentLanguage == 'ar';
    notifyListeners();
  }

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

    notifyListeners();
  }

  setPrivilegeModelList({List<PrivilegeModel> privilege}) {
    this.isLoginChild = isLoginChild;
    privilegeRootUser = privilege;

    AppGlobal.isNetworkDebugEnabled = havePrivilege(93);

    notifyListeners();
  }

  setPrivilege({privilegeList, bool isLoginChild = false}) {
    List<PrivilegeModel> privilege = List();

    privilegeList['List'][0]['ListPrivilege'].forEach((item) {
      privilege.add(PrivilegeModel.fromJson(item));
    });

    this.isLoginChild = isLoginChild;
    if (isLoginChild)
      privilegeChildUser = privilege;
    else
      privilegeRootUser = privilege;

    notifyListeners();
  }

  setIsLoginChild({@required bool isLoginChild}) {
    this.isLoginChild = isLoginChild;
    notifyListeners();
  }

  bool havePrivilege(int id) {
    bool isHavePrivilege = false;
    if (isLoginChild)
      privilegeChildUser.forEach((element) {
        if (element.iD == id) isHavePrivilege = element.privilege;
      });
    else {
      privilegeRootUser.forEach((element) {
        if (element.iD == id) isHavePrivilege = element.privilege;
      });
    }
    return isHavePrivilege;
  }

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

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