import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:test_sa/extensions/int_extensions.dart';
import 'package:test_sa/new_views/pages/land_page/home_app_bar.dart';
import 'package:test_sa/new_views/pages/land_page/my_assets_page.dart';
import 'package:test_sa/new_views/pages/land_page/my_requests_page.dart';

import '../../common_widgets/app_bottom_nav_bar.dart';
import '../../common_widgets/app_drawer.dart';
import '../../common_widgets/app_floating_action_button.dart';
import 'contact_us_bottom_sheet.dart';
import 'dashboard_page.dart';

class LandPage extends StatefulWidget {
  static const String routeName = "/land-page";
  const LandPage({Key key}) : super(key: key);

  @override
  State<LandPage> createState() => _LandPageState();
}

class _LandPageState extends State<LandPage> {
  final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();
  int currentPageIndex = 0;
  final List<Widget> _pages = const <Widget>[
    DashboardPage(),
    MyRequestsPage(),
    MyAssetsPage(),
  ];

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: () async {
        /// TODO [zaid] : show dialog before exit
        // bool result = await showDialog(
        //   context: context,
        //   builder: (_) => const AAlertDialog(
        //     title: "_subtitle.exit",
        //     content: "_subtitle.exitAlert",
        //   ),
        // );
        if (true) {
          if (Platform.isAndroid) {
            SystemChannels.platform.invokeMethod('SystemNavigator.pop');
          } else {
            exit(0);
          }
        }
        return false;
      },
      child: Scaffold(
        key: _scaffoldKey,
        appBar: HomeAppBar(scaffoldKey: _scaffoldKey),
        body: Padding(
          padding: EdgeInsetsDirectional.only(start: 16.toScreenWidth, end: 11.toScreenHeight),
          child: _pages[currentPageIndex],
        ),
        drawer: const AppDrawer(),
        floatingActionButton: const AppFloatingActionButton(),
        bottomNavigationBar: AppBottomNavigationBar(
          selectedIndex: currentPageIndex,
          onPressed: (index) {
            if (index == 3) {
              showModalBottomSheet(
                context: context,
                useSafeArea: true,
                backgroundColor: Colors.transparent,
                builder: (context) => const ContactUsBottomSheet(),
              );
            } else {
              setState(() {
                currentPageIndex = index;
              });
            }
          },
        ),
      ),
    );
  }
}
