import 'package:anyway/pages/current_trip.dart';
import 'package:anyway/utils/load_trips.dart';
import 'package:flutter/material.dart';

import 'package:anyway/structs/trip.dart';


class TripsOverview extends StatefulWidget {
  final SavedTrips trips;
  const TripsOverview({
    super.key,
    required this.trips,
    });

  @override
  State<TripsOverview> createState() => _TripsOverviewState();
}

class _TripsOverviewState extends State<TripsOverview> {
  Widget tripListItemBuilder(BuildContext context, int index) {
    Trip trip = widget.trips.trips[index];
    return ListTile(
      title: FutureBuilder(
        future: trip.cityName,
        builder: (BuildContext context, AsyncSnapshot<String> snapshot) {
          if (snapshot.hasData) {
            return Text("Trip to ${snapshot.data}");
          } else if (snapshot.hasError) {
            return Text("Error: ${snapshot.error}");
          } else {
            return const Text("Trip to ...");
          }
        },
      ),
      // emoji of the country flag of the trip's country
      leading: const Icon(Icons.pin_drop),

      onTap: () {
        Navigator.of(context).push(
          MaterialPageRoute(
            builder: (context) => TripPage(trip: trip)
          )
        );
      },
    );
  }


  // Widget listBuild (BuildContext context, SavedTrips trips) {
  //   List<Widget> children;
  //   List<Trip> items = trips.trips;
  //   children = List<Widget>.generate(items.length, (index) {
  //     Trip trip = items[index];
  //     return ListTile(
  //       title: FutureBuilder(
  //         future: trip.cityName,
  //         builder: (BuildContext context, AsyncSnapshot<String> snapshot) {
  //           if (snapshot.hasData) {
  //             return Text("Trip to ${snapshot.data}");
  //           } else if (snapshot.hasError) {
  //             return Text("Error: ${snapshot.error}");
  //           } else {
  //             return const Text("Trip to ...");
  //           }
  //         },
  //       ),
  //       leading: const Icon(Icons.pin_drop),
  //       onTap: () {
  //         Navigator.of(context).push(
  //           MaterialPageRoute(
  //             builder: (context) => TripPage(trip: trip)
  //           )
  //         );
  //       },
  //     );
  //   });

  //   return ListView(
  //     padding: const EdgeInsets.only(top: 0),
  //     children: children,
  //   );
  // }

  @override
  Widget build(BuildContext context) {
    return ListenableBuilder(
      listenable: widget.trips,
      builder: (BuildContext context, Widget? child) => ListView.builder(
        itemCount: widget.trips.trips.length,
        itemBuilder: tripListItemBuilder,
      )
    );
  }
}
