import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.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 '../../../res/app_colors.dart';
import '../controllers/general_ledger_controller.dart';

class GeneralLedgerView extends GetView<GeneralLedgerController> {
  const GeneralLedgerView({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    String className = runtimeType.toString().split('.').last;
    return Scaffold(
      appBar: DashBoardAppBar(
        title: TranslationKeys.makeTranslation(TranslationKeys.transAddMoney),
        onBackButtonPressed: () {
          Navigator.of(context).pop();
        },
      ),
      persistentFooterButtons: [
        Padding(
          padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 10),
          child: CustomButton(
            onPressed: () {
              controller.handleNextClick();
            },
            buttonText: TranslationKeys.makeTranslation(TranslationKeys.textNext),
            buttonColor: AppColors.colorButton,
            buttonPadding: const EdgeInsets.only(top: 12, bottom: 12),
          ),
        )
      ],
      body: Padding(
        padding: const EdgeInsets.symmetric(horizontal: 15, vertical: 15),
        child: Column(
          children: [
            Text(TranslationKeys.makeTranslation(TranslationKeys.textSelectYourGLSAccount), style: Theme.of(context).textTheme.titleSmall?.copyWith(fontWeight: FontWeight.w500, fontSize: 20)),
            Card(
              color: AppColors.colorGrey50,
              margin: const EdgeInsets.symmetric(horizontal: 2, vertical: 10),
              elevation: 5.0,
              child: Container(
                  padding: const EdgeInsets.all(12),
                  decoration: const BoxDecoration(borderRadius: BorderRadius.all(Radius.circular(10)) // Add border radius
                      ),
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      Text(TranslationKeys.makeTranslation(TranslationKeys.textConformation), style: Theme.of(context).textTheme.titleSmall?.copyWith(fontWeight: FontWeight.w500, fontSize: 20)),
                      const SizedBox(height: 10),
                      Row(
                        children: [
                          Expanded(
                              flex: 2,
                              child: Padding(
                                padding: const EdgeInsets.all(0.0),
                                child: Column(
                                  crossAxisAlignment: CrossAxisAlignment.start,
                                  children: [
                                    Text(TranslationKeys.makeTranslation(TranslationKeys.textTotalAmount), style: Theme.of(context).textTheme.bodyMedium!.copyWith(fontSize: 14, fontWeight: FontWeight.w500)),
                                    const SizedBox(height: 10),
                                    Text(TranslationKeys.makeTranslation(TranslationKeys.textExchangeRate), style: Theme.of(context).textTheme.bodyMedium!.copyWith(fontSize: 14, fontWeight: FontWeight.w500)),
                                    const SizedBox(height: 10),
                                    Text(TranslationKeys.makeTranslation(TranslationKeys.textCurrency), style: Theme.of(context).textTheme.bodyMedium!.copyWith(fontSize: 14, fontWeight: FontWeight.w500)),
                                  ],
                                ),
                              )),

                          Expanded(
                              flex: 2,
                              child: Padding(
                                padding: const EdgeInsets.all(0.0),
                                child: Column(
                                  crossAxisAlignment: CrossAxisAlignment.end,
                                  mainAxisAlignment: MainAxisAlignment.end,
                                  children: [
                                    Text(controller.evaluatedCurrencyModel.value.sgtGntramtfc.toString(), style: Theme.of(context).textTheme.bodyMedium!.copyWith(fontSize: 14, fontWeight: FontWeight.w400)),
                                    const SizedBox(height: 10),
                                    Text(controller.evaluatedCurrencyModel.value.targetPerEratrateact.toString(), style: Theme.of(context).textTheme.bodyMedium!.copyWith(fontSize: 14, fontWeight: FontWeight.w400)),
                                    const SizedBox(height: 10),
                                    Text(getFlagWithCurrencyCode(controller.evaluatedCurrencyModel.value.pcrCurrcode.toString()), style: Theme.of(context).textTheme.bodyMedium!.copyWith(fontSize: 14, fontWeight: FontWeight.w400)),
                                  ],
                                ),
                              )),

                          Expanded(
                              flex: 2,
                              child: Padding(
                                padding: const EdgeInsets.only(left: 12.0, right: 12.0),
                                child: Column(
                                  crossAxisAlignment: CrossAxisAlignment.end,
                                  mainAxisAlignment: MainAxisAlignment.end,
                                  children: [
                                    Text("sdd", style: Theme.of(context).textTheme.bodySmall),
                                    const SizedBox(height: 10),
                                    Text("sds", style: Theme.of(context).textTheme.bodySmall),
                                    const SizedBox(height: 10),
                                    Text("skks", style: Theme.of(context).textTheme.bodySmall),
                                    const SizedBox(height: 10),
                                    Text("sk", style: Theme.of(context).textTheme.bodySmall),
                                  ],
                                ),
                              )),
                        ],
                      ),
                    ],
                  )),
            ),
            controller.glsAccounts = InputField(
              refKey: FormFieldConstants.instance().getFormKey("${className}depositAccountField"),
              controller: FormFieldConstants.instance().getController("${className}depositAccountField"),
              labelText: "Select GL Account",
              textInputAction: TextInputAction.done,
              suffixIcon: const Icon(Icons.keyboard_arrow_down, size: 25),
              isDropDown: true,
              hintText: "Select GL account",
              dropDownType: DropDownType.SPECIAL,
              items: controller.allGlsAccounts,
              onItemSelected: (item) {
                controller.updateSelectedDeposit(item!);
              },
            ),

            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,
              isPassword: true,
              inputFormatters: [LengthLimitingTextInputFormatter(FieldUtils.transactionPinLength), InputType.NUMBER],
              onClick: () {
                controller.transactionPinDialog(context, controller);
              },
            ),

            controller.messageField = InputField(
              enableInteractiveSelection: false,
              maxLines: 1,
              refKey: FormFieldConstants.instance().getFormKey("${className}messageField"),
              controller: FormFieldConstants.instance().getController("${className}messageField"),
              hintText: TranslationKeys.makeTranslation(TranslationKeys.textEnterMessage),
              labelText: TranslationKeys.makeTranslation(TranslationKeys.textMessageMemo),
              onSubmitted: (value) {
                controller.handleNextClick();
              },
            ),
          ],
        ),
      ),
    );
  }

  String getFlagWithCurrencyCode(String currencyCode) {
    int currencyCodeInt = int.tryParse(currencyCode) ?? 0;

    if (currencyCodeInt == 586) {
      return "PKR";
    } else if (currencyCodeInt == 997) {
      return "USD";
    } else if (currencyCodeInt == 978) {
      return "EUR";
    } else {
      return "";
    }
  }
}
