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();

  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: [
                  DropdownButton(
                    hint: Text(bLandC.text),
                    items: const [
                      DropdownMenuItem(
                        value: 'Baden-Württemberg',
                        child: Text('Baden-Württemberg'),
                      ),
                      DropdownMenuItem(
                        value: 'Bayern',
                        child: Text('Bayern'),
                      ),
                      DropdownMenuItem(
                        value: 'Berlin',
                        child: Text('Berlin'),
                      ),
                      DropdownMenuItem(
                        value: 'Brandenburg',
                        child: Text('Brandenburg'),
                      ),
                      DropdownMenuItem(
                        value: 'Bremen',
                        child: Text('Bremen'),
                      ),
                      DropdownMenuItem(
                        value: 'Hamburg',
                        child: Text('Hamburg'),
                      ),
                      DropdownMenuItem(
                        value: 'Hessen',
                        child: Text('Hessen'),
                      ),
                      DropdownMenuItem(
                        value: 'Mecklenburg-Vorpommern',
                        child: Text('Mecklenburg-Vorpommern'),
                      ),
                      DropdownMenuItem(
                        value: 'Niedersachsen',
                        child: Text('Niedersachsen'),
                      ),
                      DropdownMenuItem(
                        value: 'Nordrhein-Westfalen',
                        child: Text('Nordrhein-Westfalen'),
                      ),
                      DropdownMenuItem(
                        value: 'Rheinland-Pfalz',
                        child: Text('Rheinland-Pfalz'),
                      ),
                      DropdownMenuItem(
                        value: 'Saarland',
                        child: Text('Saarland'),
                      ),
                      DropdownMenuItem(
                        value: 'Sachsen',
                        child: Text('Sachsen'),
                      ),
                      DropdownMenuItem(
                        value: 'Sachsen-Anhalt',
                        child: Text('Sachsen-Anhalt'),
                      ),
                      DropdownMenuItem(
                        value: 'Schleswig-Holstein',
                        child: Text('Schleswig-Holstein'),
                      ),
                      DropdownMenuItem(
                        value: 'Thüringen',
                        child: Text('Thüringen'),
                      ),
                    ],
                    onChanged: (value) {
                      setState(() {
                        bLandC.text = value!;
                      });
                    },
                  ),
                  TextField(
                    controller: bLandC,
                  ),
                  const SizedBox(
                    height: 15,
                  ),
                  TextField(
                    decoration: InputDecoration(
                        hintText: AppLocalizations.of(context)!.apiaddress),
                    controller: apiAddress,
                  )
                ],
              ),
              const SizedBox(
                height: 15,
              ),
              ElevatedButton(
                  onPressed: () {
                    _saveData();
                    Navigator.pushNamedAndRemoveUntil(
                        context, '/home', (route) => false);
                  },
                  child: Text(AppLocalizations.of(context)!.continueB))
            ],
          ),
        ),
      ),
    );
  }
}
