import 'dart:async';
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter_styled_toast/flutter_styled_toast.dart';
import 'package:prime_chat/screens/ContactsScreen.dart';
import 'package:prime_chat/screens/SpaceScreen.dart';

// MAIN UI 底部导航栏
class AppScreen extends StatefulWidget {
  const AppScreen({Key? key}) : super(key: key);

  @override
  State<AppScreen> createState() => _AppScreenState();
}

class _AppScreenState extends State<AppScreen> {
  late StreamSubscription<String?> _linkSubscription;


  static const List<Widget> _widgetOptions = <Widget>[
    ContactsScreen(),
    SpaceScreen(),
  ];

  late int _selectedIndex = 0;

  void _onItemTapped(int index) {
    setState(() {
      _selectedIndex = index;
    });
  }

  @override
  Widget build(BuildContext context) {
    final body = Scaffold(
      body: IndexedStack(
        index: _selectedIndex,
        children: _widgetOptions,
      ),
      bottomNavigationBar: BottomNavigationBar(
        items: const <BottomNavigationBarItem>[
          BottomNavigationBarItem(
            icon: Icon(Icons.people),
            label: '通讯录',
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.face),
            label: '我的',
          ),
        ],
        currentIndex: _selectedIndex,
        iconSize: 20,
        selectedFontSize: 12,
        unselectedFontSize: 12,
        onTap: _onItemTapped,
      ),
    );
    return willPop(body);
  }

  int _noticeTime = 0;

  Widget willPop(Scaffold body) {
    return WillPopScope(
      child: body,
      onWillPop: () async {
        final now = DateTime.now().millisecondsSinceEpoch;
        if (_noticeTime + 3000 > now) {
          return true;
        } else {
          _noticeTime = now;
          showToast(
            "再次返回将会退出应用程序",
            context: context,
            position: StyledToastPosition.center,
            animation: StyledToastAnimation.scale,
            reverseAnimation: StyledToastAnimation.fade,
            duration: const Duration(seconds: 3),
            animDuration: const Duration(milliseconds: 300),
            curve: Curves.elasticOut,
            reverseCurve: Curves.linear,
          );
          return false;
        }
      },
    );
  }
}
