import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:shared_preferences/shared_preferences.dart';

import '../../dataOld/accountModel.dart';
import 'debug/debugOverview.dart';

class Settings extends StatefulWidget {
  const Settings({Key? key}) : super(key: key);

  @override
  State<Settings> createState() => _SettingsState();
}

class _SettingsState extends State<Settings> {

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("Einstellungen"),
      ),
      body: ListView(
        children: [

          ListTile(
            leading: const Icon(Icons.logout),
            title: const Text("Konto abmelden"),
            onTap: () {
              showDialog(
                context: context,
                builder: (context) {
                  return AlertDialog(
                    title: const Text("Abmelden?"),
                    content: const Text("Möchtest du dich wirklich abmelden?"),
                    actions: [
                      TextButton(
                          child: const Text("Abmelden"),
                          onPressed: () {
                            SharedPreferences.getInstance().then((value) => {
                              value.clear(),
                            }).then((value) => {
                              Provider.of<AccountModel>(context, listen: false).logout(),
                              Navigator.popUntil(context, (route) => !Navigator.canPop(context)),
                            });
                          }
                      ),

                      TextButton(
                        child: const Text("Abbrechen"),
                        onPressed: () {
                          Navigator.pop(context);
                        },
                      ),
                    ],
                  );
                },
              );
            },
          ),

          ListTile(
            leading: const Icon(Icons.info),
            title: const Text("Informationen und Lizenzen"),
            onTap: () {
              showAboutDialog(
                context: context,
                applicationIcon: const Icon(Icons.send_time_extension_outlined),
                applicationName: "MarianumMobile",
                applicationVersion: "Development Build",
                applicationLegalese: "Marianum Fulda 2023 Elias Müller",
              );
            },
          ),

          ListTile(
            leading: const Icon(Icons.bug_report_outlined),
            title: const Text("Speicheransicht"),
            onTap: () {
              Navigator.push(context, MaterialPageRoute(builder: (context) {
                return const DebugOverview();
              }));
            },
          )

        ],
      ),
    );
  }
}