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


bool debugMode = false;

class ProfilePage extends StatefulWidget {
  @override
  _ProfilePageState createState() => _ProfilePageState();
}

class _ProfilePageState extends State<ProfilePage> {
  Future<UserPreferences> _prefs = loadUserPreferences();

  @override
  Widget build(BuildContext context) {
    return ListView(
      children: [
        // First a round, centered image
        Center(
          child: CircleAvatar(
            radius: 100,
            child: Icon(Icons.person, size: 100),
          )
        ),
        Center(
          child: Text('Curious traveler', 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('For a tailored experience, please rate your discovery preferences.', style: TextStyle(fontSize: 18))
          ),
        ),

        FutureBuilder(future: _prefs, builder: futureSliders),

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

        privacyInfo(),

        debugButton(),
      ]
    );
  }

  Widget debugButton() {
    return Padding(
      padding: EdgeInsets.only(top: 20),
      child: Row(
        children: [
          Text('Debug mode'),
          Switch(
            value: debugMode,
            onChanged: (bool? newValue) {
              setState(() {
                debugMode = newValue!;
                showDialog(
                  context: context,
                  builder: (BuildContext context) {
                    return AlertDialog(
                      title: Text('Debug mode - custom API'),
                      content: TextField(
                        decoration: InputDecoration(
                          hintText: 'http://localhost:8000'
                        ),
                      onChanged: (value) {
                        setState(() {
                          API_URL_BASE = value;
                        });
                      },
                    ),
                    actions: [
                      TextButton(
                        child: Text('OK'),
                        onPressed: () {
                          Navigator.of(context).pop();
                        },
                      ),
                    ],
                    );
                  }
                );
              });
            }
          )
        ],
      )
    );
  }


  Widget futureSliders(BuildContext context, AsyncSnapshot<UserPreferences> snapshot) {
    if (snapshot.connectionState == ConnectionState.done) {
      UserPreferences prefs = snapshot.data!;

      return Column(
        children: [
          PreferenceSliders(prefs: [prefs.maxTime, prefs.maxDetour]),
          Divider(indent: 25, endIndent: 25, height: 50),
          PreferenceSliders(prefs: [prefs.sightseeing, prefs.shopping, prefs.nature])
        ]
      );
    } else {
      return CircularProgressIndicator();
    }
  }

  Widget privacyInfo() {
    return Padding(
      padding: EdgeInsets.only(top: 20),
      child: Row(
        children: [
          Text('Privacy policy is available at '),
          TextButton.icon(
            icon: Icon(Icons.info),
            label: Text(PRIVACY_URL),
            onPressed: () {
            }
          )
        ],
      )
    );
  }
}


class PreferenceSliders extends StatefulWidget {
  final List<SinglePreference> prefs;

  PreferenceSliders({required this.prefs});

  @override
  State<PreferenceSliders> createState() => _PreferenceSlidersState();
}


class _PreferenceSlidersState extends State<PreferenceSliders> {

  @override
  Widget build(BuildContext context) {
    List<Card> sliders = [];
      for (SinglePreference pref in widget.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();
                  pref.save();
                });
              },
            )
          ),
          margin: const EdgeInsets.only(left: 10, right: 10, top: 10, bottom: 0),
          shadowColor: Colors.grey,
        )
      );
    }

    return Column(
      children: sliders);
  }
}