import 'package:anyway/constants.dart';
import 'package:flutter/material.dart';

import 'package:anyway/structs/trip.dart';
import 'package:anyway/modules/current_trip_summary.dart';
import 'package:anyway/modules/current_trip_save_button.dart';
import 'package:anyway/modules/current_trip_landmarks_list.dart';
import 'package:anyway/modules/current_trip_greeter.dart';


class CurrentTripPanel extends StatefulWidget {
  final ScrollController controller;
  final Trip trip;

  const CurrentTripPanel({
    super.key,
    required this.controller,
    required this.trip,
    });

  @override
  State<CurrentTripPanel> createState() => _CurrentTripPanelState();
}

class _CurrentTripPanelState extends State<CurrentTripPanel> {
  @override
  Widget build(BuildContext context) {
    return ListenableBuilder(
      listenable: widget.trip,
      builder: (context, child) {
        if (widget.trip.uuid != 'pending' && widget.trip.uuid != 'error') {
          return ListView(
            controller: widget.controller,
            padding: const EdgeInsets.only(bottom: 30, left: 5, right: 5),
            children: [
              SizedBox(
                // reuse the exact same height as the panel has when collapsed
                // this way the greeter will be centered when the panel is collapsed
                height: MediaQuery.of(context).size.height * TRIP_PANEL_MIN_HEIGHT - 20,
                child: Greeter(trip: widget.trip),
              ),

              const Padding(padding: EdgeInsets.only(top: 10)),

              // CurrentTripSummary(trip: widget.trip),

              // const Divider(),

              ...landmarksList(widget.trip),

              const Padding(padding: EdgeInsets.only(top: 10)),

              Center(child: saveButton(widget.trip)),
            ],
          );
        } else if(widget.trip.uuid == 'pending') {
          return SizedBox(
            // reuse the exact same height as the panel has when collapsed
            // this way the greeter will be centered when the panel is collapsed
            height: MediaQuery.of(context).size.height * TRIP_PANEL_MIN_HEIGHT - 20,
            child: Greeter(trip: widget.trip)
          );
        } else {
          return Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              const Icon(
                Icons.error_outline,
                color: Colors.red,
                size: 50,
              ),
              Padding(
                padding: const EdgeInsets.only(left: 10),
                child: Text('Error: ${widget.trip.errorDescription}'),
              ),
            ],
          );
        }
      }
    );
  }
}