import 'package:amap_location_fluttify/amap_location_fluttify.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:sport/application.dart';
import 'package:sport/config_inject.dart';
import 'package:sport/pages/game/index.dart';
import 'package:sport/pages/home/home_info_page.dart';
import 'package:sport/pages/my/my_page.dart';
import 'package:sport/pages/social/social_index_page.dart';
import 'package:sport/provider/bluetooth.dart';
import 'package:sport/services/app_lifecycle_state.dart';
import 'package:sport/utils/toast.dart';
import 'package:sport/utils/update_apk.dart';
import 'package:sport/widgets/decoration.dart';

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

class _HomePageState extends LifecycleState<HomePage> with ConfigInject {
  var _pageController = PageController();
  DateTime _lastPressed;

  ValueNotifier<int> _valueNotifierIndex = ValueNotifier(0);
  final Duration _exitDuration = Duration(seconds: 2);

  final List<Widget> _pages = <Widget>[HomeInfoPage(), GamePage(), SocialIndexPage(), MyPage()];

  @override
  void initState() {
    super.initState();
    Provider.of<Bluetooth>(context, listen: false).listen();
    updateApp(context);
  }

  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    super.didChangeAppLifecycleState(state);
    print("$state");
    switch (state) {
      case AppLifecycleState.resumed:
        Provider.of<Bluetooth>(context, listen: false).listen();
        break;
      case AppLifecycleState.paused:
        Provider.of<Bluetooth>(context, listen: false).disposeBluetooth();
        break;
      default:
        break;
    }
  }

  @override
  void dispose() {
    _valueNotifierIndex?.dispose();
    _pageController?.dispose();
    super.dispose();
    AmapLocation.instance?.dispose();
  }

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
  }

  @override
  void didUpdateWidget(HomePage oldWidget) {
    super.didUpdateWidget(oldWidget);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      body: WillPopScope(
        onWillPop: () async {
          if (_lastPressed == null || DateTime.now().difference(_lastPressed) > _exitDuration) {
            //两次点击间隔超过2秒则重新计时
            _lastPressed = DateTime.now();
            ToastUtil.show("再次点击返回键退出");
            return false;
          }
          return true;
        },
        child: Stack(
          children: <Widget>[
            Padding(
              padding:  EdgeInsets.only(bottom: 50 + MediaQuery.of(context).padding.bottom),
              child: PageView.builder(
                itemBuilder: (ctx, index) => _pages[index],
                itemCount: _pages.length,
                controller: _pageController,
                physics: NeverScrollableScrollPhysics(),
                onPageChanged: (index) {
                  Application.currentPage = index;
                },
              ),
            ),
            Positioned(
              right: 0,left: 0,bottom: 0,
              child: Container(
                decoration: shadowTop(),
                child: ValueListenableBuilder(
                  valueListenable: _valueNotifierIndex,
                  builder: (BuildContext context, int value, Widget child) => BottomNavigationBar(
                    backgroundColor: Colors.white,
                    elevation: 0,
                    iconSize: 22.0,
                    selectedFontSize: 11,
                    selectedItemColor: Color(0xff333333),
                    unselectedFontSize: 11,
                    unselectedItemColor: Color(0xff999999),
                    type: BottomNavigationBarType.fixed,
                    items: <BottomNavigationBarItem>[
                      BottomNavigationBarItem(
                        icon: Image.asset("lib/assets/img/tab_home_${value == 0 ? "press" : "normal"}.png"),
                        title: Text("首页"),
                      ),
                      BottomNavigationBarItem(
                        icon: Image.asset("lib/assets/img/tab_game_${value == 1 ? "press" : "normal"}.png"),
                        title: Text("运动"),
                      ),
                      BottomNavigationBarItem(
                        icon: Image.asset("lib/assets/img/tab_bbs_${value == 2 ? "press" : "normal"}.png"),
                        title: Text("社区"),
                      ),
                      BottomNavigationBarItem(
                        icon: Image.asset("lib/assets/img/tab_mine_${value == 3 ? "press" : "normal"}.png"),
                        title: Text("我的"),
                      ),
                    ],
                    currentIndex: value,
                    onTap: (index) {
                      _pageController.jumpToPage(index);
                      _valueNotifierIndex.value = index;
                    },
                  ),
                ),
              ),
            )
          ],
        ),
      ),
    );
  }
}
