import 'package:doctor_app_flutter/config/config.dart';
import 'package:doctor_app_flutter/core/model/auth/activation_Code_req_model.dart';
import 'package:doctor_app_flutter/core/model/auth/activation_code_for_verification_screen_model.dart';
import 'package:doctor_app_flutter/core/model/auth/check_activation_code_for_doctor_app_response_model.dart';
import 'package:doctor_app_flutter/core/model/auth/check_activation_code_request_model.dart';
import 'package:doctor_app_flutter/core/model/auth/imei_details.dart';
import 'package:doctor_app_flutter/core/model/auth/insert_imei_model.dart';
import 'package:doctor_app_flutter/core/model/auth/new_login_information_response_model.dart';
import 'package:doctor_app_flutter/core/model/auth/send_activation_code_for_doctor_app_response_model.dart';
import 'package:doctor_app_flutter/core/model/doctor/doctor_profile_model.dart';
import 'package:doctor_app_flutter/core/model/doctor/profile_req_Model.dart';
import 'package:doctor_app_flutter/core/model/doctor/user_model.dart';
import 'package:doctor_app_flutter/core/service/base/base_service.dart';

class AuthenticationService extends BaseService {
  List<GetIMEIDetailsModel> _imeiDetails = [];

  List<GetIMEIDetailsModel> get dashboardItemsList => _imeiDetails;
  NewLoginInformationModel _loginInfo = NewLoginInformationModel();
  String  _errorCode ='';
  String get errorCode => _errorCode;
  NewLoginInformationModel get loginInfo => _loginInfo;
  SendActivationCodeForDoctorAppResponseModel _activationCodeVerificationScreenRes = SendActivationCodeForDoctorAppResponseModel();

  SendActivationCodeForDoctorAppResponseModel get activationCodeVerificationScreenRes => _activationCodeVerificationScreenRes;

  SendActivationCodeForDoctorAppResponseModel _activationCodeForDoctorAppRes = SendActivationCodeForDoctorAppResponseModel();

  SendActivationCodeForDoctorAppResponseModel get activationCodeForDoctorAppRes => _activationCodeForDoctorAppRes;
  CheckActivationCodeForDoctorAppResponseModel _checkActivationCodeForDoctorAppRes = CheckActivationCodeForDoctorAppResponseModel();

  CheckActivationCodeForDoctorAppResponseModel get checkActivationCodeForDoctorAppRes => _checkActivationCodeForDoctorAppRes;
  Map<String, dynamic> _insertDeviceImeiRes = {};

  List<DoctorProfileModel> _doctorProfilesList = [];

  List<DoctorProfileModel> get doctorProfilesList => _doctorProfilesList;

  Future selectDeviceImei(imei) async {
    try {
      await baseAppClient.post(SELECT_DEVICE_IMEI, onSuccess: (dynamic response, int statusCode) {
        _imeiDetails = [];
        response['List_DoctorDeviceDetails']?.forEach((v) {
          _imeiDetails.add(GetIMEIDetailsModel.fromJson(v));
        });
      }, onFailure: (String error, int statusCode) {
        hasError = true;
        super.error = error;
      }, body: {"IMEI": imei, }, isCustomRequest: true);
    } catch (error) {
      hasError = true;
      super.error = error.toString();
    }
  }

  Future login(UserModel userInfo) async {
    hasError = false;
    _loginInfo = NewLoginInformationModel();
    try {
      await baseAppClient.post(LOGIN_URL, onSuccess: (dynamic response, int statusCode) {
        _loginInfo = NewLoginInformationModel.fromJson(response);
      }, onFailure: (String error, int statusCode) {
        hasError = true;
        super.error = error;
      }, body: userInfo.toJson(), isAllowAny: true);
    } catch (error) {
      hasError = true;
      super.error = error.toString();
    }
  }

  Future sendActivationCodeVerificationScreen(ActivationCodeForVerificationScreenModel activationCodeModel) async {
    hasError = false;
    _activationCodeVerificationScreenRes = SendActivationCodeForDoctorAppResponseModel();
    try {
      await baseAppClient.post(SEND_ACTIVATION_CODE_FOR_VERIFICATION_SCREEN, onSuccess: (dynamic response, int statusCode) {
        _activationCodeVerificationScreenRes = SendActivationCodeForDoctorAppResponseModel.fromJson(response);
      }, onFailure: (String error, int statusCode) {
        hasError = true;
        super.error = error;
      }, body: activationCodeModel.toJson());
    } catch (error) {
      hasError = true;
      super.error = error.toString();
    }
  }

  Future sendActivationCodeForDoctorApp(ActivationCodeModel activationCodeModel) async {
    hasError = false;
    _activationCodeForDoctorAppRes = SendActivationCodeForDoctorAppResponseModel();
    try {
      await baseAppClient.post(SEND_ACTIVATION_CODE_FOR_DOCTOR_APP, onSuccess: (dynamic response, int statusCode) {
        _activationCodeForDoctorAppRes = SendActivationCodeForDoctorAppResponseModel.fromJson(response);
      }, onFailure: (String error, int statusCode) {
        hasError = true;
        super.error = error;
      }, body: activationCodeModel.toJson());
    } catch (error) {
      hasError = true;
      super.error = error.toString();
    }
  }

  Future checkActivationCodeForDoctorApp(CheckActivationCodeRequestModel checkActivationCodeRequestModel) async {
    hasError = false;
    _checkActivationCodeForDoctorAppRes = CheckActivationCodeForDoctorAppResponseModel();
    try {
      await baseAppClient.post(CHECK_ACTIVATION_CODE_FOR_DOCTOR_APP, onSuccess: (dynamic response, int statusCode) {
        if(response['ErrorCode'] =='699'){
          _errorCode = response['ErrorCode'];
          hasError = true;
          super.error = response['ErrorEndUserMessage'];
        }else {
          _errorCode ='';
          _checkActivationCodeForDoctorAppRes =
              CheckActivationCodeForDoctorAppResponseModel.fromJson(response);
        }
      }, onFailure: (String error, int statusCode) {
        _errorCode ='';
        hasError = true;
        super.error = error;
      }, body: checkActivationCodeRequestModel.toJson());
    } catch (error) {
      _errorCode ='';
      hasError = true;
      super.error = error.toString();
    }
  }

  Future insertDeviceImei(InsertIMEIDetailsModel insertIMEIDetailsModel) async {
    hasError = false;
    _insertDeviceImeiRes = {};
    try {
      await baseAppClient.post(INSERT_DEVICE_IMEI, onSuccess: (dynamic response, int statusCode) {
        _insertDeviceImeiRes = response;
      }, onFailure: (String error, int statusCode) {
        hasError = true;
        super.error = error;
      }, body: insertIMEIDetailsModel.toJson());
    } catch (error) {
      hasError = true;
      super.error = error.toString();
    }
  }

  Future getDoctorProfileBasedOnClinic(ProfileReqModel profileReqModel) async {
    hasError = false;
    try {
      await baseAppClient.post(GET_DOC_PROFILES, onSuccess: (dynamic response, int statusCode) {
        _doctorProfilesList.clear();
        response['DoctorProfileList'].forEach((v) {
          _doctorProfilesList.add(DoctorProfileModel.fromJson(v));
        });
      }, onFailure: (String error, int statusCode) {
        hasError = true;
        super.error = error;
      }, body: profileReqModel.toJson());
    } catch (error) {
      hasError = true;
      super.error = error.toString();
    }
  }
}
