import 'package:flutter/material.dart';
import 'package:get/get.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_toasty.dart';
import '../../../models/CreateTransactionPinOTPRequest.dart';
import '../../../routes/app_pages.dart';


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

  TextEditingController newTransactionPinController = TextEditingController();
  TextEditingController confirmTransactionPinController = TextEditingController();

  void validateAndSendRequest() async {
    if (newTransactionPinController.text.isEmpty) {
      Toasty.error("Please input and then proceed");
      return;
    }
    if (confirmTransactionPinController.text.isEmpty) {
      Toasty.error("Please input and then proceed");
      return;
    }
    if (newTransactionPinController.text != confirmTransactionPinController.text) {
      Toasty.error("Transaction Pin did not match");
      return;
    }

    CreateTransactionPinOTPRequest request = CreateTransactionPinOTPRequest(
      newTransPincode: newTransactionPinController.text,
      pinType: AppConstants.PIN_TYPE_Transaction,
      channelCode: RequestConstants.channelCode,
      porOrgacode: RequestConstants.porOrgacode,
      pctCstycode: RequestConstants.pctCstycode,
      cmpCustcode: SessionCache.instance.userInfo.cmpCustcode,
      isOtpRequire: false,
    );

    ServerResponse response = await repository.createTransactionPinOTP(request);
    if (response.isError) {
      Toasty.error(response.errorMsg);
      return;
    }

    Get.toNamed(
      Routes.CREATE_TRANSACTION_PIN_OTP,
      arguments: {
        'transactionPin': newTransactionPinController.text,
        'confirmTransactionPin': confirmTransactionPinController.text,
        'isSomething': false,
        AppConstants.CREATE_TRANSACTION_PIN_OTP: request,
      },
    );
    clearController();
  }

  void clearController() {
    newTransactionPinController.clear();
    confirmTransactionPinController.clear();
  }
}
