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


import '../../../core/data/repositories/app_repositories.dart';
import '../../../core/enums/request_constants.dart';
import '../../../core/utils/SessionCache.dart';
import '../../../custom_widgets/custom_dialog.dart';
import '../../../custom_widgets/custom_toasty.dart';
import '../../../models/TransactionSubmitRequestModel.dart';
import '../../../models/resend_otp_usend.dart';
import '../../../routes/app_pages.dart';

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

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

  RxBool isResend = false.obs;

  CountdownController countdownController = CountdownController(autoStart: true);

  @override
  Future<void> onReady() async {
    super.onReady();
    // var arguments = Get.arguments;
    // transactionSubmitRequestModel = (arguments != null ? arguments[AppConstants.SEND_TRANS_STEP1_DATA] ?? TransactionSubmitRequestModel.empty() : TransactionSubmitRequestModel.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;

    CustomDialog.showSuccessDialog(
        description: "You have successfully added the Beneficiary.",
        onTapPositive: () {
          Get.offAndToNamed(Routes.BENEFICIARY_MANAGEMENT);
        },
        title: "Beneficiary Added");
  }

  void resenOtp() async {
    //

    if (!isResend.value) {
      Fluttertoast.showToast(msg: "Please wait for 1 minute");
      return;
    }

    var responce = await repository.resendOtpUsend(ResendOtpUsendModel(
      channelCode: RequestConstants.channelCode,
      porOrgacode: RequestConstants.porOrgacode,
      email: SessionCache.instance.userInfo.cmpUserId,
      cmpCustcode: SessionCache.instance.userInfo.cmpCustcode,
      isOtpRequired: false,
      pctCstycode: SessionCache.instance.userInfo.pctCstycode,
      pinType: 'C_TRANPIN',
    ));

    if (responce.isError) {
      Fluttertoast.showToast(msg: responce.errorMsg);
      return;
    }

    isResend.value = false;

    countdownController.restart();

    Fluttertoast.showToast(msg: "OTP successfully send to your email address");
  }

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