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/config/server_response.dart';
import '../../../core/constants/app_contants.dart';
import '../../../core/data/repositories/app_repositories.dart';
import '../../../core/utils/logs_utils.dart';
import '../../../custom_widgets/custom_dialog.dart';
import '../../../custom_widgets/custom_toasty.dart';
import '../../../models/ChangeTransactionPinRequest.dart';
import '../../../models/resen_ot_chanange_trans_pin.dart';
import '../../../routes/app_pages.dart';

class ChangeTransactionPinOtpController extends GetxController {
  TextEditingController pinPutController = TextEditingController();
  final AppRepositories repository = Get.find<AppRepositories>();
  late String oldTransactionPin;
  late String confirmTransactionPin;
  late String newTransactionPin;
  ChangeTransactionPinRequest changeTransactionPinOTPRequest = ChangeTransactionPinRequest();
  RxBool isResend = false.obs;

  CountdownController countdownController = CountdownController(autoStart: true);
  late bool isSomething;
  late final request;

  @override
  void onReady() {
    super.onReady();
    var arguments = Get.arguments;
    oldTransactionPin = arguments['oldTransactionPin'];
    newTransactionPin = arguments['newTransactionPin'];
    confirmTransactionPin = arguments['confirmTransactionPin'];
    changeTransactionPinOTPRequest = arguments[AppConstants.CHANGE_TRANSACTION_PIN_OTP];

    dp("oldTransactionPin>>>>", oldTransactionPin);
    dp("newTransactionPin>>>>", newTransactionPin);
    dp("confirmTransactionPin>>>>", confirmTransactionPin);
  }

  void validateAndSendRequest() async {
    if (pinPutController.text.isEmpty) {
      Toasty.error("Please input and then proceed");
      return;
    }
    //
    // CreateTransactionPinRequest request = CreateTransactionPinRequest(
    //   channelCode: RequestConstants.channelCode,
    //   pctCstycode: RequestConstants.pctCstycode,
    //   porOrgacode: RequestConstants.porOrgacode,
    //   cmpCustcode: SessionCache.instance.userInfo.cmpCustcode,
    //   email: SessionCache.instance.userInfo.cmpUserId,
    //   obpPincode: pinPutController.text,
    //   pinType: AppConstants.PIN_TYPE_Transaction,
    // );
    //
    // ServerResponse response = await repository.createTransactionPin(request);
    // if (response.isError) {
    //   Toasty.error(response.errorMsg);
    //   return;
    // }

    clearController();
    CustomDialog.showSuccessDialog(
      // title: "Congratulations!",
      description: "Your Transaction pin successfully ! An send email and message have been sent to you.",
      onTapPositive: () {
        Get.offAllNamed(Routes.DASHBOARD_SCREEN);
      },
    );
  }

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

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

  void clearController() {
    pinPutController.clear();
  }

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