import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';

class IntroScreen extends StatefulWidget {
  const IntroScreen({super.key});

  @override
  State<IntroScreen> createState() => _IntroScreenState();
}

class _IntroScreenState extends State<IntroScreen> {
  TextEditingController addresse1C = TextEditingController();
  TextEditingController bLandC = TextEditingController();
  TextEditingController apiAddress = TextEditingController();

  String selectedApiAddress = "Test";
  String? selectedBLand;

  Future<void> _saveData() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    await prefs.setString('addresse1', addresse1C.text);
    await prefs.setString('bLand', bLandC.text);
    await prefs.setBool('isFirstLaunch', false);
    await prefs.setString('apiAddress', apiAddress.text);
  }

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

  void _loadPrefs() {
    Future.delayed(Duration.zero, () async {
      SharedPreferences prefs = await SharedPreferences.getInstance();
      setState(() {
        apiAddress.text = prefs.getString('apiAddress') ?? "";
        addresse1C.text = prefs.getString('addresse1') ?? "";
        bLandC.text = prefs.getString('bLand') ?? "";
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('FFOrte'),
      ),
      body: Center(
        child: Container(
          padding: const EdgeInsets.all(31),
          child: Column(
            children: [
              TextField(
                decoration: InputDecoration(
                    hintText: AppLocalizations.of(context)!.benutzername),
                controller: addresse1C,
                onChanged: (value) => setState(() {
                  addresse1C.text = value;
                }),
              ),
              const SizedBox(
                height: 15,
              ),
              Column(
                children: [
                  Row(
                    children: [
                      Expanded(
                        flex: 4,
                        child: TextField(
                          controller: bLandC,
                        ),
                      ),
                      Expanded(
                        flex: 1,
                        child: PopupMenuButton(
                          icon: const Icon(Icons.arrow_drop_down),
                          initialValue: selectedBLand,
                          onSelected: (value) {
                            setState(() {
                              selectedBLand = value;
                              bLandC.text = value;
                            });
                          },
                          itemBuilder: (context) => const <PopupMenuEntry>[
                            PopupMenuItem(
                              value: 'Baden-Württemberg',
                              child: Text('Baden-Württemberg'),
                            ),
                            PopupMenuItem(
                              value: 'Bayern',
                              child: Text('Bayern'),
                            ),
                            PopupMenuItem(
                              value: 'Berlin',
                              child: Text('Berlin'),
                            ),
                            PopupMenuItem(
                              value: 'Brandenburg',
                              child: Text('Brandenburg'),
                            ),
                            PopupMenuItem(
                              value: 'Bremen',
                              child: Text('Bremen'),
                            ),
                            PopupMenuItem(
                              value: 'Hamburg',
                              child: Text('Hamburg'),
                            ),
                            PopupMenuItem(
                              value: 'Hessen',
                              child: Text('Hessen'),
                            ),
                            PopupMenuItem(
                              value: 'Mecklenburg-Vorpommern',
                              child: Text('Mecklenburg-Vorpommern'),
                            ),
                            PopupMenuItem(
                              value: 'Niedersachsen',
                              child: Text('Niedersachsen'),
                            ),
                            PopupMenuItem(
                              value: 'Nordrhein-Westfalen',
                              child: Text('Nordrhein-Westfalen'),
                            ),
                            PopupMenuItem(
                              value: 'Rheinland-Pfalz',
                              child: Text('Rheinland-Pfalz'),
                            ),
                            PopupMenuItem(
                              value: 'Saarland',
                              child: Text('Saarland'),
                            ),
                            PopupMenuItem(
                              value: 'Sachsen',
                              child: Text('Sachsen'),
                            ),
                            PopupMenuItem(
                              value: 'Sachsen-Anhalt',
                              child: Text('Sachsen-Anhalt'),
                            ),
                            PopupMenuItem(
                              value: 'Schleswig-Holstein',
                              child: Text('Schleswig-Holstein'),
                            ),
                            PopupMenuItem(
                              value: 'Thüringen',
                              child: Text('Thüringen'),
                            ),
                          ],
                        ),
                      ),
                    ],
                  ),
                  const SizedBox(
                    height: 15,
                  ),
                  Row(
                    children: [
                      Expanded(
                        flex: 4,
                        child: TextField(
                          decoration: InputDecoration(
                              hintText:
                                  AppLocalizations.of(context)!.apiaddress),
                          controller: apiAddress,
                        ),
                      ),
                      Expanded(
                          flex: 1,
                          child: PopupMenuButton(
                            icon: const Icon(Icons.arrow_drop_down),
                            initialValue: selectedApiAddress,
                            onSelected: (value) {
                              setState(() {
                                selectedApiAddress = value;
                                apiAddress.text = value;
                              });
                            },
                            itemBuilder: (context) => <PopupMenuEntry>[
                              PopupMenuItem(
                                  value:
                                      "http://192.168.1.106/www.dbb-wolf.de/data/app24.php",
                                  child:
                                      Text(AppLocalizations.of(context)!.test)),
                              PopupMenuItem(
                                  value: "...",
                                  child: Text(
                                      AppLocalizations.of(context)!.notest))
                            ],
                          ))
                    ],
                  )
                ],
              ),
              const SizedBox(
                height: 15,
              ),
              ElevatedButton(
                  onPressed: () {
                    _saveData();
                    Navigator.pushNamedAndRemoveUntil(
                        context, '/home', (route) => false);
                  },
                  child: Text(AppLocalizations.of(context)!.continueB))
            ],
          ),
        ),
      ),
    );
  }
}
