import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';
import 'package:pin_code_fields/pin_code_fields.dart';


import '../../../core/constants/form_field_constants.dart';
import '../../../core/constants/translation_keys.dart';
import '../../../core/utils/fields_utils.dart';
import '../../../custom_widgets/Fields/input_field.dart';
import '../../../custom_widgets/custom_app_bar.dart';
import '../../../custom_widgets/custom_button.dart';
import '../../../custom_widgets/custom_toasty.dart';
import '../../../res/app_colors.dart';
import '../../../routes/app_pages.dart';
import '../controllers/beneficiary_transfer_controller.dart';

class BeneficiaryTransferView extends GetView<BeneficiaryTransferController> {
  const BeneficiaryTransferView({Key? key, this.showAppBar = true}) : super(key: key);
  final bool showAppBar;

  @override
  Widget build(BuildContext context) {

    String className = runtimeType.toString().split('.').last;

    return Scaffold(
      appBar: showAppBar
          ? DashBoardAppBar(
        title: "Beneficiary Payment",
        /*TranslationKeys.makeTranslation(TranslationKeys.textUSend),*/
        onBackButtonPressed: () {
          Get.back();
        },
      )
          : null,
      body: Column(
        children: [
          Expanded(
            child: SingleChildScrollView(
              child: Padding(
                padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 20),
                child: Card(
                  color: AppColors.colorGrey50,
                  child: Padding(
                    padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 10),
                    child: Column(
                      mainAxisAlignment: MainAxisAlignment.start,
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: <Widget>[
                        controller.fromAccountField = InputField(
                          refKey: FormFieldConstants.instance().getFormKey("${className}fromAccountField"),
                          controller: FormFieldConstants.instance().getController("${className}fromAccountField"),
                          labelText: "Select From Account",
                          isDropDown: true,
                          hintText: "Select from account",
                          dropDownType: DropDownType.SPECIAL,
                          isRequired: true,
                          items: controller.allFromAccounts,
                          onItemSelected: (item) {
                            // controller.updateSelectedDeposit(item!);
                            controller.onFromAccountSelection(item!);
                          },
                        ),
                        controller.toAccountField = InputField(
                          enableInteractiveSelection: false,
                          refKey: FormFieldConstants.instance().getFormKey("${className}toAccountField"),
                          controller: FormFieldConstants.instance().getController("${className}toAccountField"),
                          labelText: "To Account",
                          readOnly: true,
                          showCursor: false,
                        ),
                        Row(
                          children: [
                            Expanded(
                              flex: 1,
                              child: controller.sendingAmountField = InputField(
                                enableInteractiveSelection: false,
                                maxLines: 1,
                                textInputAction: TextInputAction.done,
                                refKey: FormFieldConstants.instance().getFormKey("${className}sendingAmountField"),
                                controller: FormFieldConstants.instance().getController("${className}sendingAmountField"),
                                labelText: TranslationKeys.makeTranslation(TranslationKeys.textSendingAmount),
                                hintText: TranslationKeys.makeTranslation(TranslationKeys.textSendingAmount),
                                isRequired: true,
                                keyboardType: const TextInputType.numberWithOptions(decimal: true),
                                inputFormatters: InputType.maxValueFilter(InputType.INT_MAX_VALUE_, true, 2),
                              ),
                            ),
                            const SizedBox(width: 10),
                            Expanded(
                              flex: 1,
                              child: controller.exchangeRateField = InputField(
                                enableInteractiveSelection: false,
                                refKey: FormFieldConstants.instance().getFormKey("${className}exchangeRateField"),
                                controller: FormFieldConstants.instance().getController("${className}exchangeRateField"),
                                hintText: TranslationKeys.makeTranslation(TranslationKeys.textExchangeRate),
                                labelText: TranslationKeys.makeTranslation(TranslationKeys.textExchangeRate),
                                readOnly: true,
                                showCursor: false,
                              ),
                            ),
                          ],
                        ),
                        controller.userTransactionPinField = InputField(
                          enableInteractiveSelection: false,
                          refKey: FormFieldConstants.instance().getFormKey("${className}userTransactionPinField"),
                          controller: FormFieldConstants.instance().getController("${className}userTransactionPinField"),
                          hintText: "Enter transaction pin",
                          labelText: "Transaction Pin",
                          readOnly: true,
                          showCursor: false,
                          requiredPasswordIcon: true,
                          isRequired: true,
                          isPassword: true,
                          inputFormatters: [LengthLimitingTextInputFormatter(FieldUtils.transactionPinLength), InputType.NUMBER],
                          onClick: () {
                            transactionPinDialog(context, controller);
                          },
                        ),
                        controller.referenceCodeField = InputField(
                          enableInteractiveSelection: false,
                          readOnly: true,
                          showCursor: false,
                          requiredPasswordIcon: true,
                          isRequired: true,
                          isPassword: true,
                          refKey: FormFieldConstants.instance().getFormKey("${className}referenceCodeField"),
                          controller: FormFieldConstants.instance().getController("${className}referenceCodeField"),
                          hintText: "Enter reference code",
                          labelText: TranslationKeys.makeTranslation(TranslationKeys.textReferenceCode),
                          inputFormatters: [LengthLimitingTextInputFormatter(FieldUtils.transactionPinLength), InputType.NUMBER],
                          onClick: () {
                            referenceCodeDialog(context, controller);
                          },
                        ),
                        controller.messageField = InputField(
                          enableInteractiveSelection: false,
                          maxLines: 1,
                          refKey: FormFieldConstants.instance().getFormKey("${className}messageField"),
                          controller: FormFieldConstants.instance().getController("${className}messageField"),
                          hintText: "Enter message",
                          labelText: TranslationKeys.makeTranslation(TranslationKeys.textMessageMemo),
                          onSubmitted: (value) {
                            controller.sendBeneficiaryTransaction();
                          },
                        ),
                        const SizedBox(height: 20),
                        Row(
                          children: [
                            Expanded(
                              flex: 1,
                              child: CustomButton(
                                onPressed: () {
                                  FocusScope.of(context).requestFocus(FocusNode());
                                  controller.sendBeneficiaryTransaction();
                                },
                                buttonText: TranslationKeys.makeTranslation(TranslationKeys.textSend),
                                buttonPadding: const EdgeInsets.symmetric(vertical: 11),
                                textStyle: Theme.of(context).textTheme.titleSmall?.copyWith(color: AppColors.white),
                                buttonColor: AppColors.colorButton,
                              ),
                            ),
                            // const SizedBox(width: 10),
                            // Expanded(
                            //   flex: 1,
                            //   child: CustomButton(
                            //     onPressed: () {
                            //       FocusScope.of(context).requestFocus(FocusNode());
                            //       controller.clearTransaction();
                            //       controller.disposeAllResources();
                            //     },
                            //     buttonPadding: const EdgeInsets.symmetric(vertical: 11),
                            //     buttonText: TranslationKeys.makeTranslation(TranslationKeys.textClear),
                            //     buttonColor: AppColors.colorSkipBtn,
                            //     textStyle: Theme.of(context).textTheme.titleSmall?.copyWith(color: AppColors.black),
                            //     sideBorder: const BorderSide(color: AppColors.colorBorderSkipBtn, width: 1),
                            //   ),
                            // ),
                          ],
                        ),
                      ],
                    ),
                  ),
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }

  /// Pin Dialog...........Transactions
  void transactionPinDialog(BuildContext context, BeneficiaryTransferController controller) {
    Get.dialog(
      responsiveWidget(
        Dialog(
          backgroundColor: Colors.transparent,
          shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(10)),
          child: Container(
            decoration: const BoxDecoration(
              color: Colors.white,
              borderRadius: BorderRadius.all(Radius.circular(10.0)),
            ),
            child: SingleChildScrollView(
              child: Padding(
                padding: const EdgeInsets.all(10),
                child: ListView(
                  physics: const NeverScrollableScrollPhysics(),
                  shrinkWrap: true,
                  children: [
                    Column(
                      mainAxisAlignment: MainAxisAlignment.center,
                      crossAxisAlignment: CrossAxisAlignment.center,
                      children: [
                        const SizedBox(height: 10),
                        Text(TranslationKeys.makeTranslation(TranslationKeys.textTransactionPin), style: Theme.of(context).textTheme.titleMedium),
                        const SizedBox(height: 20),
                        Wrap(
                          children: [
                            PinCodeTextField(
                              autoDisposeControllers: false,
                              keyboardType: TextInputType.number,
                              controller: controller.pinPutController,
                              showCursor: true,
                              autoDismissKeyboard: false,
                              enablePinAutofill: true,
                              length: 6,
                              hintCharacter: "0",
                              appContext: context,
                              hintStyle: Theme.of(context).textTheme.bodySmall!.copyWith(fontSize: 35, fontWeight: FontWeight.w600, color: AppColors.colorGrey350),
                              mainAxisAlignment: MainAxisAlignment.center,
                              cursorColor: Colors.black.withOpacity(0.8),
                              obscureText: false,
                              animationType: AnimationType.fade,
                              pinTheme: PinTheme(
                                selectedColor: AppColors.colorGrey350,
                                /*Colors.black.withOpacity(0.6),*/
                                inactiveColor: AppColors.colorGrey350,
                                /*Colors.black.withOpacity(0.6),*/
                                activeColor: AppColors.colorGrey350,
                                fieldOuterPadding: const EdgeInsets.all(5),
                                shape: PinCodeFieldShape.box,
                                borderRadius: BorderRadius.circular(5),
                                fieldHeight: 50,
                                inactiveFillColor: AppColors.white,
                                selectedFillColor: AppColors.white,
                                fieldWidth: 40,
                                activeFillColor: Colors.white,
                              ),
                              animationDuration: const Duration(milliseconds: 100),
                              backgroundColor: Colors.transparent,
                              enableActiveFill: true,
                              onCompleted: (v) {},
                              onChanged: (value) {},
                              beforeTextPaste: (text) {
                                return true;
                              },
                            ),
                          ],
                        ),
                        const SizedBox(height: 20),
                        Padding(
                          padding: const EdgeInsets.only(left: 80.0, right: 80.0),
                          child: CustomButton(
                              onPressed: () async {
                                if (controller.pinPutController.text.length != 6) {
                                  Toasty.error("Please input 6 digit transaction pin");
                                  return;
                                }
                                controller.userTransactionPinField.setText(controller.pinPutController.text);
                                Navigator.of(context, rootNavigator: true).pop();
                              },
                              buttonText: TranslationKeys.makeTranslation(TranslationKeys.textContinue),
                              buttonPadding: const EdgeInsets.symmetric(vertical: 10, horizontal: 20),
                              buttonColor: AppColors.colorButton),
                        ),
                      ],
                    ),
                  ],
                ),
              ),
            ),
          ),
        ),
      ),
      barrierDismissible: false,
    );
  }

  /// Reference Dialog...........Transactions
  void referenceCodeDialog(BuildContext context, BeneficiaryTransferController controller) {
    Get.dialog(
      responsiveWidget(
        Dialog(
          backgroundColor: Colors.transparent,
          shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(10)),
          child: Container(
            decoration: const BoxDecoration(
              color: Colors.white,
              borderRadius: BorderRadius.all(Radius.circular(10.0)),
            ),
            child: SingleChildScrollView(
              child: Padding(
                padding: const EdgeInsets.all(10),
                child: ListView(
                  physics: const NeverScrollableScrollPhysics(),
                  shrinkWrap: true,
                  children: [
                    Column(
                      mainAxisAlignment: MainAxisAlignment.center,
                      crossAxisAlignment: CrossAxisAlignment.center,
                      children: [
                        const SizedBox(height: 10),
                        Text(TranslationKeys.makeTranslation(TranslationKeys.textReferenceCode), style: Theme.of(context).textTheme.titleMedium),
                        const SizedBox(height: 20),
                        Wrap(
                          children: [
                            PinCodeTextField(
                              autoDisposeControllers: false,
                              keyboardType: TextInputType.number,
                              controller: controller.referenceCodeController,
                              showCursor: true,
                              autoDismissKeyboard: false,
                              enablePinAutofill: true,
                              length: 6,
                              hintCharacter: "0",
                              appContext: context,
                              hintStyle: Theme.of(context).textTheme.bodySmall!.copyWith(
                                fontSize: 35,
                                fontWeight: FontWeight.w600,
                                color: AppColors.colorGrey350,
                              ),
                              mainAxisAlignment: MainAxisAlignment.center,
                              cursorColor: Colors.black.withOpacity(0.8),
                              obscureText: false,
                              animationType: AnimationType.fade,
                              textInputAction: TextInputAction.done,
                              pinTheme: PinTheme(
                                selectedColor: AppColors.colorGrey350,
                                /*Colors.black.withOpacity(0.6),*/
                                inactiveColor: AppColors.colorGrey350,
                                /*Colors.black.withOpacity(0.6),*/
                                activeColor: AppColors.colorGrey350,
                                fieldOuterPadding: const EdgeInsets.all(5),
                                shape: PinCodeFieldShape.box,
                                borderRadius: BorderRadius.circular(5),
                                fieldHeight: 50,
                                inactiveFillColor: AppColors.white,
                                selectedFillColor: AppColors.white,
                                fieldWidth: 40,
                                activeFillColor: Colors.white,
                              ),
                              animationDuration: const Duration(milliseconds: 100),
                              backgroundColor: Colors.transparent,
                              enableActiveFill: true,
                              onCompleted: (v) {},
                              onChanged: (value) {},
                              beforeTextPaste: (text) {
                                return true;
                              },
                            ),
                          ],
                        ),
                        const SizedBox(height: 20),
                        Padding(
                          padding: const EdgeInsets.only(left: 80.0, right: 80.0),
                          child: CustomButton(
                              onPressed: () async {
                                if (controller.referenceCodeController.text.length != 6) {
                                  Toasty.error("Please input 6 digit transaction pin");
                                  return;
                                }
                                controller.referenceCodeField.setText(controller.referenceCodeController.text);
                                Navigator.of(context, rootNavigator: true).pop();
                              },
                              buttonText: TranslationKeys.makeTranslation(TranslationKeys.textContinue),
                              buttonPadding: const EdgeInsets.symmetric(vertical: 10, horizontal: 20),
                              buttonColor: AppColors.colorButton),
                        ),
                      ],
                    ),
                  ],
                ),
              ),
            ),
          ),
        ),
      ),
      barrierDismissible: false,
    );
  }
}
