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


import '../../../../core/constants/form_field_constants.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 AccountListScreen extends StatefulWidget {
  AccountListScreen({super.key});

  @override
  State<AccountListScreen> createState() => _AccountListScreenState();
}

class _AccountListScreenState extends State<AccountListScreen> {
  @override
  void initState() {
    Get.find<AddMoneyController>().searchAccountInit();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    String className = runtimeType.toString().split('.').last;
    return responsiveWidget(
      Scaffold(
        appBar: DashBoardAppBar(
          onBackButtonPressed: () {
            Get.back();
          },
        ),
        body: SingleChildScrollView(
          child: Padding(
            padding: const EdgeInsets.only(
              left: 20,
              right: 20,
              top: 20,
            ),
            child: GetBuilder<AddMoneyController>(builder: (controller) {
              return Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(
                    "Choose account",
                    style: context.textTheme.bodyMedium!.copyWith(fontSize: 15, fontWeight: FontWeight.w500, color: AppColors.titleColor),
                  ),
                  const SizedBox(height: 8),
                  const Divider(color: Colors.grey),
                  const SizedBox(height: 8),
                  controller.searchAccount = InputField(
                    refKey: FormFieldConstants.instance().getFormKey("${className}searchAccount"),
                    controller: FormFieldConstants.instance().getController("${className}searchAccount"),
                    hintText: "Search for a account",
                    prefixIcon: const Icon(Icons.search),
                    isDropDown: false,
                    showLabel: false,
                    isTopMarginRequired: false,
                  ),
                  const SizedBox(height: 8),
                  const Divider(color: Colors.grey),
                  const SizedBox(height: 8),
                  ...controller.currencyList
                      .map((e) => InkWell(
                          onTap: () {
                            controller.updateFromAccountCurrency(e);
                            Get.back();
                          },
                          child: currencyTile(e.dmpProdshort, "${e.dmpProddesc} (${e.dmpProdcode})", controller.getFlagWithCurrencyCode(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(
              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),
                ),
              ],
            )
          ],
        ),
      ),
    );
  }
}
