import 'package:anyway/main.dart';
import 'package:anyway/pages/no_trips_page.dart';
import 'package:anyway/structs/agreement.dart';
import 'package:flutter/material.dart';

import 'package:anyway/structs/trip.dart';
import 'package:anyway/pages/current_trip.dart';
import 'package:anyway/pages/onboarding.dart';


Widget getFirstPage() {
  // check if the user has already seen the onboarding and agreed to the terms of service
  return FutureBuilder(
    future: getAgreement(),
    builder: (context, snapshot) {
      if (snapshot.connectionState == ConnectionState.done) {
        if (snapshot.hasData) {
          Agreement agrement = snapshot.data!;
          if (agrement.agreed) {
            return FutureBuilder(
              future: savedTrips.loadTrips(),
              builder: (context, snapshot) {
                if (snapshot.connectionState == ConnectionState.done) {
                  if (snapshot.hasData) {
                    List<Trip> trips = snapshot.data!;
                    if (trips.isNotEmpty) {
                      return TripPage(trip: trips[0]);
                    } else {
                      return const NoTripsPage();
                    }
                  } else {
                    return const Center(child: CircularProgressIndicator());
                  }
                } else {
                  return const Center(child: CircularProgressIndicator());
                }
              },
            );
          } else {
            return const OnboardingPage();
          }
        } else {
          return const OnboardingPage();
        }
      } else {
        return const OnboardingPage();
      }
    },
  );
}
