import 'package:anyway/modules/new_trip_button.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({required this.trip});

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

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: ListView(
        children: [
          // Center(
          //   child: CircleAvatar(
          //     radius: 100,
          //     child: Icon(Icons.person, size: 100),
          //   )
          // ),
          Center(
            child: FutureBuilder(
              future: widget.trip.cityName,
              builder: (context, snapshot) => Text(
                'New trip to ${snapshot.hasData ? snapshot.data! : "..."}',
                style: TextStyle(fontSize: 24)
              )
            )
          ),

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

          Center(
            child: Padding(
            padding: EdgeInsets.only(left: 10, right: 10, top: 0, bottom: 10),
              child: Text('Tell us about your ideal trip.', style: TextStyle(fontSize: 18))
            ),
          ),

          durationPicker(preferences.maxTime),

          preferenceSliders([preferences.sightseeing, preferences.shopping, preferences.nature]),
        ]
      ),
      floatingActionButton: NewTripButton(trip: widget.trip, preferences: preferences),
    );
  }


  Widget durationPicker(SinglePreference maxTime) {
    return Card(
      margin: const EdgeInsets.only(left: 10, right: 10, top: 10, bottom: 0),
      shadowColor: Colors.grey,
      child: ListTile(
        leading: Icon(Icons.timer),
        title: Text('How long should the trip be?'),
        subtitle: CupertinoTimerPicker(
          mode: CupertinoTimerPickerMode.hm,
          initialTimerDuration: 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(
          margin: const EdgeInsets.only(left: 10, right: 10, top: 10, bottom: 0),
          shadowColor: Colors.grey,
          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
    );
  }
}