import 'dart:async';

import 'package:connectivity_plus/connectivity_plus.dart';
import 'package:hmg_patient_app/analytics/google-analytics.dart';
import 'package:hmg_patient_app/config/shared_pref_kay.dart';
import 'package:hmg_patient_app/core/model/privilege/HMCProjectListModel.dart';
import 'package:hmg_patient_app/core/model/privilege/PrivilegeModel.dart';
import 'package:hmg_patient_app/core/model/privilege/ProjectDetailListModel.dart';
import 'package:hmg_patient_app/core/model/privilege/VidaPlusProjectListModel.dart';
import 'package:hmg_patient_app/core/viewModels/base_view_model.dart';
import 'package:hmg_patient_app/locator.dart';
import 'package:hmg_patient_app/models/Appointments/laser_body_parts.dart';
import 'package:hmg_patient_app/models/Authentication/register_info_response.dart';
import 'package:hmg_patient_app/models/InPatientServices/get_admission_info_response_model.dart';
import 'package:hmg_patient_app/models/InPatientServices/get_admission_request_info_response_model.dart';
import 'package:hmg_patient_app/uitl/PlatformBridge.dart';
import 'package:hmg_patient_app/uitl/app_shared_preferences.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_datetime_picker_plus/flutter_datetime_picker_plus.dart';

import '../../models/Appointments/DoctorListResponse.dart';

bool 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 = true;
  bool isInternetConnection = true;
  bool isLoading = false;
  bool isError = false;
  String error = '';
  dynamic searchvalue;
  bool isLogin = false;
  bool _isAllAppointmentsLoaded = false;

  bool get isAllAppointmentsLoaded => _isAllAppointmentsLoaded;
  bool isPatientAdmitted = false;
  bool patientHasAdmissionRequest = false;
  int inPatientProjectID = 0;
  GetAdmissionInfoResponseModel getAdmissionInfoResponseModel = GetAdmissionInfoResponseModel();
  GetAdmissionRequestInfoResponseModel getAdmissionRequestInfoResponseModel = GetAdmissionRequestInfoResponseModel();

  bool isIndoorNavigationEnabled = true;

  int waitingAppointmentProjectID = 0;
  DoctorList? waitingAppointmentDoctor;
  String waitingAppointmentNFCCode = "";

  setWaitingAppointmentDoctor(DoctorList waitingAppointmentDoctor) {
    this.waitingAppointmentDoctor = waitingAppointmentDoctor;
    notifyListeners();
  }

  setWaitingAppointmentNFCCode(String waitingAppointmentNFCCode) {
    this.waitingAppointmentNFCCode = waitingAppointmentNFCCode;
    notifyListeners();
  }

  setWaitingAppointmentProjectID(int projectID) {
    this.waitingAppointmentProjectID = projectID;
    notifyListeners();
  }

  void setIsAllAppointmentsLoaded(bool value) {
    _isAllAppointmentsLoaded = value;
    notifyListeners();
  }

  RegisterInfoResponse _registerInfo = RegisterInfoResponse();

  RegisterInfoResponse get registerInfo => _registerInfo;

  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<PrivilegeModel> privilegeChildUser = [];
  List<VidaPlusProjectListModel> _vidaPlusProjectListModel = [];
  List<HMCProjectListModel> _hMCProjectListModel = [];
  List<ProjectDetailListModel> _projectDetailListModel = [];

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

  List<VidaPlusProjectListModel> get vidaPlusProjectList => _vidaPlusProjectListModel;

  List<HMCProjectListModel> get hMCProjectListModel => _hMCProjectListModel;

  List<ProjectDetailListModel> get projectDetailListModel => _projectDetailListModel;

  List<LaserBodyPart> selectedBodyPartList = [];

  late StreamSubscription subscription;

  late double _latitude = 0.0;
  late double _longitude = 0.0;

  double get latitude => _latitude;

  double get longitude => _longitude;

  late int laserSelectionDuration = 0;

  ProjectViewModel() {
    try {
      loadSharedPrefLanguage();
      subscription = Connectivity().onConnectivityChanged.listen((List<ConnectivityResult> result) {
        if (result.first == ConnectivityResult.mobile) {
          isInternetConnection = true;
        } else if (result.first == ConnectivityResult.wifi) {
        } else if (result.first == ConnectivityResult.none) {
          isInternetConnection = false;
        }
        notifyListeners();
      });
    } catch (e) {
      print(e);
    }
  }

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

  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();
  }

  setIsPatientAdmitted(bool isAdmitted) {
    this.isPatientAdmitted = isAdmitted;
    notifyListeners();
  }

  setIsIndoorNavigationEnabled(bool isEnabled) {
    this.isIndoorNavigationEnabled = isEnabled;
    notifyListeners();
  }

  setPatientHasAdmissionRequest(bool hasAdmissionRequest) {
    this.patientHasAdmissionRequest = hasAdmissionRequest;
    notifyListeners();
  }

  setInPatientProjectID(int projectID) {
    this.inPatientProjectID = projectID;
    notifyListeners();
  }

  setInPatientAdmissionInfo(GetAdmissionInfoResponseModel _getAdmissionInfoResponseModel) {
    this.getAdmissionInfoResponseModel = _getAdmissionInfoResponseModel;
    notifyListeners();
  }

  setInPatientAdmissionRequest(GetAdmissionRequestInfoResponseModel _getAdmissionRequestInfoResponseModel) {
    this.getAdmissionRequestInfoResponseModel = _getAdmissionRequestInfoResponseModel;
    notifyListeners();
  }

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

    notifyListeners();
  }

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

  setProjectsDetailList(List<ProjectDetailListModel> projectDetailListModel) {
    _projectDetailListModel = projectDetailListModel;
    notifyListeners();
  }

  setHMCProjectList(List<HMCProjectListModel> hMCProjectListModel) {
    _hMCProjectListModel = hMCProjectListModel;
    notifyListeners();
  }

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

    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;
    try {
      if (isLoginChild) {
        privilegeChildUser.forEach((element) {
          if (element.iD == id) isHavePrivilege = element.privilege!;
        });
      } else {
        privilegeRootUser.forEach((element) {
          if (element.iD == id) isHavePrivilege = element.privilege!;
        });
      }
    } catch (e) {
      print(e);
    }
    return isHavePrivilege;
  }

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

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

  void setLatitudeLongitude(double lat, double long) {
    _latitude = lat;
    _longitude = long;
    notifyListeners();
  }

  setRegisterData(RegisterInfoResponse data) {
    _registerInfo = data;
    notifyListeners();
  }
}
