import 'package:fast_network_navigation/modules/trips_overview.dart';
import 'package:fast_network_navigation/pages/new_trip.dart';
import 'package:fast_network_navigation/pages/tutorial.dart';
import 'package:fast_network_navigation/structs/trip.dart';
import 'package:fast_network_navigation/utils/load_trips.dart';
import 'package:flutter/material.dart';

import 'package:fast_network_navigation/pages/overview.dart';
import 'package:fast_network_navigation/pages/profile.dart';

// BasePage is the scaffold that holds all other pages
// A side drawer is used to switch between pages
class BasePage extends StatefulWidget {
  final String mainScreen;
  final Future<Trip>? trip;
  
  const BasePage({
    super.key,
    required this.mainScreen,
    this.trip
  });

  @override
  State<BasePage> createState() => _BasePageState();
}

class _BasePageState extends State<BasePage> {

  @override
  Widget build(BuildContext context) {
    Widget currentView = const Text("loading...");
    Future<List<Trip>> trips = loadTrips();
    
    
    if (widget.mainScreen == "map") {
      currentView = NavigationOverview(trip: widget.trip ?? getFirstTrip(trips));
    } else if (widget.mainScreen == "tutorial") {
      currentView = TutorialPage();
    } else if (widget.mainScreen == "profile") {
      currentView = ProfilePage();
    }

    final ThemeData theme = Theme.of(context);
    
    return Scaffold(
      appBar: AppBar(title: Text("City Nav")),
      body: Center(child: currentView),
      drawer: Drawer(
        child: Column(
          children: [
            DrawerHeader(
              decoration: BoxDecoration(
                gradient: LinearGradient(colors: [Colors.cyan, theme.primaryColor])
              ),
              child: Center(
                child: Text(
                  'City Nav',
                  style: TextStyle(
                    color: Colors.white,
                    fontSize: 24,
                    fontWeight: FontWeight.bold,
                  ),
                ),
              ),
            ),
            ListTile(
              title: const Text('Your Trips'),
              leading: const Icon(Icons.map),
              selected: widget.mainScreen == "map",
              onTap: () {},
              trailing: ElevatedButton(
                onPressed: () {
                  Navigator.of(context).push(
                    MaterialPageRoute(
                      builder: (context) => const NewTripPage()
                    )
                  );
                },
                child: const Text('New'),
              ),
            ),

            // Adds a ListView to the drawer. This ensures the user can scroll
            // through the options in the drawer if there isn't enough vertical
            // space to fit everything.
            Expanded(
              child: TripsOverview(trips: trips),
            ),
            ElevatedButton(
              onPressed: () async {
                removeAllTripsFromPrefs();
              },
              child: const Text('Clear trips'),
            ),
            const Divider(),
            ListTile(
              title: const Text('How to use'),
              leading: Icon(Icons.help),
              selected: widget.mainScreen == "tutorial",
              onTap: () {
                Navigator.of(context).push(
                  MaterialPageRoute(
                    builder: (context) => BasePage(mainScreen: "tutorial")
                  )
                );
              },
            ),

            // settings in the bottom of the drawer
            ListTile(
              title: const Text('Settings'),
              leading: const Icon(Icons.settings),
              selected: widget.mainScreen == "profile",
              onTap: () {
                Navigator.of(context).push(
                  MaterialPageRoute(
                    builder: (context) => BasePage(mainScreen: "profile")
                  )
                );
              },
            ),
          ],
        ),
      ),
    );
  }
}



Future<Trip> getFirstTrip (Future<List<Trip>> trips) async {
  List<Trip> tripsf = await trips;
  return tripsf[0];
}