import 'package:didvan/config/design_config.dart';
import 'package:didvan/views/home/home_state.dart';
import 'package:didvan/views/home/news/news.dart';
import 'package:didvan/views/home/radar/radar.dart';
import 'package:didvan/views/home/settings/settings.dart';
import 'package:didvan/views/home/statistics/statistics.dart';
import 'package:didvan/views/home/studio/studio.dart';
import 'package:didvan/views/widgets/didvan/bnb.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

class Home extends StatefulWidget {
  const Home({Key? key}) : super(key: key);

  @override
  State<Home> createState() => _HomeState();
}

class _HomeState extends State<Home> with SingleTickerProviderStateMixin {
  late final TabController _tabController;

  @override
  void initState() {
    DesignConfig.updateSystemUiOverlayStyle();
    _tabController = TabController(length: 5, vsync: this, initialIndex: 2);
    _tabController.addListener(() {
      context.read<HomeState>().currentPageIndex = _tabController.index;
    });
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: TabBarView(
        physics: const NeverScrollableScrollPhysics(),
        controller: _tabController,
        children: const [
          News(),
          Statictics(),
          Radar(),
          Studio(),
          Settings(),
        ],
      ),
      bottomNavigationBar: Consumer<HomeState>(
        builder: (context, state, child) => DidvanBNB(
          currentTabIndex: state.currentPageIndex,
          onTabChanged: (index) {
            state.currentPageIndex = index;
            FocusScope.of(context).unfocus();
            _tabController.animateTo(index);
          },
        ),
      ),
    );
  }
}
