/* 
 * 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:event_bus/event_bus.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_svg/svg.dart';
import 'package:tuple/tuple.dart';

import '../../services/event_bus/events/global/wallet_sync_status_changed_event.dart';
import '../../themes/stack_colors.dart';
import '../../utilities/assets.dart';
import '../../utilities/constants.dart';
import '../../utilities/text_styles.dart';
import '../../wallets/isar/providers/eth/current_token_wallet_provider.dart';
import '../../widgets/background.dart';
import '../../widgets/custom_buttons/app_bar_icon_button.dart';
import '../../widgets/custom_buttons/blue_text_button.dart';
import '../../widgets/icon_widgets/eth_token_icon.dart';
import '../wallet_view/transaction_views/tx_v2/all_transactions_v2_view.dart';
import 'sub_widgets/token_summary.dart';
import 'sub_widgets/token_transaction_list_widget.dart';
import 'token_contract_details_view.dart';

/// [eventBus] should only be set during testing
class TokenView extends ConsumerStatefulWidget {
  const TokenView({
    super.key,
    required this.walletId,
    this.popPrevious = false,
    this.eventBus,
  });

  static const String routeName = "/token";

  final String walletId;
  final bool popPrevious;
  final EventBus? eventBus;

  @override
  ConsumerState<TokenView> createState() => _TokenViewState();
}

class _TokenViewState extends ConsumerState<TokenView> {
  late final WalletSyncStatus initialSyncStatus;

  @override
  void initState() {
    initialSyncStatus = ref.read(pCurrentTokenWallet)!.refreshMutex.isLocked
        ? WalletSyncStatus.syncing
        : WalletSyncStatus.synced;
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    debugPrint("BUILD: $runtimeType");

    return WillPopScope(
      onWillPop: () async {
        final nav = Navigator.of(context);
        if (widget.popPrevious) {
          nav.pop();
        }
        nav.pop();
        return false;
      },
      child: Background(
        child: Scaffold(
          backgroundColor:
              Theme.of(context).extension<StackColors>()!.background,
          appBar: AppBar(
            leading: AppBarBackButton(
              onPressed: () {
                final nav = Navigator.of(context);
                if (widget.popPrevious) {
                  nav.pop();
                }
                nav.pop();
              },
            ),
            centerTitle: true,
            title: Row(
              mainAxisAlignment: MainAxisAlignment.center,
              mainAxisSize: MainAxisSize.min,
              children: [
                Expanded(
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      EthTokenIcon(
                        contractAddress: ref.watch(
                          pCurrentTokenWallet.select(
                            (value) => value!.tokenContract.address,
                          ),
                        ),
                        size: 24,
                      ),
                      const SizedBox(
                        width: 10,
                      ),
                      Flexible(
                        child: Text(
                          ref.watch(
                            pCurrentTokenWallet
                                .select((value) => value!.tokenContract.name),
                          ),
                          style: STextStyles.navBarTitle(context),
                          overflow: TextOverflow.ellipsis,
                          textAlign: TextAlign.center,
                        ),
                      ),
                    ],
                  ),
                ),
              ],
            ),
            actions: [
              Padding(
                padding: const EdgeInsets.only(right: 2),
                child: AspectRatio(
                  aspectRatio: 1,
                  child: AppBarIconButton(
                    icon: SvgPicture.asset(
                      Assets.svg.verticalEllipsis,
                      color: Theme.of(context)
                          .extension<StackColors>()!
                          .topNavIconPrimary,
                    ),
                    onPressed: () {
                      // todo: context menu
                      Navigator.of(context).pushNamed(
                        TokenContractDetailsView.routeName,
                        arguments: Tuple2(
                          ref.watch(
                            pCurrentTokenWallet.select(
                                (value) => value!.tokenContract.address),
                          ),
                          widget.walletId,
                        ),
                      );
                    },
                  ),
                ),
              ),
            ],
          ),
          body: Container(
            color: Theme.of(context).extension<StackColors>()!.background,
            child: Column(
              children: [
                const SizedBox(
                  height: 10,
                ),
                Padding(
                  padding: const EdgeInsets.symmetric(horizontal: 16),
                  child: TokenSummary(
                    walletId: widget.walletId,
                    initialSyncStatus: initialSyncStatus,
                  ),
                ),
                const SizedBox(
                  height: 20,
                ),
                Padding(
                  padding: const EdgeInsets.symmetric(horizontal: 16),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      Text(
                        "Transactions",
                        style: STextStyles.itemSubtitle(context).copyWith(
                          color: Theme.of(context)
                              .extension<StackColors>()!
                              .textDark3,
                        ),
                      ),
                      CustomTextButton(
                        text: "See all",
                        onTap: () {
                          Navigator.of(context).pushNamed(
                            AllTransactionsV2View.routeName,
                            arguments: (
                              walletId: widget.walletId,
                              contractAddress: ref.watch(
                                pCurrentTokenWallet.select(
                                  (value) => value!.tokenContract.address,
                                ),
                              ),
                            ),
                          );
                        },
                      ),
                    ],
                  ),
                ),
                const SizedBox(
                  height: 12,
                ),
                Expanded(
                  child: Padding(
                    padding: const EdgeInsets.symmetric(horizontal: 16),
                    child: ClipRRect(
                      borderRadius: BorderRadius.vertical(
                        top: Radius.circular(
                          Constants.size.circularBorderRadius,
                        ),
                        bottom: Radius.circular(
                          // TokenView.navBarHeight / 2.0,
                          Constants.size.circularBorderRadius,
                        ),
                      ),
                      child: Container(
                        decoration: BoxDecoration(
                          color: Colors.transparent,
                          borderRadius: BorderRadius.circular(
                            Constants.size.circularBorderRadius,
                          ),
                        ),
                        child: Column(
                          crossAxisAlignment: CrossAxisAlignment.stretch,
                          children: [
                            Expanded(
                              child: TokenTransactionsList(
                                walletId: widget.walletId,
                              ),
                            ),
                          ],
                        ),
                      ),
                    ),
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}