import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:go_router/go_router.dart';

import '../../../core/l10n/app_localizations.dart';
import '../../../providers/asset_provider.dart'
    show assetProvider, currentAssetSubTabProvider;
import '../../../providers/auth_provider.dart';
import '../../../providers/copy_trading_provider.dart';
import '../../../providers/futures_provider.dart'
    show activeBottomTabProvider;
import '../../../providers/home_provider.dart';
import '../../../providers/my_copy_trading_provider.dart';

/// 需要登录才能进入的 tab 索引集合（5=资产）
const _authRequiredTabs = {5};

/// 底部导航壳 — 首页 / 行情 / 交易 / 合约 / 跟单 / 资产
class BottomNavShell extends ConsumerWidget {
  const BottomNavShell({super.key, required this.navigationShell});

  final StatefulNavigationShell navigationShell;

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    final l10n = AppLocalizations.of(context)!;
    return Scaffold(
      body: navigationShell,
      bottomNavigationBar: BottomNavigationBar(
        currentIndex: navigationShell.currentIndex,
        onTap: (index) {
          // 需要登录的 tab：未登录时跳转登录页
          if (_authRequiredTabs.contains(index)) {
            final isLoggedIn = ref.read(isLoggedInProvider);
            if (!isLoggedIn) {
              context.push('/login');
              return;
            }
          }
          ref.read(activeBottomTabProvider.notifier).state = index;

          // 交易 tab：始终回到现货
          if (index == 2) {
            context.go('/spot/BTCUSDT');
          } else {
            // 先切换 tab，刷新操作延迟到下一帧执行，避免阻塞切换动画
            navigationShell.goBranch(
              index,
              initialLocation: index == navigationShell.currentIndex,
            );
          }
          // 延迟执行数据刷新，不阻塞 tab 切换的首帧渲染
          Future.microtask(() {
            if (index == 0) {
              ref.read(homeProvider.notifier).refreshAsset();
            }
            if (index == 4) {
              ref.read(copyTradingProvider.notifier).refresh();
            }
            if (index == 5) {
              final subTab = ref.read(currentAssetSubTabProvider);
              ref.read(assetProvider.notifier).silentRefresh();
              if (subTab == 4) {
                ref.read(myCopyTradingProvider.notifier).silentRefresh();
              } else if (subTab == 5) {
                ref.read(assetProvider.notifier).loadStakingOrders();
              }
            }
          });
        },
        items: [
          BottomNavigationBarItem(
            icon: const Icon(Icons.home_outlined),
            activeIcon: const Icon(Icons.home),
            label: l10n.home,
          ),
          BottomNavigationBarItem(
            icon: const Icon(Icons.bar_chart_outlined),
            activeIcon: const Icon(Icons.bar_chart),
            label: l10n.market,
          ),
          BottomNavigationBarItem(
            icon: const Icon(Icons.swap_horiz_outlined),
            activeIcon: const Icon(Icons.swap_horiz),
            label: l10n.bottomNavTrade,
          ),
          BottomNavigationBarItem(
            icon: const Icon(Icons.layers_outlined),
            activeIcon: const Icon(Icons.layers),
            label: l10n.futures,
          ),
          BottomNavigationBarItem(
            icon: const Icon(Icons.people_alt_outlined),
            activeIcon: const Icon(Icons.people_alt),
            label: l10n.copyTrading,
          ),
          BottomNavigationBarItem(
            icon: const Icon(Icons.account_balance_wallet_outlined),
            activeIcon: const Icon(Icons.account_balance_wallet),
            label: l10n.assets,
          ),
        ],
      ),
    );
  }
}
