import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:yumi/app/constants/sc_global_config.dart';
import 'package:yumi/shared/data_sources/sources/local/user_manager.dart';
import 'package:yumi/modules/index/main_route.dart';
import 'package:yumi/services/auth/user_profile_manager.dart';
import 'package:provider/provider.dart';
import 'package:yumi/app/routes/sc_fluro_navigator.dart';
import 'package:yumi/ui_kit/theme/socialchat_theme.dart';
import 'package:yumi/ui_kit/widgets/countdown_timer.dart';

///Home页面
class SCIndexHomePage extends StatefulWidget {
  @override
  _SCIndexHomePageState createState() => _SCIndexHomePageState();
}

class _SCIndexHomePageState extends State<SCIndexHomePage>
    with TickerProviderStateMixin {
  TabController? _tabController;
  final List<Widget> _pages = [];
  final List<Widget> _tabs = [];
  bool refresh = false; // 👈 改为 false，初始显示加载

  @override
  void initState() {
    super.initState();
    // 在构建完成后加载数据
    WidgetsBinding.instance.addPostFrameCallback((_) {
      _updateTabs();
    });
  }

  void _updateTabs() {
    _tabController?.dispose();
    _pages.clear();

    final strategy = SCGlobalConfig.businessLogicStrategy;
    _pages.addAll(strategy.getHomeTabPages(context));

    _tabController = TabController(
      length: _pages.length,
      vsync: this,
      initialIndex: strategy.getHomeInitialTabIndex(),
    );
    // 可选：监听切换事件
    _tabController?.addListener(() {});

    // 数据更新完成后，刷新界面
    if (mounted) {
      setState(() {
        refresh = true;
      });
    }
  }

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

  @override
  Widget build(BuildContext context) {
    // 如果数据未准备好，显示加载指示器
    if (!refresh || _tabController == null || _pages.isEmpty) {
      return Center(
        child: Container(
          decoration: BoxDecoration(
            color: Colors.black26,
            borderRadius: BorderRadius.circular(8),
          ),
          width: 55.w,
          height: 55.w,
          child: const CupertinoActivityIndicator(),
        ),
      );
    }

    // 正常渲染界面
    _tabs.clear();
    final strategy = SCGlobalConfig.businessLogicStrategy;
    _tabs.addAll(strategy.getHomeTabLabels(context));

    return Stack(
      children: [
        Scaffold(
          resizeToAvoidBottomInset: false,
          backgroundColor: Colors.transparent,
          body: Column(
            children: [
              // 自定义顶部栏（包含状态栏和 TabBar）
              SafeArea(
                child: Row(
                  children: [
                    // TabBar 直接放在这里
                    Expanded(
                      child: TabBar(
                        tabAlignment: TabAlignment.start,
                        isScrollable: true,
                        splashFactory: NoSplash.splashFactory,
                        overlayColor: MaterialStateProperty.all(
                          Colors.transparent,
                        ),
                        indicator: const BoxDecoration(),
                        labelStyle: TextStyle(
                          fontWeight: FontWeight.bold,
                          fontStyle: FontStyle.italic,
                          fontSize: 19.sp,
                          color:SocialChatTheme.primaryLight,
                        ),
                        unselectedLabelStyle: TextStyle(
                          fontWeight: FontWeight.normal,
                          fontSize: 14.sp,
                          color: Colors.white
                        ),
                        indicatorColor: Colors.transparent,
                        dividerColor: Colors.transparent,
                        controller: _tabController,
                        tabs: _tabs,
                      ),
                    ),
                    Container(
                      padding: EdgeInsets.symmetric(horizontal: 16),
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.end,
                        children: [
                          IconButton(
                            icon: Image.asset(
                              "sc_images/index/sc_icon_serach.png",
                              width: 25.w,
                              height: 25.w,
                            ),
                            onPressed:
                                () => SCNavigatorUtils.push(
                                  context,
                                  SCMainRoute.mainSearch,
                                ),
                          ),
                        ],
                      ),
                    ),
                  ],
                ),
              ),
              // TabBarView 直接作为剩余空间
              Expanded(
                child: TabBarView(controller: _tabController, children: _pages),
              ),
            ],
          ),
        ),
        Consumer<SocialChatUserProfileManager>(
          builder: (context, ref, child) {
            if (SCGlobalConfig.isReview) return Container();
            final strategy = SCGlobalConfig.businessLogicStrategy;
            if (strategy.shouldShowFirstRechargePrompt()) {
              final position = strategy.getFirstRechargePosition();
              return PositionedDirectional(
                top: position['top'],
                bottom: position['bottom'],
                start: position['start'],
                end: position['end'],
                child: GestureDetector(
                  child: Column(
                    children: [
                      Image.asset(
                        "sc_images/index/sc_icon_first_recharge_tag.webp",
                        height: 70.w,
                      ),
                      Transform.translate(
                        offset: Offset(0, -10),
                        child: Container(
                          height: 23.w,
                          width: 100.w,
                          alignment: AlignmentDirectional.center,
                          decoration: BoxDecoration(
                            image: DecorationImage(
                              image: AssetImage(
                                "sc_images/index/sc_icon_first_recharge_btn.png",
                              ),
                              fit: BoxFit.fill,
                            ),
                          ),
                          child: CountdownTimer(
                            expiryDate: DateTime.fromMillisecondsSinceEpoch(
                              AccountStorage()
                                      .getCurrentUser()
                                      ?.userProfile
                                      ?.firstRechargeEndTime ??
                                  0,
                            ),
                            color: Colors.white,
                          ),
                        ),
                      ),
                    ],
                  ),
                  onTap: () {
                    strategy.onFirstRechargeTap(context);
                  },
                ),
              );
            }
            return Container();
          },
        ),
      ],
    );
  }
}
