import 'package:base_app/blocs/list_bloc.dart';
import 'package:base_app/blocs/bloc_index.dart';
import 'package:base_app/res/index.dart';
import 'package:base_app/ui/pages/demo/list_page.dart';
import 'package:base_app/ui/pages/demo/map_page.dart';
import 'package:base_app/ui/pages/demo/demo_tab_page.dart';
import 'package:base_app/ui/pages/main_left_page.dart';
import 'package:base_app/ui/widgets/common/button/large_button_widget.dart';
import 'package:base_app/utils/util_index.dart';
import 'package:fluintl/fluintl.dart';
import 'package:flustars/flustars.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:oktoast/oktoast.dart';

import 'demo/simple_page.dart';

// 页面
class _Page {
  final String labelId;
  _Page(this.labelId);
}

final List<_Page> _allPages = <_Page>[
  _Page(Ids.titleHome),
  _Page(Ids.titleRepos),
  _Page(Ids.titleEvents),
  _Page(Ids.titleList),
];

/// 主菜单页
class MainPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return new MainPageState();
  }
}

class MainPageState extends State<MainPage> {
  DateTime _lastPressedAt; //上次点击时间
  int _currentIndex = 0;
  List<Widget> _pages;

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

  @override
  Widget build(BuildContext context) {
    // 全局
    return Scaffold(
        appBar: AppBar(
          title: Text(
              IntlUtil.getString(context, _allPages[_currentIndex].labelId)),
          centerTitle: true,
        ),
        // 抽屉
        drawer: Drawer(child: MainLeftPage()),
        // 底部导航栏
        bottomNavigationBar: BottomNavigationBar(
            currentIndex: _currentIndex, //当前tab
            selectedItemColor: Theme.of(context).primaryColor, // 选中后的颜色
            type: BottomNavigationBarType.fixed, // 类别fixed和shift
            unselectedItemColor: Colours.gray_66, //未选中颜色
            onTap: (int index) {
              setState(() {
                _currentIndex = index;
              });
            },
            items: [
              BottomNavigationBarItem(
                  title: new Text("simple"), icon: new Icon(Icons.home)),
              BottomNavigationBarItem(
                  title: new Text("tab"), icon: new Icon(Icons.tab)),
              BottomNavigationBarItem(
                  title: new Text("map"), icon: new Icon(Icons.map)),
              BottomNavigationBarItem(
                  title: new Text("list"), icon: new Icon(Icons.list)),
            ]),
        // 监控返回按钮
        body: new WillPopScope(
            child: buildTabView(context, _currentIndex),
            onWillPop: () async {
              if (_lastPressedAt == null ||
                  DateTime.now().difference(_lastPressedAt) >
                      Duration(seconds: 2)) {
                showToast("再按一次退出程序");
                //两次点击间隔超过1秒则重新计时
                _lastPressedAt = DateTime.now();
                return false;
              }
              return true;
            }));
  }

  Widget buildTabView(BuildContext context, index) {
    _Page page = _allPages[index];
    String labelId = page.labelId;
    switch (labelId) {
      case Ids.titleHome:
        return SimplePage(labelId: labelId);
        break;
      case Ids.titleRepos:
        return DemoTabPage(labelId: labelId);
        break;
      case Ids.titleEvents:
        return MapPage(labelId: labelId);
        break;
      case Ids.titleList:
        // new Bloc
        return new BlocProvider<ListBloc>(
            child: ListPage(labelId: labelId), bloc: new ListBloc());
        break;
      default:
        return Container();
        break;
    }
  }
}
