import 'package:doctor_app_flutter/client/base_app_client.dart';
import 'package:doctor_app_flutter/config/config.dart';
import 'package:doctor_app_flutter/config/shared_pref_kay.dart';
import 'package:doctor_app_flutter/core/model/imei_details.dart';
import 'package:doctor_app_flutter/core/model/insert_imei_model.dart';
import 'package:doctor_app_flutter/core/viewModel/base_view_model.dart';
import 'package:doctor_app_flutter/models/auth/activation_Code_req_model.dart';
import 'package:doctor_app_flutter/models/auth/check_activation_code_request_model.dart';
import 'package:doctor_app_flutter/models/auth/send_activation_code_model2.dart';
import 'package:doctor_app_flutter/models/doctor/clinic_model.dart';
import 'package:doctor_app_flutter/models/doctor/doctor_profile_model.dart';

import '../../models/doctor/user_model.dart';

enum APP_STATUS { LOADING, UNAUTHENTICATED, AUTHENTICATED }

class AuthViewModel extends BaseViewModel {
  List<ClinicModel> doctorsClinicList = [];

  String selectedClinicName;
  bool isLogin = false;
  bool isLoading = true;
  DoctorProfileModel doctorProfile;
  BaseAppClient baseAppClient = BaseAppClient();
  setDoctorProfile(DoctorProfileModel profileModel) {
    doctorProfile = profileModel;
    notifyListeners();
  }

  AuthViewModel() {
    getUserAuthentication();
  }

  getUserAuthentication() async {
    Map profile = await sharedPref.getObj(DOCTOR_PROFILE);
    if (profile != null) {
      doctorProfile = new DoctorProfileModel.fromJson(profile);
      isLoading = false;
      isLogin = true;
    } else {
      isLoading = false;
      isLogin = false;
    }
    notifyListeners();
  }

  APP_STATUS get stutas {
    if (isLoading) {
      return APP_STATUS.LOADING;
    } else {
      if (this.isLogin) {
        return APP_STATUS.AUTHENTICATED;
      } else {
        return APP_STATUS.UNAUTHENTICATED;
      }
    }
  }

  Future<dynamic> login(UserModel userInfo) async {
    try {
      dynamic localRes;

      await baseAppClient.post(LOGIN_URL,
          onSuccess: (dynamic response, int statusCode) {
        localRes = response;
      }, onFailure: (String error, int statusCode) {
        throw error;
      }, body: userInfo.toJson());

      return Future.value(localRes);
    } catch (error) {
      print(error);
      throw error;
    }
  }

  Future insertDeviceImei(request) async {
    var loggedIn = await sharedPref.getObj(LOGGED_IN_USER);
    var user = await sharedPref.getObj(LAST_LOGIN_USER);
    if (user != null) {
      user = GetIMEIDetailsModel.fromJson(user);
    }
    request['IMEI'] = DEVICE_TOKEN;
    request['LogInTypeID'] = await sharedPref.getInt(OTP_TYPE);
    request['BioMetricEnabled'] = true;
    request['MobileNo'] =
        loggedIn != null ? loggedIn['MobileNumber'] : user.mobile;
    InsertIMEIDetailsModel nRequest = InsertIMEIDetailsModel.fromJson(request);
    nRequest.genderDescription = request['Gender_Description'];
    nRequest.genderDescriptionN = request['Gender_DescriptionN'];
    nRequest.genderDescriptionN = request['Gender_DescriptionN'];
    nRequest.titleDescription = request['Title_Description'];
    nRequest.titleDescriptionN = request['Title_DescriptionN'];
    nRequest.projectID = await sharedPref.getInt(PROJECT_ID);
    nRequest.doctorID = loggedIn != null
        ? loggedIn['List_MemberInformation'][0]['MemberID']
        : user.doctorID;
    nRequest.outSA = loggedIn != null ? loggedIn['PatientOutSA'] : user.outSA;
    nRequest.vidaAuthTokenID = await sharedPref.getString(VIDA_AUTH_TOKEN_ID);
    nRequest.vidaRefreshTokenID =
        await sharedPref.getString(VIDA_REFRESH_TOKEN_ID);
    nRequest.password = await sharedPref.getString(PASSWORD);
    try {
      var localRes;
      await baseAppClient.post(INSERT_DEVICE_IMEI,
          onSuccess: (dynamic response, int statusCode) {
        localRes = response;
      }, onFailure: (String error, int statusCode) {}, body: nRequest.toJson());
      return Future.value(localRes);
    } catch (error) {
      throw error;
    }
  }

  Future sendActivationCodeByOtpNotificationType(activationCodeModel) async {
    try {
      var localRes;
      await baseAppClient.post(SEND_ACTIVATION_CODE_BY_OTP_NOTIFICATION_TYPE,
          onSuccess: (dynamic response, int statusCode) {
        localRes = response;
      }, onFailure: (String error, int statusCode) {
        throw error;
      }, body: activationCodeModel);
      return Future.value(localRes);
    } catch (error) {
      print(error);
      throw error;
    }
  }

  Future sendActivationCodeForDoctorApp(
      ActivationCodeModel activationCodeModel) async {
    try {
      var localRes;
      await baseAppClient.post(SEND_ACTIVATION_CODE_FOR_DOCTOR_APP,
          onSuccess: (dynamic response, int statusCode) {
        localRes = response;
      }, onFailure: (String error, int statusCode) {
        throw error;
      }, body: activationCodeModel.toJson());
      return Future.value(localRes);
    } catch (error) {
      print(error);
      throw error;
    }
  }

  Future<dynamic> memberCheckActivationCodeNew(activationCodeModel) async {
    try {
      dynamic localRes;
      await baseAppClient.post(MEMBER_CHECK_ACTIVATION_CODE_NEW,
          onSuccess: (dynamic response, int statusCode) {
        localRes = response;
        selectedClinicName =
            ClinicModel.fromJson(response['List_DoctorsClinic'][0]).clinicName;

        response['List_DoctorsClinic'].forEach((v) {
          doctorsClinicList.add(new ClinicModel.fromJson(v));
        });
      }, onFailure: (String error, int statusCode) {
        throw error;
      }, body: activationCodeModel);
      return Future.value(localRes);
    } catch (error) {
      print(error);
      throw error;
    }
  }

  Future<dynamic> checkActivationCodeForDoctorApp(
      CheckActivationCodeRequestModel checkActivationCodeRequestModel) async {
    try {
      dynamic localRes;
      await baseAppClient.post(CHECK_ACTIVATION_CODE_FOR_DOCTOR_APP,
          onSuccess: (dynamic response, int statusCode) {
        localRes = response;
        selectedClinicName =
            ClinicModel.fromJson(response['List_DoctorsClinic'][0]).clinicName;

        response['List_DoctorsClinic'].forEach((v) {
          doctorsClinicList.add(new ClinicModel.fromJson(v));
        });
      }, onFailure: (String error, int statusCode) {
        throw error;
      }, body: checkActivationCodeRequestModel.toJson());
      return Future.value(localRes);
    } catch (error) {
      print(error);
      throw error;
    }
  }

  Future<dynamic> getDocProfiles(docInfo,
      {bool allowChangeProfile = true}) async {
    try {
      dynamic localRes;
      await baseAppClient.post(GET_DOC_PROFILES,
          onSuccess: (dynamic response, int statusCode) {
        localRes = response;
        if (allowChangeProfile) {
          doctorProfile =
              DoctorProfileModel.fromJson(response['DoctorProfileList'][0]);
          selectedClinicName =
              response['DoctorProfileList'][0]['ClinicDescription'];
        }
      }, onFailure: (String error, int statusCode) {
        throw error;
      }, body: docInfo);
      notifyListeners();
      return Future.value(localRes);
    } catch (error) {
      print(error);
      throw error;
    }
  }

  Future sendActivationCodeVerificationScreen(
      ActivationCodeModel2 activationCodeModel) async {
    try {
      var localRes;
      await baseAppClient.post(SEND_ACTIVATION_CODE_FOR_VERIFICATION_SCREEN,
          onSuccess: (dynamic response, int statusCode) {
        localRes = response;
      }, onFailure: (String error, int statusCode) {
        throw error;
      }, body: activationCodeModel.toJson());
      return Future.value(localRes);
    } catch (error) {
      print(error);
      throw error;
    }
  }
}
