import 'dart:io';

import 'package:flutter/material.dart';
import 'package:school_management_system/api/session_requests.dart';
import 'package:school_management_system/components/default_values.dart';
import 'package:school_management_system/screens/main/components/navigation_details.dart';
import 'package:school_management_system/screens/welcome/welcome_screen.dart';

class MainScaffold extends StatefulWidget {
  //final Widget child;
  final String appTitle;

  const MainScaffold({
    super.key,
    // required this.child,
    this.appTitle = 'School Management Demo',
  });

  @override
  State<MainScaffold> createState() => _MainScaffold();
}

class _MainScaffold extends State<MainScaffold> {
  var selectedIndex = 0;

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

  List<NavigationItems> navigationItems = getNavigationItems();

  @override
  Widget build(BuildContext context) {
    Widget page = navigationItems[selectedIndex].widget;

    return FutureBuilder(
        future: getCurrentSession(),
        builder: (context, snapshot) {
          return Scaffold(
            resizeToAvoidBottomInset: true,
            appBar: AppBar(
              title: Row(
                children: [
                  if (navigationItems[selectedIndex].label == 'Home')
                    Row(
                      children: [
                        const SizedBox(width: 35),
                        Text(navigationItems[selectedIndex].label)
                      ],
                    )
                  else
                    Row(
                      children: [
                        Image.asset(
                          'assets/images/unilogo.png',
                          width: 25,
                          height: 25,
                          fit: BoxFit.contain,
                        ),
                        const SizedBox(width: 10),
                        Text(navigationItems[selectedIndex].label),
                      ],
                    ),
                  // Text(

                  //   navigationItems[selectedIndex].label,

                  // ),
                ],
              ),
              backgroundColor: primaryColor,
              actions: [
                IconButton(
                  icon: const Icon(
                    Icons.logout,
                  ),
                  tooltip: 'Logout',
                  onPressed: () {
                    Navigator.push(
                      context,
                      MaterialPageRoute(
                        builder: (context) {
                          if (Platform.isAndroid) {
                            //SystemNavigator.pop();
                            return const WelcomeScreen();
                          } else if (Platform.isIOS) {
                      //      exit(0);
                            return const WelcomeScreen();
                          } else {
                            return const WelcomeScreen();
                          }
                        },
                      ),
                    );
                  },
                )
              ],
            ),
            drawer: SafeArea(
              child: Responsive(
                mobile: Row(
                  children: [
                    NavigationDrawer(
                      elevation: 50,
                      backgroundColor: primaryLightColor.withOpacity(1),
                      indicatorColor: primaryColor,
                      selectedIndex: selectedIndex,
                      onDestinationSelected: (value) {
                        setState(() {
                         Navigator.pop(context);
                          selectedIndex = value;
                        });
                      },
                      children:
                          navigationItems.map((NavigationItems navigationItem) {
                        return NavigationDrawerDestination(
                          icon: navigationItem.icon,
                          label: Text(navigationItem.label),
                          selectedIcon: navigationItem.icon,
                          backgroundColor: primaryColor,
                        );
                      }).toList(),
                    ),
                  ],
                ),
                desktop: Row(
                  children: [
                    NavigationRail(
                      extended: true,
                      labelType: NavigationRailLabelType.none,
                      destinations:
                          navigationItems.map((NavigationItems navigationItem) {
                        return NavigationRailDestination(
                          label: Text(navigationItem.label),
                          icon: navigationItem.icon,
                          selectedIcon: navigationItem.icon,
                        );
                      }).toList(),
                      selectedIndex: selectedIndex,
                      onDestinationSelected: (value) {
                        setState(() {
                          selectedIndex = value;
                        });
                      },
                    ),
                  ],
                ),
              ),
            ),
            body: SizedBox(
              width: double.infinity,
              height: MediaQuery.of(context).size.height,
              child: Stack(
                alignment: Alignment.center,
                children: [
                  SafeArea(
                    child: page,
                  ),
                ],
              ),
            ),
          );
        });
  }
}

class Responsive extends StatelessWidget {
  final Widget mobile;
  final Widget? tablet;
  final Widget desktop;

  const Responsive({
    super.key,
    required this.mobile,
    this.tablet,
    required this.desktop,
  });

  static bool isMobile(BuildContext context) =>
      MediaQuery.of(context).size.width < 576;

  static bool isTablet(BuildContext context) =>
      MediaQuery.of(context).size.width >= 576 &&
      MediaQuery.of(context).size.width <= 992;

  static bool isDesktop(BuildContext context) =>
      MediaQuery.of(context).size.width > 992;

  @override
  Widget build(BuildContext context) {
    final Size size = MediaQuery.of(context).size;
    if (size.width > 992) {
      return desktop;
    } else if (size.width >= 600 && tablet != null) {
      return tablet!;
    } else {
      return mobile;
    }
  }
}
