import 'package:flutter/material.dart';

import '../../../core/l10n/app_localizations.dart';
import '../../../core/theme/app_colors.dart';
import 'airdrop_screen.dart';
import 'staking_screen.dart';

class FinanceHubScreen extends StatefulWidget {
  const FinanceHubScreen({
    super.key,
    this.initialTab = 0,
  });

  final int initialTab;

  @override
  State<FinanceHubScreen> createState() => _FinanceHubScreenState();
}

class _FinanceHubScreenState extends State<FinanceHubScreen>
    with SingleTickerProviderStateMixin {
  late final TabController _tabController;

  @override
  void initState() {
    super.initState();
    _tabController = TabController(
      length: 2,
      vsync: this,
      initialIndex: widget.initialTab.clamp(0, 1),
    )..addListener(() {
        if (mounted) {
          setState(() {});
        }
      });
  }

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

  @override
  Widget build(BuildContext context) {
    final l10n = AppLocalizations.of(context)!;
    final isAirdrop = _tabController.index == 1;
    final title = isAirdrop ? l10n.airdropTitle : l10n.financeIdoTitle;
    final unselectedColor = Theme.of(context).brightness == Brightness.dark
        ? Colors.white.withAlpha(160)
        : Theme.of(context).colorScheme.onSurface.withAlpha(160);

    return Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.transparent,
        foregroundColor: Theme.of(context).colorScheme.onSurface,
        elevation: 0,
        centerTitle: true,
        title: Text(
          title,
          style: const TextStyle(
            fontSize: 20,
            fontWeight: FontWeight.w600,
          ),
        ),
        bottom: PreferredSize(
          preferredSize: const Size.fromHeight(44),
          child: TabBar(
            controller: _tabController,
            labelColor: AppColors.brand,
            unselectedLabelColor: unselectedColor,
            indicatorColor: AppColors.brand,
            indicatorWeight: 2,
            labelStyle: const TextStyle(
              fontSize: 15,
              fontWeight: FontWeight.w600,
            ),
            unselectedLabelStyle: const TextStyle(
              fontSize: 15,
              fontWeight: FontWeight.w400,
            ),
            tabs: [
              Tab(text: l10n.financeIdoTitle),
              Tab(text: l10n.airdropTitle),
            ],
          ),
        ),
      ),
      body: TabBarView(
        controller: _tabController,
        children: const [
          StakingScreen(showAppBar: false),
          AirdropScreen(showAppBar: false),
        ],
      ),
    );
  }
}
