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 '../../../core/enums/request_constants.dart';
import '../../../core/utils/SessionCache.dart';
import '../../../core/utils/logs_utils.dart';
import '../../../custom_widgets/custom_dialog.dart';
import '../../../custom_widgets/custom_toasty.dart';
import '../../../models/CreateTransactionPinOTPRequest.dart';
import '../../../models/CreateTransactionPinRequest.dart';
import '../../../routes/app_pages.dart';


class CreateTransactionPinOtpController extends GetxController {
  TextEditingController pinPutController = TextEditingController();
  final AppRepositories repository = Get.find<AppRepositories>();
  late String newTransactionPin;
  late String confirmTransactionPin;
  CreateTransactionPinOTPRequest createTransactionPinOTPRequest = CreateTransactionPinOTPRequest();
  RxBool isResend = false.obs;

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

  @override
  void onReady() {
    super.onReady();
    var arguments = Get.arguments;
    newTransactionPin = arguments['transactionPin'];
    confirmTransactionPin = arguments['confirmTransactionPin'];
    isSomething = arguments['isSomething'];
    createTransactionPinOTPRequest = arguments[AppConstants.CREATE_TRANSACTION_PIN_OTP];

    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.createTransactionPinOTP(createTransactionPinOTPRequest);

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

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

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