import 'dart:ffi';

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:uco_mobile_poc/app/core/config/server_response.dart';
import 'package:uco_mobile_poc/app/core/constants/app_contants.dart';
import 'package:uco_mobile_poc/app/core/data/repositories/app_repositories.dart';
import 'package:uco_mobile_poc/app/core/utils/logs_utils.dart';
import 'package:uco_mobile_poc/app/custom_widgets/custom_dialog.dart';
import 'package:uco_mobile_poc/app/custom_widgets/custom_toasty.dart';
import 'package:uco_mobile_poc/app/models/TransactionSubmitRequestModel.dart';
import 'package:uco_mobile_poc/app/models/TransactionSubmitResponseModel.dart';

import '../../../routes/app_pages.dart';
import '../../dashboard/controllers/dashboard_screen_controller.dart';

class RecipientDetailsController extends GetxController {
  BuildContext context = Get.context as BuildContext;
  final AppRepositories repository = Get.find<AppRepositories>();

  TextEditingController pinPutController = TextEditingController();
  Rx<TransactionSubmitRequestModel> transactionSubmitRequestModel =
      TransactionSubmitRequestModel
          .empty()
          .obs;
  String exchangeRate = "";
  String endingBalance = "";

  @override
  Future<void> onReady() async {
    super.onReady();
    var arguments = Get.arguments;
    transactionSubmitRequestModel.value = (arguments != null
        ? arguments[AppConstants.SEND_TRANS_STEP1_DATA] ??
        TransactionSubmitRequestModel.empty()
        : TransactionSubmitRequestModel.empty());
    exchangeRate =
    (arguments != null ? arguments[AppConstants.EXCHANGE_RATE] ?? "" : "");
  }

  Future<void> sendTransaction() async {
    ServerResponse response = await repository
        .sendTransactionStep2(transactionSubmitRequestModel.value);
    if (response.isError) {
      Toasty.error(response.errorMsg);
      return;
    }

    TransactionSubmitResponseModel responseModel =
    TransactionSubmitResponseModel.fromMap(response.response);

    transitionUpdate.value++;

    dp("Transition value is", transitionUpdate.value);

    CustomDialog.showTransactionSuccessDialog(
      context,
      responseModel.mbmBkmsbalance.toString(),
      exchangeRate,
      transactionSubmitRequestModel.value,
    );
    Get.offAllNamed(Routes.DASHBOARD_SCREEN);
  }
}
