import 'dart:convert';

import 'package:diplomaticquarterapp/config/config.dart';
import 'package:diplomaticquarterapp/config/shared_pref_kay.dart';
import 'package:diplomaticquarterapp/core/model/my_balance/patient_advance_balance_amount.dart';
import 'package:diplomaticquarterapp/core/model/my_balance/patient_info.dart';
import 'package:diplomaticquarterapp/core/model/my_balance/patient_info_and_mobile_number.dart';
import 'package:diplomaticquarterapp/core/service/base_service.dart';
import 'package:diplomaticquarterapp/models/FamilyFiles/GetAllSharedRecordByStatusResponse.dart';
import 'package:diplomaticquarterapp/models/FamilyFiles/GetAllSharedRecordsByStatusReq.dart';
import 'package:diplomaticquarterapp/services/family_files/family_files_provider.dart';
import 'package:diplomaticquarterapp/uitl/app_toast.dart';

class MyBalanceService extends BaseService {
  List<PatientAdvanceBalanceAmount> patientAdvanceBalanceAmountList = List();
  double totalAdvanceBalanceAmount;
  List<PatientInfo> patientInfoList = List();
  GetAllSharedRecordsByStatusResponse getAllSharedRecordsByStatusResponse =
      GetAllSharedRecordsByStatusResponse();
  PatientInfoAndMobileNumber patientInfoAndMobileNumber;
  String logInTokenID;
  String verificationCode;

  getPatientAdvanceBalanceAmount() async {
    hasError = false;
    super.error = "";
    await baseAppClient.post(GET_PATIENT_AdVANCE_BALANCE_AMOUNT,
        onSuccess: (response, statusCode) async {
      patientAdvanceBalanceAmountList.clear();
      response['List_PatientAdvanceBalanceAmount'].forEach((item) {
        patientAdvanceBalanceAmountList
            .add(PatientAdvanceBalanceAmount.fromJson(item));
      });
      totalAdvanceBalanceAmount = response['TotalAdvanceBalanceAmount'];
    }, onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error;
    }, body: Map());
  }

  getPatientInfoByPatientID({String id}) async {
    hasError = false;
    super.error = "";
    Map<String, dynamic> body = Map();
    body['SearchPatientID'] = int.parse(id);
    body['isDentalAllowedBackend'] = false;
    await baseAppClient.post(GET_PATIENT_INFO_BY_ID,
        onSuccess: (response, statusCode) async {
      patientInfoList.clear();
      response['GetPatientInfoByPatientIDList'].forEach((item) {
        patientInfoList.add(PatientInfo.fromJson(item));
      });
    }, onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error;
    }, body: body);
  }

  getPatientInfoByPatientIDAndMobileNumber() async {
    hasError = false;
    super.error = "";
    Map<String, dynamic> body = Map();
    body['isDentalAllowedBackend'] = false;
    body['MobileNo'] = user.mobileNumber;
    body['ProjectID'] = user.projectID;

    await baseAppClient.post(GET_PATIENT_INFO_BY_ID_AND_MOBILE_NUMBER,
        onSuccess: (response, statusCode) async {
      response['List_PatientInfo'].forEach((item) {
        patientInfoAndMobileNumber = PatientInfoAndMobileNumber.fromJson(item);
      });
    }, onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error;
      super.error = error;
    }, body: body);
  }

  sendActivationCodeForAdvancePayment({int patientID,int projectID}) async {
    hasError = false;
    super.error = "";
    Map<String, dynamic> body = Map();
    body['PatientID'] = patientID;
    body['ProjectID'] = projectID;
    body['isDentalAllowedBackend'] = false;

    await baseAppClient.post(SEND_ACTIVATION_CODE_FOR_ADVANCE_PAYMENT,
        onSuccess: (response, statusCode) async {
      logInTokenID = response['LogInTokenID'];
      verificationCode = response['VerificationCode'];
      print(verificationCode);
    }, onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error;
      super.error = error;
    }, body: body);
  }

  checkActivationCodeForAdvancePayment({String activationCode}) async {
    hasError = false;
    super.error = "";
    Map<String, dynamic> body = Map();
    body['activationCode'] = activationCode;
    body['PatientMobileNumber'] = 'XXXXXXXXXX';
    body['isDentalAllowedBackend'] = false;
    body['LogInTokenID'] = logInTokenID;

    await baseAppClient.post(CHECK_ACTIVATION_CODE_FOR_ADVANCE_PAYMENT,
        onSuccess: (response, statusCode) async {

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

  getSharedRecordByStatus() async {
    try {
      var request = GetAllSharedRecordsByStatusReq();
      request.status = 0;
      await baseAppClient.post(GET_SHARED_RECORD_BY_STATUS,
          onSuccess: (dynamic response, int statusCode) {
        sharedPref.setObject(FAMILY_FILE, response);
        getAllSharedRecordsByStatusResponse =
            GetAllSharedRecordsByStatusResponse.fromJson(response);
      }, onFailure: (String error, int statusCode) {
        AppToast.showErrorToast(message: error);
        hasError = true;
        super.error = error;
      }, body: request.toJson());
    } catch (error) {
      print(error);
      hasError = true;
      super.error = error;
    }
  }

  getFamilyFiles() async {
    if (await sharedPref.getObject(FAMILY_FILE) != null) {
      getAllSharedRecordsByStatusResponse =
          GetAllSharedRecordsByStatusResponse.fromJson(
              await sharedPref.getObject(FAMILY_FILE));
      return getAllSharedRecordsByStatusResponse;
    } else {
      return getSharedRecordByStatus();
    }
  }
}
