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


import '../../../../core/constants/form_field_constants.dart';
import '../../../../core/utils/logs_utils.dart';
import '../../../../custom_widgets/Fields/input_field.dart';
import '../../../../custom_widgets/custom_app_bar.dart';
import '../../../../res/app_colors.dart';
import '../../../../routes/app_pages.dart';
import '../../controllers/add_money_controller.dart';

class CurrencyListScreen extends StatefulWidget {
  CurrencyListScreen({super.key});

  @override
  State<CurrencyListScreen> createState() => _CurrencyListScreenState();
}

class _CurrencyListScreenState extends State<CurrencyListScreen> {

  @override
  void initState() {
    Get.find<AddMoneyController>().searchCurrencyM();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    String className = runtimeType.toString().split('.').last;
    return responsiveWidget(
      Scaffold(
        appBar: DashBoardAppBar(
          onBackButtonPressed: () {
            Get.back();
          },
        ),
        body: GetBuilder<AddMoneyController>(builder: (controller) {
          return Padding(
            padding: const EdgeInsets.only(
              left: 20,
              right: 30,
              top: 20,
            ),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  "Choose a currency",
                  style: context.textTheme.bodyMedium!.copyWith(
                      fontSize: 18,
                      fontWeight: FontWeight.w500,
                      color: AppColors.titleColor),
                ),
                const SizedBox(height: 14),
                controller.searchCurrency = InputField(
                  refKey: FormFieldConstants.instance()
                      .getFormKey("${className}searchCurrency"),
                  controller: FormFieldConstants.instance()
                      .getController("${className}searchCurrency"),
                  hintText: "Search for a currency / country",
                  prefixIcon: const Icon(Icons.search),
                  isDropDown: false,
                  showLabel: false,
                  isTopMarginRequired: false,
                ),
                const SizedBox(height: 18),
                Text(
                  "Popular currencies",
                  style: context.textTheme.bodyMedium!.copyWith(
                    fontSize: 14,
                    fontWeight: FontWeight.w400,
                    color: AppColors.colorTitle,
                  ),
                ),
                const Divider(color: Colors.grey),
                ...controller.allAccountList
                    .map((e) => InkWell(
                        onTap: () {
                          dp("Click popular", "arg");
                          controller.updateReceipentAccount(e);
                          Get.back();
                        },
                        child: currencyTile(
                            e.mbmBkmstitle, e.dmpProddesc, e.pcrCurrcode)))
                    .toList(),
              ],
            ),
          );
        }),
      ),
    );
  }

  Widget currencyTile(String title, String subTitle, String flag) {
    return Padding(
      padding: const EdgeInsets.only(top: 0),
      child: SizedBox(
        height: 60,
        child: Row(
          crossAxisAlignment: CrossAxisAlignment.center,
          mainAxisAlignment: MainAxisAlignment.start,
          children: [
            Image.asset(
              Get.find<AddMoneyController>().getFlagWithCurrencyCode(flag),
              height: 40,
              width: 40,
            ),
            const SizedBox(width: 12),
            Column(
              mainAxisAlignment: MainAxisAlignment.center,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  title,
                  style: Get.context!.textTheme.bodyMedium!
                      .copyWith(fontSize: 14, fontWeight: FontWeight.w500),
                ),
                const SizedBox(height: 2),
                Text(
                  subTitle,
                  style: Get.context!.textTheme.bodyMedium!
                      .copyWith(fontSize: 12, fontWeight: FontWeight.w400),
                ),
              ],
            )
          ],
        ),
      ),
    );
  }
}
