import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:test_sa/controllers/providers/api/user_provider.dart';
import 'package:test_sa/extensions/context_extension.dart';
import 'package:test_sa/models/enums/user_types.dart';
import 'package:test_sa/new_views/pages/land_page/calendar_page.dart';
import 'package:test_sa/new_views/pages/land_page/my_request/my_requests_page.dart';
import 'package:test_sa/views/widgets/equipment/single_device_picker.dart';

import '../../../controllers/providers/settings/setting_provider.dart';
import '../../../views/widgets/dialogs/dialog.dart';
import '../../common_widgets/app_bottom_nav_bar.dart';
import '../../common_widgets/app_drawer.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;
  bool showAppbar = true;
  List<Widget> _pages;
  UserProvider _userProvider;

  @override
  void initState() {
    _pages = <Widget>[];
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    if (_userProvider == null) {
      _userProvider = Provider.of<UserProvider>(context, listen: false);
      _pages = <Widget>[
        DashboardPage(onDrawerPress: (() {
          _scaffoldKey.currentState.isDrawerOpen ? _scaffoldKey.currentState.closeDrawer() : _scaffoldKey.currentState.openDrawer();
        })),
        // const old_page.LandPage(),
        const MyRequestsPage(),
        const MyAssetsPage(fromBottomBar: true),
        if (_userProvider.user.type == UsersTypes.engineer) const CalendarPage(),
      ];
    }

    return WillPopScope(
      onWillPop: () async {
        bool result = await showDialog(
          context: context,
          builder: (_) => AAlertDialog(title: context.translation.signOut, content: context.translation.logoutAlert),
        );
        if (result) {
          Provider.of<SettingProvider>(context, listen: false).resetSettings();
          Provider.of<UserProvider>(context, listen: false).reset();
          Navigator.of(context).pop();
          Navigator.of(context).pop();
        }
        return false;
      },
      child: Scaffold(
        key: _scaffoldKey,
        drawer: const AppDrawer(),
        body: _pages[currentPageIndex],
        bottomNavigationBar: AppBottomNavigationBar(
          selectedIndex: currentPageIndex,
          onPressed: (index) {
            bool isEngineer = _userProvider.user.type == UsersTypes.engineer;

            if (index == (isEngineer ? 4 : 3)) {
              showModalBottomSheet(
                context: context,
                useSafeArea: true,
                backgroundColor: Colors.transparent,
                builder: (context) => const ContactUsBottomSheet(),
              );
            } else {
              setState(() {
                currentPageIndex = index;
              });
              if (index == 1) {
                setState(() {
                  showAppbar = false;
                });
              } else {
                setState(() {
                  showAppbar = true;
                });
              }
            }
          },
        ),
      ),
    );
  }
}
