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/ChangeTransactionPinRequest.dart';
import '../../../routes/app_pages.dart';


class ChangeTransactionPinController extends GetxController {
  TextEditingController oldTransactionPinController = TextEditingController();
  TextEditingController newTransactionPinController = TextEditingController();
  TextEditingController confirmTransactionPinController = TextEditingController();

  final AppRepositories repository = Get.find<AppRepositories>();

  void validateAndSendRequest() async {
    if (oldTransactionPinController.text.isEmpty) {
      Toasty.error("Please input and then proceed!");
      return;
    }

    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 (oldTransactionPinController.text != newTransactionPinController.text) {
    //   Toasty.error("Old and New Transaction Pin not match!");
    //   return;
    // }

    if (newTransactionPinController.text != confirmTransactionPinController.text) {
      Toasty.error("New and Confirm Transaction Pin not match!");
      return;
    }


    ChangeTransactionPinRequest request = ChangeTransactionPinRequest(
     oldTransPincode:  oldTransactionPinController.text,
      newTransPincode: newTransactionPinController.text,
      channelCode: RequestConstants.channelCode,
      porOrgacode: RequestConstants.porOrgacode,
      pctCstycode: RequestConstants.pctCstycode,
      cmpCustcode: SessionCache.instance.userInfo.cmpCustcode,
      isOtpRequired: false,
      pinType: AppConstants.PIN_TYPE_Transaction,

    );

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

    Get.toNamed(Routes.CHANGE_TRANSACTION_PIN_OTP,
      arguments: {
      'oldTransactionPin':  oldTransactionPinController.text,
      'newTransactionPin': newTransactionPinController.text,
      'confirmTransactionPin': confirmTransactionPinController.text,
      AppConstants.CHANGE_TRANSACTION_PIN_OTP: request,
    },
    );

    clearController();
  }

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