import 'dart:collection';

import 'package:anyway/structs/landmark.dart';
import 'package:flutter/material.dart';

import 'package:anyway/constants.dart';

import 'package:anyway/structs/trip.dart';
import 'package:anyway/modules/trips_overview.dart';
import 'package:anyway/utils/load_trips.dart';

import 'package:anyway/pages/new_trip.dart';
import 'package:anyway/pages/tutorial.dart';
import 'package:anyway/pages/overview.dart';
import 'package:anyway/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 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(APP_NAME)),
      body: Center(child: currentView),
      drawer: Drawer(
        child: Column(
          children: [
            DrawerHeader(
              decoration: BoxDecoration(
                gradient: LinearGradient(colors: [Colors.red, Colors.yellow])
              ),
              child: Center(
                child: Text(
                  APP_NAME,
                  style: TextStyle(
                    color: Colors.grey[800],
                    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")
                  )
                );
              },
            ),
          ],
        ),
      ),
    );
  }
}

// This function is used to get the first trip from a list of trips
// TODO: Implement this function
Trip getFirstTrip(Future<List<Trip>> trips) {
  Trip t1 = Trip(uuid: '1', landmarks: LinkedList<Landmark>());
  t1.landmarks.add(
    Landmark(
      uuid: '0',
      name: "Start",
      location: [48.85, 2.32],
      type: start,
    ),
  );
  t1.landmarks.add(
    Landmark(
      uuid: '1',
      name: "Eiffel Tower",
      location: [48.859, 2.295],
      type: sightseeing,
      imageURL: "https://upload.wikimedia.org/wikipedia/commons/thumb/a/a8/Tour_Eiffel_Wikimedia_Commons.jpg/1037px-Tour_Eiffel_Wikimedia_Commons.jpg"
    ),
  );
  t1.landmarks.add(
    Landmark(
      uuid: "2",
      name: "Notre Dame Cathedral",
      location: [48.8530, 2.3498],
      type: sightseeing,
      imageURL: "https://upload.wikimedia.org/wikipedia/commons/thumb/f/f7/Notre-Dame_de_Paris%2C_4_October_2017.jpg/440px-Notre-Dame_de_Paris%2C_4_October_2017.jpg"
    ),
  );
  t1.landmarks.add(
    Landmark(
      uuid: "3",
      name: "Louvre palace",
      location: [48.8606, 2.3376],
      type: sightseeing,
      imageURL: "https://upload.wikimedia.org/wikipedia/commons/thumb/6/66/Louvre_Museum_Wikimedia_Commons.jpg/540px-Louvre_Museum_Wikimedia_Commons.jpg"
    ),
  );
  t1.landmarks.add(
    Landmark(
      uuid: "4",
      name: "Pont-des-arts",
      location: [48.8585, 2.3376],
      type: sightseeing,
      imageURL: "https://upload.wikimedia.org/wikipedia/commons/thumb/d/d1/Pont_des_Arts%2C_6e_Arrondissement%2C_Paris_%28HDR%29_20140320_1.jpg/560px-Pont_des_Arts%2C_6e_Arrondissement%2C_Paris_%28HDR%29_20140320_1.jpg"
    ),
  );
  t1.landmarks.add(
    Landmark(
      uuid: "5",
      name: "Panthéon",
      location: [48.847, 2.347],
      type: sightseeing,
      imageURL: "https://upload.wikimedia.org/wikipedia/commons/thumb/8/80/Pantheon_of_Paris_007.JPG/1280px-Pantheon_of_Paris_007.JPG"
    ),
  );
  t1.landmarks.add(
    Landmark(
      uuid: "6",
      name: "Galeries Lafayette",
      location: [48.87, 2.32],
      type: shopping,
      imageURL: "https://upload.wikimedia.org/wikipedia/commons/thumb/d/de/GaleriesLafayetteNuit.jpg/220px-GaleriesLafayetteNuit.jpg"
    ),
  );
  return t1;
}