import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:get/get.dart';
import 'package:timer_count_down/timer_controller.dart';
import 'package:uco_mobile_poc/app/core/config/server_response.dart';
import 'package:uco_mobile_poc/app/core/constants/app_contants.dart';
import 'package:uco_mobile_poc/app/core/data/repositories/app_repositories.dart';
import 'package:uco_mobile_poc/app/core/enums/request_constants.dart';
import 'package:uco_mobile_poc/app/core/utils/fields_utils.dart';
import 'package:uco_mobile_poc/app/custom_widgets/custom_toasty.dart';
import 'package:uco_mobile_poc/app/models/RegisterRequest.dart';
import 'package:uco_mobile_poc/app/models/SignupOtpRequest.dart';
import 'package:uco_mobile_poc/app/models/resend_ot_login_model.dart';
import 'package:uco_mobile_poc/app/routes/app_pages.dart';

import '../../../core/constants/form_field_constants.dart';
import '../../../custom_widgets/Fields/input_field.dart';

class SignUpScreenOTPController extends GetxController {
  final AppRepositories repository = Get.find<AppRepositories>();
  BuildContext context = Get.context as BuildContext;

  late InputField firstNameField;
  TextEditingController pinPutController = TextEditingController();

  RegisterRequest firstStepData = RegisterRequest.empty();

  RxBool isResend = false.obs;

  CountdownController countdownController = CountdownController(autoStart: true);

  @override
  Future<void> onReady() async {
    super.onReady();
    var arguments = Get.arguments;
    firstStepData = (arguments != null ? arguments[AppConstants.SIGN_UP_FIRST_STEP_DATA] ?? RegisterRequest.empty() : RegisterRequest.empty());
  }
  Future<void> validatePinAndNavigate() async {
    hideKeyBoard();
    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;
    }
    SignupOtpRequest request = SignupOtpRequest(
      username: firstStepData.username,
      email: firstStepData.email,
      obpPincode: pinPutController.text,
      // obpPintype: AppConstants.PIN_TYPE_SIGNUP,
      channelCode: RequestConstants.channelCode,
      porOrgacode: RequestConstants.porOrgacode,
      // pctCstycode: RequestConstants.pctCstycode,
    );

    ServerResponse response = await repository.signupStep2(request);
    if (response.isError) {
      Toasty.error(response.errorMsg);
      return;
    }
    Get.toNamed(Routes.SIGN_UP_SCREEN_STEP2,
        arguments: {
      AppConstants.SIGN_UP_FIRST_STEP_DATA: firstStepData,
    }
    );

    clearControllersOTP();
  }

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

  void resenOtp() async {

    if (!isResend.value) {
      Fluttertoast.showToast(msg: "Please wait for 1 minute");
      return;
    }

    var responce = await repository.resentOtpLoginSignUp(
      ResendOtpLoginModel(
        userRole: firstStepData.userRole,
        channelCode: RequestConstants.channelCode,
        porOrgacode: RequestConstants.porOrgacode,
        email: firstStepData.email,
        isOtpRequire: false,
        phone: firstStepData.phone,
        pinType: 'C_TRANPIN',
        username: firstStepData.email,
      ),
    );

    if (responce.isError) {
      Fluttertoast.showToast(msg: responce.errorMsg);
      return;
    }

    isResend.value = false;

    countdownController.setOnRestart(
      () {},
    );

    countdownController.restart();

    Fluttertoast.showToast(msg: "OTP successfully send to your email address");
  }

  void updateResend(bool send) {
    isResend.value = send;
  }
  @override
  void onClose() {
    FormFieldConstants.instance().clearKeysForScreen("SignUpScreenOTPView");
    super.onClose();
  }
}
