import 'dart:io';

import 'package:easy_localization/easy_localization.dart';
import 'package:hmg_flutter_template/main.dart';
import 'package:hmg_flutter_template/model/base/generic_response_model2.dart';
import 'package:hmg_flutter_template/model/base/post_params_model.dart';
import 'package:hmg_flutter_template/model/login/imei_details_model.dart';
import 'package:hmg_flutter_template/model/login/list_doctors_clinic_model.dart';
import 'package:hmg_flutter_template/model/login/member_login_model.dart';

class AppState {
  // static final AppState _instance = AppState._internal();
  //
  // AppState._internal();
  //
  // factory AppState() => _instance;

  //Tokens
  String? imei, logInTokenID, vidaAuthTokenID, vidaRefreshTokenID, authenticationTokenID;

  int projectID = 0;
  int clinicId = 0;

  //TODO: Must be -1 before login or logout
  int lastLoginTyp = -1;
  int? doctorUserId;
  int? employeeId;
  String? password;
  String? lastLoginDate;
  GetIMEIDetailsModel? lastLoginImeiDate;
  List<ListDoctorsClinicModel>? listDoctorsClinic;

  int setupId=91877;

  GenericResponseModel2? doctorProfile;

  bool isAuthenticated = false;

  set setIsAuthenticated(v) => isAuthenticated = v;

  bool isLogged = false;

  PostParamsModel _postParamsInitConfig = PostParamsModel(
    tokenID: "",
    languageID: 2,
    stamp: "",
    iPAdress: "9.9.9.9",
    versionID: 9,
    channel: 9,
    sessionID: "BlUSkYymTt",
    isLoginForDoctorApp: true,
    patientOutSA: false,
    vidaAuthTokenID: null,
    vidaRefreshTokenID: null,
    deviceTypeID: 1,
    generalID: 'Cs2020@2016\$2958',
  );

  void setPostParamsInitConfig() {
    isAuthenticated = false;
    isLogged = false;
    _postParams = _postParamsInitConfig;
  }

  PostParamsModel? _postParams;

  PostParamsModel? get postParamsObject => _postParams;

  // Map<String, dynamic> get postParamsJson => isLogged ? (_postParams?.toJsonAfterLogin() ?? {}) : (_postParams?.toJson() ?? {});
  Map<String, dynamic> get postParamsJson {
    _postParams!.stamp = DateTime.now().toIso8601String();
    _postParams?.vidaAuthTokenID = vidaAuthTokenID;
    _postParams?.vidaRefreshTokenID = vidaRefreshTokenID;
    _postParams?.imei = imei;
    if(isLogged)
      _postParams?.tokenID=authenticationTokenID;
    return isLogged ? _postParams?.toJsonLogged() ?? {} : _postParams?.toJson() ?? {};
  }

  void setPostParamsModel(PostParamsModel _postParams) {
    this._postParams = _postParams;
  }

  bool isArabic() => EasyLocalization.of(navigatorKey.currentContext!)?.locale.languageCode == "ar";

  int getLanguageID(context) => EasyLocalization.of(context)?.locale.languageCode == "ar" ? 1 : 2;

  MemberLoginModel? memberBeforeLogin;
}
