import 'dart:async';
import 'package:flutter/material.dart';

import 'package:mobdr/main.dart';
import 'package:mobdr/ui/account/tab_account.dart';
import 'package:mobdr/ui/home/tab_home.dart';
import 'package:mobdr/ui/mp4/tab_mp4.dart';
import 'package:mobdr/ui/sync/synchronization.dart';

import 'package:mobdr/config/constant.dart';
import 'package:mobdr/events.dart';

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage>
    with SingleTickerProviderStateMixin {
  late PageController _pageController;
  late StreamSubscription subUrlEvent;

  int _currentIndex = 0;

  List<Widget> _contentPages = [];

  @override
  void initState() {
    _contentPages = <Widget>[
      TabHomePage(),
      SynchronizationPage(),
      TabMP4Page(),
      TabAccountPage(),
    ];

    // set initial pages for navigation to home page
    _pageController = PageController(initialPage: 0);
    _pageController.addListener(_handleTabSelection);

    // Listen particular event
    subUrlEvent = eventBus.on<UrlEvent>().listen((e) {
      setState(() {
        _currentIndex = 2;
        _pageController.jumpToPage(_currentIndex);
        FocusScope.of(context).unfocus();
      });
    });

    super.initState();
  }

  void _handleTabSelection() {
    /*
    setState(() {
    });
    */
  }

  @override
  void dispose() {
    subUrlEvent.cancel();
    _pageController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: PageView(
        controller: _pageController,
        physics: NeverScrollableScrollPhysics(),
        children: _contentPages.map((Widget content) {
          return content;
        }).toList(),
      ),
      bottomNavigationBar: BottomNavigationBar(
        type: BottomNavigationBarType.fixed,
        currentIndex: _currentIndex,
        onTap: (value) {
          setState(() {
            _currentIndex = value;
            _pageController.jumpToPage(value);
            FocusScope.of(context).unfocus();
          });
        },
        selectedFontSize: 8,
        unselectedFontSize: 8,
        iconSize: 28,
        selectedItemColor:
            Colors.blue, // Couleur de l'icône de l'onglet sélectionné
        unselectedItemColor:
            CHARCOAL, // Couleur de l'icône des onglets non sélectionnés
        selectedLabelStyle: TextStyle(
          color: Colors.blue, // Couleur du texte de l'onglet sélectionné
          fontWeight: FontWeight.bold,
        ),
        unselectedLabelStyle: TextStyle(
          color: CHARCOAL, // Couleur du texte des onglets non sélectionnés
          fontWeight: FontWeight.bold,
        ),
        items: [
          BottomNavigationBarItem(
            label: 'Home',
            icon: Icon(Icons.home),
          ),
          BottomNavigationBarItem(
            label: 'Synchro',
            icon: Icon(Icons.sync),
          ),
          BottomNavigationBarItem(
            label: 'MP4',
            icon: Icon(Icons.web),
          ),
          BottomNavigationBarItem(
            label: 'Account',
            icon: Icon(Icons.person_outline),
          ),
        ],
      ),
    );
  }
}
