import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:timer_count_down/timer_controller.dart';

import '../../../core/config/server_response.dart';
import '../../../core/constants/app_contants.dart';
import '../../../core/data/repositories/app_repositories.dart';
import '../../../custom_widgets/custom_toasty.dart';
import '../../../models/BeneficiaryAccountModel.dart';
import '../../../models/DepositAccountResponse.dart';
import '../../../models/TransactionPinRequestModel.dart';
import '../../../models/TransactionSubmitRequestModel.dart';
import '../../../routes/app_pages.dart';


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

  TextEditingController pinPutController = TextEditingController();
  TransactionSubmitRequestModel transactionSubmitRequestModel = TransactionSubmitRequestModel.empty();
  TransactionPinRequestModel internalTransPinRequestModel = TransactionPinRequestModel.empty();
  String exchangeRate = "";

  RxBool isResend = false.obs;

  CountdownController countdownController = CountdownController(autoStart: true);

  late DepositAccount fromAccount;
  late DepositAccount toAccount;
  late List<BeneficiaryAccountModel> beneficiaryList;

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

  Future<void> validatePinAndNavigate() async {
    if (pinPutController.text.isEmpty) {
      Toasty.error("Please input and then proceed");
      return;
    }

    if (pinPutController.text.length != 6) {
      Toasty.error("Please enter a valid OTP");
      return;
    }

    transactionSubmitRequestModel.pinType = "TRAN";

    transactionSubmitRequestModel.obpPincode = pinPutController.text;

    Get.toNamed(Routes.RECIPIENT_DETAILS,
        arguments: {
      AppConstants.SEND_TRANS_STEP1_DATA: transactionSubmitRequestModel,
      AppConstants.EXCHANGE_RATE: exchangeRate,
    }
    );
  }

  Future<void> sendBeneficiaryTransaction({navigate = true}) async {
    ServerResponse response = await repository.sendTransactionStep1(internalTransPinRequestModel);

    if (response.isError) {
      Toasty.error(response.errorMsg);
      return;
    }
    Toasty.success("OTP send successfully");
  }

  void updateResend(bool send) {
    isResend.value = send;
  }
}
