import 'package:diplomaticquarterapp/config/config.dart';
import 'package:diplomaticquarterapp/core/model/AlHabibMedicalService/EReferral/check_activation_code_for_e_referral_request_model.dart';
import 'package:diplomaticquarterapp/core/model/AlHabibMedicalService/EReferral/create_e_referral_request_model.dart';
import 'package:diplomaticquarterapp/core/model/AlHabibMedicalService/EReferral/get_all_cities_response_model.dart';
import 'package:diplomaticquarterapp/core/model/AlHabibMedicalService/EReferral/get_all_relationship_types_response_model.dart';
import 'package:diplomaticquarterapp/core/model/AlHabibMedicalService/EReferral/search_e_referral_request_model.dart';
import 'package:diplomaticquarterapp/core/model/AlHabibMedicalService/EReferral/search_e_referral_response_model.dart';
import 'package:diplomaticquarterapp/core/model/AlHabibMedicalService/EReferral/send_activation_code_for_e_referral_request_model.dart';
import 'package:diplomaticquarterapp/core/service/base_service.dart';

class EReferralService extends BaseService {
  List<GetAllRelationshipTypeResponseModel> _relationTypes = List();
  List<GetAllRelationshipTypeResponseModel> get relationTypes => _relationTypes;
  List<GetAllCitiesResponseModel> _allCities = List();
  List<GetAllCitiesResponseModel> get allCities => _allCities;

  List<SearchEReferralResponseModel> _allReferral = List();
  List<SearchEReferralResponseModel> get allReferral => _allReferral;
  String _activationCode;
  String _logInTokenID;

  String get activationCode => _activationCode;

  bool _isActivationCodeValid = false;

  bool get isActivationCodeValid => _isActivationCodeValid;

  Future getRelationTypes() async {
    await baseAppClient.post(GET_ALL_RELATIONSHIP_TYPES,
        onSuccess: (dynamic response, int statusCode) {
      _relationTypes.clear();
      response['List_EReferralResult'].forEach((relation) {
        _relationTypes
            .add(GetAllRelationshipTypeResponseModel.fromJson(relation));
      });
    }, onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error;
    }, body: {});
  }

  Future getAllCities() async {
    await baseAppClient.post(GET_ALL_CITIES,
        onSuccess: (dynamic response, int statusCode) {
          _allCities.clear();
          response['ListCities'].forEach((city) {
            _allCities
                .add(GetAllCitiesResponseModel.fromJson(city));
          });
        }, onFailure: (String error, int statusCode) {
          hasError = true;
          super.error = error;
        }, body: {});
  }

  Future sendActivationCodeForEReferral(

      SendActivationCodeForEReferralRequestModel
          sendActivationCodeForEReferralRequestModel) async {
    hasError = false;
    await baseAppClient.post(SEND_ACTIVATION_CODE_FOR_E_REFERRAL,
        onSuccess: (dynamic response, int statusCode) {
      _activationCode = response["VerificationCode"];
      _logInTokenID = response["LogInTokenID"];

    }, onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error;
    }, body: sendActivationCodeForEReferralRequestModel.toJson());
  }



  Future checkActivationCodeForEReferral(
      CheckActivationCodeForEReferralResponseModel
          checkActivationCodeForEReferralRequestModel) async {
    checkActivationCodeForEReferralRequestModel.isDentalAllowedBackend = false;
    checkActivationCodeForEReferralRequestModel.logInTokenID= _logInTokenID;
    hasError =false;
    await baseAppClient.post(CHECK_ACTIVATION_CODE_FOR_E_REFERRAL,
        onSuccess: (dynamic response, int statusCode) {
          _isActivationCodeValid = true;
    }, onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error;
    }, body: checkActivationCodeForEReferralRequestModel.toJson());
  }

  Future createEReferral(
  CreateEReferralRequestModel createEReferralRequestModel
      ) async {
        hasError = false;
    await baseAppClient.post(CREATE_E_REFERRAL/*'Services/Patients.svc/REST/CreateEReferral'*/,
        onSuccess: (dynamic response, int statusCode) {
          // TODO Waiting for fix service
       var asd=   ("EEEEEE");

        }, onFailure: (String error, int statusCode) {
          hasError = true;
          super.error = error;
        }, body: createEReferralRequestModel.toJson());
  }

  Future getEReferrals(
      SearchEReferralRequestModel searchEReferralRequestModel
      ) async {
    hasError = false;
    // TODO return this code when the fix the server
    // await baseAppClient.post(GET_E_REFERRALS,
    //     onSuccess: (dynamic response, int statusCode) {
    //       print("EEEEEE");
    //       // TODO Waiting for fix service
    //       // ToDo change this one when you have data
    //       _allReferral.clear();
    //       // response['ListCities'].forEach((city) {
    //       //   _allReferral
    //       //       .add(SearchEReferralResponseModel.fromJson(city));
    //       // });
    //
    //     }, onFailure: (String error, int statusCode) {
    //       hasError = true;
    //       super.error = error;
    //     }, body: searchEReferralRequestModel.toJson());
  }
}
