import 'package:anyway/layouts/scaffold.dart';
import 'package:anyway/modules/new_trip_button.dart';
import 'package:anyway/structs/landmark.dart';
import 'package:anyway/structs/preferences.dart';
import 'package:anyway/structs/trip.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';



class NewTripPreferencesPage extends StatefulWidget {
  final Trip trip;
  const NewTripPreferencesPage({super.key, required this.trip});

  @override
  _NewTripPreferencesPageState createState() => _NewTripPreferencesPageState();
}

class _NewTripPreferencesPageState extends State<NewTripPreferencesPage> with ScaffoldLayout {
  UserPreferences preferences = UserPreferences();

  @override
  Widget build(BuildContext context) {
    // Ensure that the trip is "empty" save for the start landmark
    // This is necessary because users can swipe back to this page even after the trip has been created
    if (widget.trip.landmarks.length > 1) {
      Landmark start = widget.trip.landmarks.first;
      widget.trip.landmarks.clear();
      widget.trip.addLandmark(start);
    }

    return mainScaffold(
      context,
      child: Scaffold(
        body: ListView(
          children: [
          const Center(
            child: Padding(
            padding: EdgeInsets.only(left: 10, right: 10, top: 20, bottom: 0),
              child: Text('Tell us about your ideal trip.', style: TextStyle(fontSize: 18))
            ),
          ),

          const Divider(indent: 25, endIndent: 25, height: 50),

          durationPicker(preferences.maxTime),

          preferenceSliders([preferences.sightseeing, preferences.shopping, preferences.nature]),

          // Add a conditional padding to avoid the floating button covering the last slider
          Padding(
            padding: EdgeInsets.only(bottom: MediaQuery.of(context).viewInsets.bottom + 80),
            ),
        ]
      ),
      floatingActionButton: NewTripButton(trip: widget.trip, preferences: preferences),
      ),
      title: FutureBuilder(
        future: widget.trip.cityName,
        builder: (context, snapshot) => Text(
          'Your trip to ${snapshot.hasData ? snapshot.data! : "..."}',
        )
      ),
      helpTexts: [
        'Trip preferences',
        'Set your preferences for this trip. These will be used to generate a custom itinerary.'
      ],
    );
  }


  Widget durationPicker(SinglePreference maxTime) {
    return Card(
      margin: const EdgeInsets.only(left: 10, right: 10, top: 10, bottom: 0),
      shadowColor: Colors.grey,
      child: ListTile(
        leading: preferences.maxTime.icon,
        title: Text(preferences.maxTime.description),
        subtitle: CupertinoTimerPicker(
          mode: CupertinoTimerPickerMode.hm,
          initialTimerDuration: const Duration(minutes: 90),
          minuteInterval: 15,
          onTimerDurationChanged: (Duration newDuration) {
            setState(() {
              preferences.maxTime.value = newDuration.inMinutes;
            });
          },
        )
      ),
    );
  }


  Widget preferenceSliders(List<SinglePreference> prefs) {
    List<Card> sliders = [];
      for (SinglePreference pref in prefs) {
      sliders.add(
        Card(
          child: ListTile(
            leading: pref.icon,
            title: Text(pref.name),
            subtitle: Slider(
              value: pref.value.toDouble(),
              min: pref.minVal.toDouble(),
              max: pref.maxVal.toDouble(),
              divisions: pref.maxVal - pref.minVal,
              label: pref.value.toString(),
              onChanged: (double newValue) {
                setState(() {
                  pref.value = newValue.toInt();
                });
              },
            )
          ),
        )
      );
    }

    return Column(
      children: sliders
    );
  }
}

