import 'package:flutter/material.dart';
import 'theme/app_colors.dart';
import 'i18n/app_localizations.dart';
import '../core/updater/update_service.dart';
import '../features/dashboard/presentation/pages/issuer_dashboard_page.dart';
import '../features/coupon_management/presentation/pages/coupon_list_page.dart';
import '../features/redemption/presentation/pages/redemption_page.dart';
import '../features/finance/presentation/pages/finance_page.dart';
import '../features/settings/presentation/pages/settings_page.dart';

/// 发行方主导航Shell
///
/// 底部5Tab：数据概览 / 券管理 / 核销 / 财务 / 我的
class IssuerMainShell extends StatefulWidget {
  const IssuerMainShell({super.key});

  @override
  State<IssuerMainShell> createState() => _IssuerMainShellState();
}

class _IssuerMainShellState extends State<IssuerMainShell> {
  int _currentIndex = 0;
  bool _updateChecked = false;

  final _pages = const [
    IssuerDashboardPage(),
    CouponListPage(),
    RedemptionPage(),
    FinancePage(),
    SettingsPage(),
  ];

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    if (!_updateChecked) {
      _updateChecked = true;
      _checkForUpdate();
    }
  }

  Future<void> _checkForUpdate() async {
    await Future.delayed(const Duration(seconds: 3));
    if (!mounted) return;
    await UpdateService().checkForUpdate(context);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: IndexedStack(
        index: _currentIndex,
        children: _pages,
      ),
      bottomNavigationBar: NavigationBar(
        selectedIndex: _currentIndex,
        onDestinationSelected: (index) {
          setState(() => _currentIndex = index);
        },
        destinations: [
          NavigationDestination(
            icon: const Icon(Icons.dashboard_outlined),
            selectedIcon: const Icon(Icons.dashboard_rounded),
            label: context.t('tab_dashboard'),
          ),
          NavigationDestination(
            icon: const Icon(Icons.confirmation_number_outlined),
            selectedIcon: const Icon(Icons.confirmation_number_rounded),
            label: context.t('tab_coupons'),
          ),
          NavigationDestination(
            icon: const Icon(Icons.qr_code_scanner_outlined),
            selectedIcon: const Icon(Icons.qr_code_scanner_rounded),
            label: context.t('tab_redemption'),
          ),
          NavigationDestination(
            icon: const Icon(Icons.account_balance_wallet_outlined),
            selectedIcon: const Icon(Icons.account_balance_wallet_rounded),
            label: context.t('tab_finance'),
          ),
          NavigationDestination(
            icon: const Icon(Icons.settings_outlined),
            selectedIcon: const Icon(Icons.settings_rounded),
            label: context.t('tab_mine'),
          ),
        ],
      ),
    );
  }
}
