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';
import 'package:flutter/material.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 _referralNumber;

  String get activationCode => _activationCode;

  String get referralNumber => _referralNumber;

  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<Map> getProjectsList(context) async {
    Map<String, dynamic> request;
    request = {};

    dynamic localRes;

    await baseAppClient.post(GET_PROJECTS_LIST, onSuccess: (response, statusCode) async {
      localRes = response;
    }, onFailure: (String error, int statusCode) {
      throw error;
    }, body: request);
    return Future.value(localRes);
  }

  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
          _referralNumber = response['ReferralNumber'].toString();
    }, onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error;
    }, body: createEReferralRequestModel.toJson());
  }

  Future getEReferrals(
      SearchEReferralRequestModel searchEReferralRequestModel) async {
    _allReferral.clear();
    hasError = false;

    // TODO return this code when the fix the server
    await baseAppClient.post(GET_E_REFERRALS,
        onSuccess: (dynamic response, int statusCode) {
      _allReferral.clear();
      response['List_EReferrals'].forEach((referral) {
        _allReferral
            .add(SearchEReferralResponseModel.fromJson(referral));
      });
    }, onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error;
    }, body: searchEReferralRequestModel.toJson());
  }
}
