/* 
 * This file is part of Stack Wallet.
 * 
 * Copyright (c) 2023 Cypher Stack
 * All Rights Reserved.
 * The code is distributed under GPLv3 license, see LICENSE file for details.
 * Generated by Cypher Stack on 2023-05-26
 *
 */

import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:stackwallet/providers/providers.dart';
import 'package:stackwallet/themes/stack_colors.dart';
import 'package:stackwallet/utilities/constants.dart';
import 'package:stackwallet/utilities/text_styles.dart';
import 'package:stackwallet/wallets/crypto_currency/crypto_currency.dart';
import 'package:stackwallet/wallets/isar/providers/wallet_info_provider.dart';
import 'package:stackwallet/widgets/background.dart';
import 'package:stackwallet/widgets/custom_buttons/app_bar_icon_button.dart';
import 'package:stackwallet/widgets/rounded_white_container.dart';
import 'package:stackwallet/widgets/wallet_info_row/sub_widgets/wallet_info_row_balance.dart';
import 'package:stackwallet/widgets/wallet_info_row/sub_widgets/wallet_info_row_coin_icon.dart';

class ChooseFromStackView extends ConsumerStatefulWidget {
  const ChooseFromStackView({
    super.key,
    required this.coin,
  });

  final CryptoCurrency coin;

  static const String routeName = "/chooseFromStack";

  @override
  ConsumerState<ChooseFromStackView> createState() =>
      _ChooseFromStackViewState();
}

class _ChooseFromStackViewState extends ConsumerState<ChooseFromStackView> {
  late final CryptoCurrency coin;

  @override
  void initState() {
    coin = widget.coin;
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    final walletIds = ref
        .watch(pWallets)
        .wallets
        .where((e) => e.info.coin == coin)
        .map((e) => e.walletId)
        .toList();

    return Background(
      child: Scaffold(
        backgroundColor: Theme.of(context).extension<StackColors>()!.background,
        appBar: AppBar(
          leading: const AppBarBackButton(),
          title: Text(
            "Choose your ${coin.ticker.toUpperCase()} wallet",
            style: STextStyles.navBarTitle(context),
          ),
        ),
        body: Padding(
          padding: const EdgeInsets.all(16),
          child: walletIds.isEmpty
              ? Column(
                  children: [
                    RoundedWhiteContainer(
                      child: Center(
                        child: Text(
                          "No ${coin.ticker.toUpperCase()} wallets",
                          style: STextStyles.itemSubtitle(context),
                        ),
                      ),
                    ),
                  ],
                )
              : ListView.builder(
                  itemCount: walletIds.length,
                  itemBuilder: (context, index) {
                    final walletId = walletIds[index];

                    return Padding(
                      padding: const EdgeInsets.symmetric(vertical: 5.0),
                      child: RawMaterialButton(
                        splashColor: Theme.of(context)
                            .extension<StackColors>()!
                            .highlight,
                        materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
                        shape: RoundedRectangleBorder(
                          borderRadius: BorderRadius.circular(
                            Constants.size.circularBorderRadius,
                          ),
                        ),
                        padding: const EdgeInsets.all(0),
                        // color: Theme.of(context).extension<StackColors>()!.popupBG,
                        elevation: 0,
                        onPressed: () async {
                          if (mounted) {
                            Navigator.of(context).pop(walletId);
                          }
                        },
                        child: RoundedWhiteContainer(
                          // color: Colors.transparent,
                          child: Row(
                            children: [
                              WalletInfoCoinIcon(coin: coin),
                              const SizedBox(
                                width: 12,
                              ),
                              Expanded(
                                child: Column(
                                  mainAxisSize: MainAxisSize.min,
                                  crossAxisAlignment: CrossAxisAlignment.start,
                                  children: [
                                    Text(
                                      ref.watch(pWalletName(walletId)),
                                      style: STextStyles.titleBold12(context),
                                      overflow: TextOverflow.ellipsis,
                                    ),
                                    const SizedBox(
                                      height: 2,
                                    ),
                                    WalletInfoRowBalance(
                                      walletId: walletIds[index],
                                    ),
                                  ],
                                ),
                              )
                            ],
                          ),
                        ),
                      ),
                    );
                  },
                ),
        ),
      ),
    );
  }
}