import 'package:doctor_app_flutter/config/config.dart';
import 'package:doctor_app_flutter/core/enum/view_state.dart';
import 'package:doctor_app_flutter/core/model/PatientRegistration/CheckActivationCodeModel.dart';
import 'package:doctor_app_flutter/core/model/PatientRegistration/CheckPatientForRegistrationModel.dart';
import 'package:doctor_app_flutter/core/model/PatientRegistration/GetPatientInfoRequestModel.dart';
import 'package:doctor_app_flutter/core/model/PatientRegistration/GetPatientInfoResponseModel.dart';
import 'package:doctor_app_flutter/core/model/PatientRegistration/PatientRegistrationModel.dart';
import 'package:doctor_app_flutter/core/model/PatientRegistration/SendActivationCodebyOTPNotificationTypeForRegistrationModel.dart';
import 'package:doctor_app_flutter/core/service/PatientRegistrationService.dart';
import 'package:doctor_app_flutter/core/viewModel/base_view_model.dart';

import '../../locator.dart';

class PatientRegistrationViewModel extends BaseViewModel {
  PatientRegistrationService _patientRegistrationService = locator<PatientRegistrationService>();

  GetPatientInfoResponseModel get getPatientInfoResponseModel => _patientRegistrationService.getPatientInfoResponseModel!;

  CheckPatientForRegistrationModel? checkPatientForRegistrationModel;

  Future checkPatientForRegistration(CheckPatientForRegistrationModel registrationModel) async {
    checkPatientForRegistrationModel = registrationModel;

    checkPatientForRegistrationModel = CheckPatientForRegistrationModel.fromJson(registrationModel.toJson());
    setState(ViewState.BusyLocal);
    await _patientRegistrationService.checkPatientForRegistration(registrationModel);
    if (_patientRegistrationService.hasError) {
      error = _patientRegistrationService.error;
      setState(ViewState.ErrorLocal);
    } else
      setState(ViewState.Idle);
  }

  Future getPatientInfo(GetPatientInfoRequestModel getPatientInfoRequestModel) async {
    setState(ViewState.BusyLocal);

    /// TODO Elham*  return call service when it working
    _patientRegistrationService.getPatientInfoResponseModel = GetPatientInfoResponseModel.fromJson({
      "Date": null,
      "LanguageID": 0,
      "ServiceName": 0,
      "Time": null,
      "AndroidLink": null,
      "AuthenticationTokenID": null,
      "Data": null,
      "Dataw": false,
      "DietType": 0,
      "ErrorCode": null,
      "ErrorEndUserMessage": null,
      "ErrorEndUserMessageN": null,
      "ErrorMessage": null,
      "ErrorType": 0,
      "FoodCategory": 0,
      "IOSLink": null,
      "IsAuthenticated": false,
      "MealOrderStatus": 0,
      "MealType": 0,
      "MessageStatus": 1,
      "NumberOfResultRecords": 0,
      "PatientBlodType": null,
      "SuccessMsg": null,
      "SuccessMsgN": null,
      "VidaUpdatedResponse": null,
      "AccessTokenObject": null,
      "Age": 33,
      "ClientIdentifierId": null,
      "CreatedBy": 0,
      "DateOfBirth": "07/31/1988",
      "FirstNameAr": "سفيان",
      "FirstNameEn": "SUFIAN",
      "Gender": "M",
      "GenderAr": null,
      "GenderEn": null,
      "HealthId": "30000018540264",
      "IdNumber": "1062938285",
      "IdType": "NationalId",
      "IsHijri": false,
      "IsInstertedOrUpdated": 0,
      "IsNull": 0,
      "IsPatientExistNHIC": 0,
      "IsRecordLockedByCurrentUser": false,
      "LastNameAr": "عثمان",
      "LastNameEn": "OTHMAN",
      "List_ActiveAccessToken": null,
      "MaritalStatus": "غير معروف",
      "MaritalStatusCode": "U",
      "NationalDateOfBirth": "18/12/1408",
      "Nationality": "السعودية",
      "NationalityCode": "SAU",
      "Occupation": "طالب",
      "PCDTransactionDataResultList": null,
      "PCD_GetVidaPatientForManualVerificationList": null,
      "PCD_NHIC_HMG_PatientDetailsMatchCalulationList": null,
      "PCD_ReturnValue": 0,
      "PatientStatus": "-",
      "PlaceofBirth": "فينا",
      "PractitionerStatusCode": null,
      "PractitionerStatusDescAr": null,
      "PractitionerStatusDescEn": null,
      "RowCount": 0,
      "SecondNameAr": "عبدالهادي",
      "SecondNameEn": "ABDULHADI",
      "ThirdNameAr": "احمد",
      "ThirdNameEn": "A",
      "YakeenVidaPatientDataStatisticsByPatientIdList": null,
      "YakeenVidaPatientDataStatisticsList": null,
      "YakeenVidaPatientDataStatisticsPrefferedList": null,
      "accessToken": null,
      "categoryCode": 0,
      "categoryNameAr": null,
      "categoryNameEn": null,
      "constraintCode": 0,
      "constraintNameAr": null,
      "constraintNameEn": null,
      "content": null,
      "errorList": null,
      "licenseExpiryDate": null,
      "licenseIssuedDate": null,
      "licenseStatusCode": null,
      "licenseStatusDescAr": null,
      "licenseStatusDescEn": null,
      "organizations": null,
      "registrationNumber": null,
      "specialtyCode": 0,
      "specialtyNameAr": null,
      "specialtyNameEn": null
    });
    // await _patientRegistrationService.
    //     getPatientInfo(getPatientInfoRequestModel);
    // if (_patientRegistrationService.hasError) {
    //   error = _patientRegistrationService.error;
    //   setState(ViewState.ErrorLocal);
    // } else
    setState(ViewState.Idle);
  }

  Future sendActivationCodeByOTPNotificationType({SendActivationCodeByOTPNotificationTypeForRegistrationModel? registrationModel, int? otpType, PatientRegistrationViewModel? user}) async {
    setState(ViewState.BusyLocal);
    print(checkPatientForRegistrationModel);
    print(checkPatientForRegistrationModel);

    await _patientRegistrationService.sendActivationCodeByOTPNotificationType(otpType: otpType, model: this, checkPatientForRegistrationModel: checkPatientForRegistrationModel);
    if (_patientRegistrationService.hasError) {
      error = _patientRegistrationService.error;
      setState(ViewState.ErrorLocal);
    } else
      setState(ViewState.Idle);
  }

  Future checkActivationCode(String code) async {
    CheckActivationCodeModel model = CheckActivationCodeModel(
      activationCode: code,
      patientIdentificationID: checkPatientForRegistrationModel!.patientIdentificationID,
      patientMobileNumber: checkPatientForRegistrationModel!.patientMobileNumber,
      zipCode: checkPatientForRegistrationModel!.zipCode,
      patientOutSA: 0,
      healthId: getPatientInfoResponseModel.healthId,
      dOB: checkPatientForRegistrationModel!.dOB,
      isRegister: checkPatientForRegistrationModel!.isRegister,
      isHijri: checkPatientForRegistrationModel!.isHijri,
      sessionID: null,
      generalid: GENERAL_ID,
      forRegisteration: true,
      isDentalAllowedBackend: false,
      projectOutSA: 0,
      searchType: 1,
      versionID: 7.1,
      channel: 3,
      // TODO Elham* loginType
      loginType: 4,
      logInTokenID: _patientRegistrationService.logInTokenID,
      nationalID: checkPatientForRegistrationModel!.patientIdentificationID,
      patientID: 0,
      mobileNo: checkPatientForRegistrationModel!.patientMobileNumber.toString(),
    );
    setState(ViewState.BusyLocal);
    await _patientRegistrationService.checkActivationCode(model);
    if (_patientRegistrationService.hasError) {
      error = _patientRegistrationService.error;
      setState(ViewState.ErrorLocal);
    } else
      setState(ViewState.Idle);
  }

  Future registrationPatient(PatientRegistrationModel registrationModel) async {
    setState(ViewState.BusyLocal);
    await _patientRegistrationService.registrationPatient(registrationModel);
    if (_patientRegistrationService.hasError) {
      error = _patientRegistrationService.error;
      setState(ViewState.ErrorLocal);
    } else
      setState(ViewState.Idle);
  }
}
