import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

class BankController extends GetxController {
  String selectedBank = '';

  Map<String, String> bankNames = {
    'Ahli United Bank'.tr: 'AUB',
    'Citi Bank N.A. Egypt'.tr: 'CITI',
    'MIDBANK'.tr: 'MIDB',
    'Banque Du Caire'.tr: 'BDC',
    'HSBC Bank Egypt S.A.E'.tr: 'HSBC',
    'Credit Agricole Egypt S.A.E'.tr: 'ECAE',
    'Egyptian Gulf Bank'.tr: 'EGB',
    'The United Bank'.tr: 'UB',
    'Qatar National Bank Alahli'.tr: 'QNB',
    'Arab Bank PLC'.tr: 'ARAB',
    'Emirates National Bank of Dubai'.tr: 'ENBD',
    'Al Ahli Bank of Kuwait – Egypt'.tr: 'ABK',
    'National Bank of Kuwait – Egypt'.tr: 'NBK',
    'Arab Banking Corporation - Egypt S.A.E'.tr: 'EABC',
    'First Abu Dhabi Bank'.tr: 'FAB',
    'Abu Dhabi Islamic Bank – Egypt'.tr: 'ADIB',
    'Commercial International Bank - Egypt S.A.E'.tr: 'CIB',
    'Housing And Development Bank'.tr: 'HDB',
    'Banque Misr'.tr: 'MISR',
    'Arab African International Bank'.tr: 'AAIB',
    'Egyptian Arab Land Bank'.tr: 'EALB',
    'Export Development Bank of Egypt'.tr: 'EDBE',
    'Faisal Islamic Bank of Egypt'.tr: 'FAIB',
    'Blom Bank'.tr: 'BLOM',
    'Abu Dhabi Commercial Bank – Egypt'.tr: 'ADCB',
    'Alex Bank Egypt'.tr: 'BOA',
    'Societe Arabe Internationale De Banque'.tr: 'SAIB',
    'National Bank of Egypt'.tr: 'NBE',
    'Al Baraka Bank Egypt B.S.C.'.tr: 'ABRK',
    'Egypt Post'.tr: 'POST',
    'Nasser Social Bank'.tr: 'NSB',
    'Industrial Development Bank'.tr: 'IDB',
    'Suez Canal Bank'.tr: 'SCB',
    'Mashreq Bank'.tr: 'MASHA',
    'Arab Investment Bank'.tr: 'AIB',
    'General Authority For Supply Commodities'.tr: 'GASCA',
    'Arab International Bank'.tr: 'AIB',
    'Agricultural Bank of Egypt'.tr: 'PDAC',
    'National Bank of Greece'.tr: 'NBG',
    'Central Bank Of Egypt'.tr: 'CBE',
    'ATTIJARIWAFA BANK Egypt'.tr: 'BBE',
  };

  @override
  void onInit() {
    super.onInit();
    selectedBank = bankNames.values.first;
  }

  void updateSelectedBank(String? bankShortName) {
    selectedBank = bankShortName ?? '';
    update();
  }

  List<DropdownMenuItem<String>> getDropdownItems() {
    return bankNames.keys.map<DropdownMenuItem<String>>((bankFullName) {
      return DropdownMenuItem<String>(
        value: bankNames[bankFullName],
        child: Text(bankFullName),
      );
    }).toList();
  }

  void showBankPicker(BuildContext context) {
    showCupertinoModalPopup(
      context: context,
      builder: (BuildContext context) => CupertinoActionSheet(
        title: Text('Select a Bank'.tr),
        actions: bankNames.keys.map((String bankFullName) {
          return CupertinoActionSheetAction(
            child: Text(bankFullName),
            onPressed: () {
              updateSelectedBank(bankNames[bankFullName]);
              Navigator.pop(context);
            },
          );
        }).toList(),
        cancelButton: CupertinoActionSheetAction(
          child: Text('Cancel'.tr),
          onPressed: () {
            Navigator.pop(context);
          },
        ),
      ),
    );
  }
}

class BankDropdown extends StatelessWidget {
  final BankController bankController = Get.put(BankController());

  @override
  Widget build(BuildContext context) {
    return GetBuilder<BankController>(
      init: bankController,
      builder: (controller) {
        return CupertinoButton(
          padding: EdgeInsets.zero,
          onPressed: () => controller.showBankPicker(context),
          child: Container(
            padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
            decoration: BoxDecoration(
              border: Border.all(color: CupertinoColors.systemGrey4),
              borderRadius: BorderRadius.circular(8),
            ),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Text(
                  controller.selectedBank != null
                      ? controller.bankNames.keys.firstWhere(
                          (key) =>
                              controller.bankNames[key] ==
                              controller.selectedBank,
                          orElse: () => 'Select a Bank'.tr,
                        )
                      : 'Select a Bank'.tr,
                  style: TextStyle(
                    color: controller.selectedBank != null
                        ? CupertinoColors.black
                        : CupertinoColors.systemGrey,
                  ),
                ),
                const Icon(CupertinoIcons.chevron_down, size: 20),
              ],
            ),
          ),
        );
      },
    );
  }
}
