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

import 'package:anyway/constants.dart';

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

import 'package:anyway/modules/current_trip_error_message.dart';
import 'package:anyway/modules/current_trip_loading_indicator.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 == 'error') {
          return ListView(
            controller: widget.controller,
            padding: const EdgeInsets.only(top: 10, left: 10, right: 10, bottom: 30),
            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
                // note that we need to account for the padding above
                height: MediaQuery.of(context).size.height * TRIP_PANEL_MIN_HEIGHT - 10,
                child: Center(child: 
                  AutoSizeText(
                    maxLines: 1,
                    'Error',
                    style: greeterStyle
                  )
                ),
              ),

              CurrentTripErrorMessage(trip: widget.trip),
            ],
          );
        } else if (widget.trip.uuid == 'pending') {
            return Align(
              alignment: Alignment.topCenter,
              child: 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,
                child: CurrentTripLoadingIndicator(trip: widget.trip),
              ),
            );
        } else {
          return ListView(
            controller: widget.controller,
            padding: const EdgeInsets.only(top: 10, left: 10, right: 10, bottom: 30),
            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
                // note that we need to account for the padding above
                height: MediaQuery.of(context).size.height * TRIP_PANEL_MIN_HEIGHT - 10,
                child: CurrentTripGreeter(trip: widget.trip),
              ),

              Padding(
                padding: EdgeInsets.all(10),
                child: Container(
                  decoration: BoxDecoration(
                    color: Colors.grey[100],
                    borderRadius: BorderRadius.circular(10),
                  ),
                  child: Column(
                    children: [
                      CurrentTripSummary(trip: widget.trip),
                      if (widget.trip.landmarks.where((Landmark landmark) => landmark.visited).isNotEmpty)
                        ExpansionTile(
                          leading: const Icon(Icons.location_on),
                          title: const Text('Visited Landmarks (tap to expand)'),
                          children: [
                            ...landmarksList(widget.trip, selector: (Landmark landmark) => landmark.visited),
                          ],
                          visualDensity: VisualDensity.compact,
                          collapsedShape: RoundedRectangleBorder(
                            borderRadius: BorderRadius.circular(10),
                          ),
                          shape: RoundedRectangleBorder(
                            borderRadius: BorderRadius.circular(10),
                          ),
                        ),
                    ],
                  ),
                ),
              ),
              

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

              // upcoming landmarks
              ...landmarksList(widget.trip, selector: (Landmark landmark) => landmark.visited == false),

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

              Center(child: saveButton(trip: widget.trip)),
            ],
          );
        }
      }
    );
  }
}