import 'package:flutter/cupertino.dart';
import 'package:fluttertoast/fluttertoast.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 '../../../core/enums/request_constants.dart';
import '../../../core/utils/SessionCache.dart';
import '../../../custom_widgets/custom_dialog.dart';
import '../../../custom_widgets/custom_toasty.dart';
import '../../../models/GLACCAddMoneyRequestModel.dart';
import '../../../models/evaluated_cuency_model.dart';
import '../../../models/resend_otp_usend.dart';
import '../../../routes/app_pages.dart';
import '../../general_ledger/controllers/general_ledger_controller.dart';


class AddMoneyOTPController extends GetxController {
  final AppRepositories repository = Get.find<AppRepositories>();

  TextEditingController pinPutController = TextEditingController();

  RxBool isResend = false.obs;

  CountdownController countdownController =
      CountdownController(autoStart: true);

  GLACCAddMoneyRequestModel transactionSubmitRequestModel =
      GLACCAddMoneyRequestModel.empty();
  Rx<EvaluatedCurrencyModel> evaluatedCurrencyModel =
      EvaluatedCurrencyModel.empty().obs;

  @override
  void onInit() {
    super.onInit();
    var arguments = Get.arguments;
    transactionSubmitRequestModel = (arguments != null
        ? arguments[AppConstants.SEND_TRANS_STEP1_DATA] ??
            GLACCAddMoneyRequestModel.empty()
        : GLACCAddMoneyRequestModel.empty());
    evaluatedCurrencyModel.value = (arguments != null
        ? arguments[AppConstants.EXCHANGE_RATE] ??
            EvaluatedCurrencyModel.empty()
        : EvaluatedCurrencyModel.empty());
  }

  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;
    transactionSubmitRequestModel.plcLocacode = "0010";
    transactionSubmitRequestModel.pinType = "TRAN";
    transactionSubmitRequestModel.obpPincode = pinPutController.text;
    sendTransaction();
  }

  Future<void> sendTransaction() async {
    ServerResponse response =
        await repository.sendAddMoneyTransaction(transactionSubmitRequestModel);
    if (response.isError) {
      Toasty.error(response.errorMsg);
      return;
    }
    Get.find<GeneralLedgerController>().clearAll();

    

    CustomDialog.showSuccessDialog(
        title: "Congratulations!",
        description: "Thank you so much, your payment has been added.",
        onTapPositive: () {
          Get.offAllNamed(Routes.DASHBOARD_SCREEN);
        });
  }

  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,
      transPincode:"123456",
      pctCstycode: SessionCache.instance.userInfo.pctCstycode,
      pinType: 'TRAN',
    ));

    isResend.value = false;
    countdownController.restart();
    Fluttertoast.showToast(msg: "OTP successfully send to your email address");
  }

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